File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` cpp
2+ #include < bits/stdc++.h>
3+ using namespace std ;
4+
5+ int Q, MOD;
6+ vector<int > pos[1000001 ]{};
7+ stack<int > st;
8+ set<int > s;
9+
10+ int main (){
11+ cin.tie(0)->sync_with_stdio(0);
12+
13+ cin>>Q>>MOD;
14+ if(MOD > Q) {
15+ for(int o,a;Q--;) {
16+ cin>>o;
17+ if(o == 1) cin>>a;
18+ if(o == 3) cout<<"-1\n";
19+ }
20+ return 0;
21+ }
22+
23+ for(int i=0,o,a;i<Q;i++) {
24+ cin>>o;
25+ if(o == 1) {
26+ cin>>a;
27+ a %= MOD;
28+ if(!pos[a].empty()) s.erase(pos[a].back());
29+ s.insert(st.size());
30+ pos[a].push_back(st.size());
31+ st.push(a);
32+ }
33+ else if(o == 2) {
34+ if(st.empty()) continue;
35+ a = st.top(); st.pop();
36+ s.erase(pos[a].back());
37+ pos[a].pop_back();
38+ if(!pos[a].empty()) s.insert(pos[a].back());
39+ }
40+ else {
41+ if(s.size() != MOD) cout<<"-1\n";
42+ else cout<<st.size()-(*s.begin())<<'\n';
43+ }
44+ }
45+
46+ }
47+ ```
You can’t perform that action at this time.
0 commit comments