Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* In this problem persistent data structure is needed..It means we have to keep a track of the previous updates.Because in some query it is said to restore the form after the kth update...It can be solved using a simple dfs..Where we make edges like ....
- after performing the ith query we make an edge to i to i+1 th query..whenever it is said to restore the kth version then we make an edge to k to ith query....Remember when dfs traversal is finished in a node we have to discard all changes made due to this query...Then simply proceed to next node....*/
- vector<piii>v[MAX];int ans[MAX];
- bitset<1002>b[1002],bit;int tot;
- void dfs(int s)
- {
- for(auto it : v[s])
- {
- int type = it.ff.ff;
- int pos = it.ff.ss;
- int r = it.ss.ff;
- int c = it.ss.ss;
- if(type==1)
- {
- int val = b[r][c];
- tot-=b[r].count();
- b[r][c] = 1;
- tot+=b[r].count();
- ans[pos] = tot;
- dfs(pos);
- tot-=b[r].count();
- b[r][c] = val;
- tot+=b[r].count();
- }
- if(type==2)
- {
- int val = b[r][c];
- tot-=b[r].count();
- b[r][c] = 0;
- tot+=b[r].count();
- ans[pos] = tot;
- dfs(pos);
- tot-=b[r].count();
- b[r][c] = val;
- tot+=b[r].count();
- }
- if(type==3)
- {
- tot-=b[r].count();
- b[r]^=bit;
- tot+=b[r].count();
- ans[pos] = tot;
- dfs(pos);
- tot -= b[r].count();
- b[r]^=bit;
- tot += b[r].count();
- }
- if(type==4)
- {
- ans[pos] = tot;
- dfs(pos);
- }
- }
- }
- int main()
- {
- booster()
- ///read("input.txt");
- int n,m,q;
- cin>>n>>m>>q;
- for(int i = 1;i<=m;i++)bit[i] = 1;
- int cur = 0;
- for(int i = 1;i<=q;i++)
- {
- int c;
- int x,y;
- cin>>c>>x;
- if(c==1||c==2)cin>>y;
- if(c!=4)
- {
- v[cur].pb(piii(pii(c,i),pii(x,y)));
- }
- else
- {
- v[x].pb(piii(pii(c,i),pii(x,y)));
- }
- cur = i;
- }
- dfs(0);
- for(int i = 1;i<=q;i++)cout<<ans[i]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement