HDU 3397 Sequence operation

来源:互联网 发布:知柏地黄丸伤胃吗 编辑:程序博客网 时间:2024/05/17 09:26

 上次月赛的时候没做出来,写了8K左右的代码,今天回头看,发现以前把东西都和在一起写很容易出错。

今天就重新写了变,还是很不熟练,WR了很多次,不过最后还是AC了。

Code:
  1. #include <cstdlib>      
  2. #include <cstring>      
  3. #include <memory>      
  4. #include <cstdio>      
  5. #include <fstream>      
  6. #include <iostream>      
  7. #include <cmath>      
  8. #include <string>      
  9. #include <sstream>      
  10. #include <stack>      
  11. #include <queue>      
  12. #include <vector>      
  13. #include <set>      
  14. #include <map>      
  15. #include <algorithm>      
  16. #include <deque>      
  17. #include <list>      
  18. using namespace std;  
  19. #define MAXN 100010  
  20. struct node  
  21. {  
  22.     int left, right, mid, len;  
  23.     int right0, left0, max0, count0;  
  24.     int right1, left1, max1, count1;  
  25.     bool fill0, fill1, chg;  
  26. };  
  27. node tree[MAXN*4];  
  28. int n, m, p[MAXN];  
  29. void updata(int i)  
  30. {  
  31.     tree[i].count0 = tree[i*2].count0 + tree[i*2+1].count0;  
  32.     tree[i].count1 = tree[i*2].count1 + tree[i*2+1].count1;  
  33.     tree[i].left0  = tree[i*2].left0==tree[i*2].len?tree[i*2].left0+tree[i*2+1].left0:tree[i*2].left0;  
  34.     tree[i].left1  = tree[i*2].left1==tree[i*2].len?tree[i*2].left1+tree[i*2+1].left1:tree[i*2].left1;  
  35.     tree[i].right0 = tree[i*2+1].right0==tree[i*2+1].len?tree[i*2+1].right0+tree[i*2].right0:tree[i*2+1].right0;  
  36.     tree[i].right1 = tree[i*2+1].right1==tree[i*2+1].len?tree[i*2+1].right1+tree[i*2].right1:tree[i*2+1].right1;  
  37.     tree[i].max0   = max(tree[i*2].right0 + tree[i*2+1].left0,max(tree[i*2].max0,tree[i*2+1].max0));  
  38.     tree[i].max1   = max(tree[i*2].right1 + tree[i*2+1].left1,max(tree[i*2].max1,tree[i*2+1].max1));  
  39. }  
  40. void build(int l,int r,int i)  
  41. {  
  42.     tree[i].left = l;  
  43.     tree[i].right = r;  
  44.     tree[i].mid = (l + r) / 2;  
  45.     tree[i].len = (r - l);  
  46.     tree[i].fill0 = tree[i].fill1 = tree[i].chg = 0;  
  47.     if(l + 1 == r)  
  48.     {  
  49.         tree[i].count0 = tree[i].max0 = tree[i].right0 = tree[i].left0 = !p[l];  
  50.         tree[i].count1 = tree[i].max1 = tree[i].right1 = tree[i].left1 = p[l];  
  51.         return ;  
  52.     }  
  53.     int mid = (l + r) / 2;  
  54.     build(l, tree[i].mid, i*2);  
  55.     build(tree[i].mid, r, i*2+1);  
  56.     updata(i);  
  57. }  
  58. void fill0(int i)  
  59. {  
  60.     int lt = 2*i, rt = 2*i+1;  
  61.     tree[i].count0 = tree[i].max0 = tree[i].right0 = tree[i].left0 = tree[i].len;  
  62.     tree[i].count1 = tree[i].max1 = tree[i].right1 = tree[i].left1 = 0;  
  63.     tree[i].fill0 = false;  
  64.     if(tree[i].left + 1 == tree[i].right) return ;  
  65.     tree[lt].fill0 = tree[rt].fill0 = 1;  
  66.     tree[lt].fill1 = tree[rt].fill1 = 0;  
  67.     tree[lt].chg = tree[rt].chg = 0;  
  68. }  
  69. void fill1(int i)  
  70. {  
  71.     int lt = 2*i, rt = 2*i+1;  
  72.     tree[i].count0 = tree[i].max0 = tree[i].right0 = tree[i].left0 = 0;  
  73.     tree[i].count1 = tree[i].max1 = tree[i].right1 = tree[i].left1 = tree[i].len;  
  74.     tree[i].fill1 = false;  
  75.     if(tree[i].left + 1 == tree[i].right) return ;  
  76.     tree[lt].fill0 = tree[rt].fill0 = 0;  
  77.     tree[lt].fill1 = tree[rt].fill1 = 1;  
  78.     tree[lt].chg = tree[rt].chg = 0;  
  79. }  
  80. void chg(int i)  
  81. {  
  82.     int lt = 2*i, rt = 2*i+1;  
  83.     swap(tree[i].count0,tree[i].count1);  
  84.     swap(tree[i].max0,tree[i].max1);  
  85.     swap(tree[i].left0,tree[i].left1);  
  86.     swap(tree[i].right0,tree[i].right1);  
  87.     tree[i].chg = false;  
  88.     if(tree[i].left + 1 == tree[i].right) return ;  
  89.     tree[lt].chg ^= true;  
  90.     tree[rt].chg ^= true;  
  91. }  
  92. void down(int i)  
  93. {  
  94.     if(tree[i].fill0) fill0(i);  
  95.     if(tree[i].fill1) fill1(i);  
  96.     if(tree[i].chg) chg(i);  
  97. }  
  98. void change(int l,int r,int i,int op)  
  99. {  
  100.     down(i);  
  101.     if(l == tree[i].left && r == tree[i].right)  
  102.     {  
  103.         if(op == 0) tree[i].fill0 = 1;  
  104.         else if(op == 1) tree[i].fill1 = 1;  
  105.         else tree[i].chg ^= 1;  
  106.         down(i);  
  107.         return ;  
  108.     }  
  109.     if(r <= tree[i].mid) change(l,r,2*i,op);  
  110.     else if( l >= tree[i].mid) change(l,r,2*i+1,op);  
  111.     else  
  112.     {  
  113.         change(l,tree[i].mid,2*i,op);  
  114.         change(tree[i].mid,r,2*i+1,op);  
  115.     }  
  116.     down(i*2);  
  117.     down(i*2+1);  
  118.     updata(i);  
  119. }  
  120. int onenum(int l,int r,int i)  
  121. {  
  122.     down(i);  
  123.     if(l == tree[i].left && r == tree[i].right) return tree[i].count1;  
  124.     if(r <= tree[i].mid) return onenum(l,r,2*i);  
  125.     else if( l >= tree[i].mid) return onenum(l,r,2*i+1);  
  126.     else return onenum(l,tree[i].mid,2*i) + onenum(tree[i].mid,r,2*i+1);  
  127. }  
  128. int lonenum(int l,int r,int i)  
  129. {  
  130.     down(i);  
  131.     if(l == tree[i].left && r == tree[i].right)    return tree[i].max1;  
  132.     if(r <= tree[i].mid) return lonenum(l,r,2*i);  
  133.     else if( l >= tree[i].mid) return lonenum(l,r,2*i+1);  
  134.     else return max(max(lonenum(l,tree[i].mid,2*i), lonenum(tree[i].mid,r,2*i+1)),  
  135.         min(tree[2*i].right1,tree[i].mid-l)+min(tree[2*i+1].left1,r-tree[i].mid));  
  136. }  
  137. int main ( )  
  138. {  
  139.     int tcase, a, b, op;  
  140.     for(scanf("%d", &tcase);tcase--;)  
  141.     {  
  142.         scanf("%d%d", &n, &m);  
  143.         for(int i = 0; i < n; i++)  
  144.             scanf("%d", &p[i]);  
  145.         build(0,n,1);  
  146.         for(int i = 0; i < m; i++)  
  147.         {  
  148.             scanf("%d%d%d", &op, &a, &b);  
  149.             if(op<=2) change(a,b+1,1,op);  
  150.             else if(op==3) printf("%d/n", onenum(a,b+1,1));  
  151.             else printf("%d/n", lonenum(a,b+1,1));  
  152.         }  
  153.     }  
  154.     return 0;  
  155. }  

 

原创粉丝点击