Advertisement
RaFiN_

cf-707D

Jan 8th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. /* 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 ....
  2.  
  3. 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....*/
  4.  
  5.  
  6. vector<piii>v[MAX];int ans[MAX];
  7.  
  8. bitset<1002>b[1002],bit;int tot;
  9.  
  10. void dfs(int s)
  11. {
  12.     for(auto it : v[s])
  13.     {
  14.         int type = it.ff.ff;
  15.         int pos = it.ff.ss;
  16.         int r = it.ss.ff;
  17.         int c = it.ss.ss;
  18.         if(type==1)
  19.         {
  20.             int val = b[r][c];
  21.             tot-=b[r].count();
  22.             b[r][c] = 1;
  23.             tot+=b[r].count();
  24.             ans[pos] = tot;
  25.             dfs(pos);
  26.             tot-=b[r].count();
  27.             b[r][c] = val;
  28.             tot+=b[r].count();
  29.  
  30.         }
  31.         if(type==2)
  32.         {
  33.             int val = b[r][c];
  34.             tot-=b[r].count();
  35.             b[r][c] = 0;
  36.             tot+=b[r].count();
  37.             ans[pos] = tot;
  38.             dfs(pos);
  39.             tot-=b[r].count();
  40.             b[r][c] = val;
  41.             tot+=b[r].count();
  42.  
  43.         }
  44.         if(type==3)
  45.         {
  46.             tot-=b[r].count();
  47.             b[r]^=bit;
  48.             tot+=b[r].count();
  49.             ans[pos] = tot;
  50.             dfs(pos);
  51.             tot -= b[r].count();
  52.             b[r]^=bit;
  53.             tot += b[r].count();
  54.         }
  55.         if(type==4)
  56.         {
  57.             ans[pos] = tot;
  58.             dfs(pos);
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. int main()
  65. {
  66.     booster()
  67.     ///read("input.txt");
  68.  
  69.     int n,m,q;
  70.     cin>>n>>m>>q;
  71.     for(int i = 1;i<=m;i++)bit[i] = 1;
  72.     int cur = 0;
  73.     for(int i = 1;i<=q;i++)
  74.     {
  75.         int c;
  76.         int x,y;
  77.         cin>>c>>x;
  78.         if(c==1||c==2)cin>>y;
  79.         if(c!=4)
  80.         {
  81.             v[cur].pb(piii(pii(c,i),pii(x,y)));
  82.         }
  83.         else
  84.         {
  85.             v[x].pb(piii(pii(c,i),pii(x,y)));
  86.         }
  87.         cur = i;
  88.  
  89.     }
  90.  
  91.     dfs(0);
  92.  
  93.     for(int i = 1;i<=q;i++)cout<<ans[i]<<endl;
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement