Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 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.         }
  22.         V[v][1].ans = 1;
  23.         if(in[l] == 0) {
  24.             V[v][1].cnt_0 = 1 - V[v][0].cnt_0;
  25.             V[v][1].cnt_1 = 1 - V[v][0].cnt_1;
  26.         }
  27.         return;
  28.     }
  29.  
  30.     int mid = (l+r)>>1;
  31.     build(1,mid, 2*v);
  32.     build(mid+1, r, 2*v+1);
  33.     V[v][0].cnt_0 = V[v*2][0].cnt_0 + V[v*2+1][0].cnt_0;
  34.     V[v][0].cnt_1 = V[v*2][0].cnt_1 + V[v*2+1][0].cnt_1;
  35.     V[v][1].cnt_0 = V[v*2][1].cnt_0 + V[v*2+1][1].cnt_0;
  36.     V[v][1].cnt_1 = V[v*2][1].cnt_1 + V[v*2+1][1].cnt_1;
  37.  
  38.     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);
  39.     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);
  40.     cout<<l<<" "<<r<<" "<<V[v][0].ans<<endl;
  41. }
  42.  
  43. void toggle(int l, int r, int v, int lll, int rrr) {
  44.     if(lll <= l && rrr >= r) {
  45.         swap(V[v][0], V[v][1]);
  46.         T[v] = 1 - T[v];
  47.         return;
  48.     }
  49.  
  50.     int mid = (l+r)>>1;
  51.     if(T[v] == 1) {
  52.         swap(V[v*2][0], V[v*2][1]);
  53.         T[v*2] = 1 - T[v*2];
  54.         swap(V[v*2+1][0], V[v*2+1][1]);
  55.         T[v*2+1] = 1 - T[v*2+1];
  56.         T[v] = 0;
  57.     }
  58.  
  59.     if(lll <= mid)
  60.         toggle(l, mid, 2*v, lll, rrr);
  61.     if(rrr > mid)
  62.         toggle(mid+1, r, 2*v+1, lll, rrr);
  63.  
  64.     V[v][0].cnt_0 = V[v*2][0].cnt_0 + V[v*2+1][0].cnt_0;
  65.     V[v][0].cnt_1 = V[v*2][0].cnt_1 + V[v*2+1][0].cnt_1;
  66.     V[v][1].cnt_0 = V[v*2][1].cnt_0 + V[v*2+1][1].cnt_0;
  67.     V[v][1].cnt_1 = V[v*2][1].cnt_1 + V[v*2+1][1].cnt_1;
  68.     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);
  69.     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);
  70. }
  71.  
  72.  
  73. int main() {
  74.     cin>>n>>m;
  75.     char ch;
  76.     for(int i = 1; i <= n; ++i) {
  77.         cin>>ch;
  78.         if(ch == 0)
  79.             in[i] = 0;
  80.         else
  81.             in[i] = 1;
  82.     }
  83.  
  84.     build(1, n, 1);
  85.  
  86.     int type, l, r;
  87.     for(int i = 0; i < m; ++i) {
  88.         cin>>type;
  89.         if(type == 1) {
  90.             cout<<V[1][0].ans<<endl;
  91.         } else {
  92.             cin>>l>>r;
  93.             toggle(1, n, 1, l, r);
  94.         }
  95.     }
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement