Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAXN 100100
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7.     int ans;
  8.     int cnt_0, cnt_1;
  9. } V[4*MAXN][2]; // 0 - original, 1 - toggled
  10.  
  11. int n,m;
  12. int in[MAXN];
  13. int T[4*MAXN];
  14.  
  15. void build(int l, int r, int v) {
  16.     if(l == r) {
  17.         V[v][0].ans = 1;
  18.         if(in[l] == 0) {
  19.             V[v][0].cnt_0 = 1;
  20.             V[v][0].cnt_1 = 0;
  21.         } else {
  22.             V[v][0].cnt_0 = 0;
  23.             V[v][0].cnt_1 = 1;
  24.         }
  25.         V[v][1].ans = 1;
  26.         V[v][1].cnt_0 = 1 - V[v][0].cnt_0;
  27.         V[v][1].cnt_1 = 1 - V[v][0].cnt_1;
  28.         return;
  29.     }
  30.  
  31.     int mid = (l+r)>>1;
  32.     build(l,mid, 2*v);
  33.     build(mid+1, r, 2*v+1);
  34.     V[v][0].cnt_0 = V[v*2][0].cnt_0 + V[v*2+1][0].cnt_0;
  35.     V[v][0].cnt_1 = V[v*2][0].cnt_1 + V[v*2+1][0].cnt_1;
  36.     V[v][1].cnt_0 = V[v*2][1].cnt_0 + V[v*2+1][1].cnt_0;
  37.     V[v][1].cnt_1 = V[v*2][1].cnt_1 + V[v*2+1][1].cnt_1;
  38.  
  39.     V[v][0].ans = max(V[v*2][0].ans + V[v*2+1][0].cnt_1, V[v*2][0].cnt_0 + V[v*2+1][0].ans);
  40.     V[v][1].ans = max(V[v*2][1].ans + V[v*2+1][1].cnt_1, V[v*2][1].cnt_0 + V[v*2+1][1].ans);
  41.     cout<<l<<" "<<r<<" "<<v<<" "<<V[v][0].ans<<" "<<V[v*2][0].ans<<" "<<V[v*2+1][0].cnt_1<<endl;
  42. }
  43.  
  44. void toggle(int l, int r, int v, int lll, int rrr) {
  45.     if(lll <= l && rrr >= r) {
  46.         swap(V[v][0], V[v][1]);
  47.         T[v] = 1 - T[v];
  48.         return;
  49.     }
  50.  
  51.     int mid = (l+r)>>1;
  52.     if(T[v] == 1) {
  53.         swap(V[v*2][0], V[v*2][1]);
  54.         T[v*2] = 1 - T[v*2];
  55.         swap(V[v*2+1][0], V[v*2+1][1]);
  56.         T[v*2+1] = 1 - T[v*2+1];
  57.         T[v] = 0;
  58.     }
  59.  
  60.     if(lll <= mid)
  61.         toggle(l, mid, 2*v, lll, rrr);
  62.     if(rrr > mid)
  63.         toggle(mid+1, r, 2*v+1, lll, rrr);
  64.  
  65.     V[v][0].cnt_0 = V[v*2][0].cnt_0 + V[v*2+1][0].cnt_0;
  66.     V[v][0].cnt_1 = V[v*2][0].cnt_1 + V[v*2+1][0].cnt_1;
  67.     V[v][1].cnt_0 = V[v*2][1].cnt_0 + V[v*2+1][1].cnt_0;
  68.     V[v][1].cnt_1 = V[v*2][1].cnt_1 + V[v*2+1][1].cnt_1;
  69.     V[v][0].ans = max(V[v*2][0].ans + V[v*2+1][0].cnt_1, V[v*2][0].cnt_0 + V[v*2+1][0].ans);
  70.     V[v][1].ans = max(V[v*2][1].ans + V[v*2+1][1].cnt_1, V[v*2][1].cnt_0 + V[v*2+1][1].ans);
  71. }
  72.  
  73.  
  74. int main() {
  75.     cin>>n>>m;
  76.     char ch;
  77.     for(int i = 1; i <= n; ++i) {
  78.         cin>>ch;
  79.         if(ch == 0)
  80.             in[i] = 0;
  81.         else
  82.             in[i] = 1;
  83.     }
  84.  
  85.     build(1, n, 1);
  86.  
  87.     int type, l, r;
  88.     for(int i = 0; i < m; ++i) {
  89.         cin>>type;
  90.         if(type == 1) {
  91.             cout<<V[1][0].ans<<endl;
  92.         } else {
  93.             cin>>l>>r;
  94.             toggle(1, n, 1, l, r);
  95.         }
  96.     }
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement