zoukankan      html  css  js  c++  java
  • POJ 3225 (线段树 区间更新) Help with Intervals

    这道题搞了好久,其实坑点挺多。。

    网上找了许多题解,发现思路其实都差不多,所以就不在重复了。

    推荐一篇比较好的题解,请戳这

    另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup函数,仅有一个pushdown。

      1 #include <cstdio>
      2 
      3 const int maxn = 131072;
      4 //const int maxn = 10;
      5 int qL, qR, op;
      6 int setv[maxn << 2], xorv[maxn << 2];
      7 bool in[maxn + 10];
      8 
      9 void pushdown(int o)
     10 {
     11     int lc = o*2, rc = o*2+1;
     12     if(setv[o] >= 0)
     13     {
     14         setv[lc] = setv[rc] = setv[o];
     15         setv[o] = -1;
     16         xorv[lc] = xorv[rc] = 0;
     17     }
     18     if(xorv[o])
     19     {
     20         if(setv[lc] >= 0) setv[lc] ^= 1; else xorv[lc] ^= 1;
     21         if(setv[rc] >= 0) setv[rc] ^= 1; else xorv[rc] ^= 1;
     22         xorv[o] = 0;
     23     }
     24 }
     25 
     26 void update(int o, int L, int R, int qL, int qR)
     27 {
     28     if(qL <= L && qR >= R)
     29     {
     30         if(op == 1) { setv[o] = xorv[o] = 0; }
     31         else if(op == 2) { setv[o] = 1; xorv[o] = 0; }
     32         else if(op == 3) { if(setv[o] >= 0) setv[o] ^= 1; else xorv[o] ^= 1; }
     33         return;
     34     }
     35     int M = (L + R) / 2;
     36     pushdown(o);
     37     if(qL <= M) update(o*2, L, M, qL, qR);
     38     if(qR > M) update(o*2+1, M+1, R, qL, qR);
     39 }
     40 
     41 void query(int o, int L, int R)
     42 {
     43     if(setv[o] >= 0)
     44     {
     45         if(setv[o] == 1) for(int i = L; i <= R; i++) in[i] = true;
     46         return;
     47     }
     48     pushdown(o);
     49     if(L == R) return;
     50     int M = (L + R) / 2;
     51     query(o*2, L, M);
     52     query(o*2+1, M+1, R);
     53 }
     54 
     55 char cmd, _left, _right;
     56 
     57 int main()
     58 {
     59     //freopen("in.txt", "r", stdin);
     60 
     61     while(scanf("%c %c%d,%d%c
    ", &cmd, &_left, &qL, &qR, &_right) >= 0)
     62     {
     63         qL <<= 1; qR <<= 1;
     64         if(_left == '(') qL++; if(_right == ')') qR--;
     65         if(qL > qR && (cmd == 'I' || cmd == 'C')) { setv[1] = 0; xorv[1] = 0; continue; }
     66         if(cmd == 'U') { op = 2; update(1, 0, maxn, qL, qR); }
     67         else if(cmd == 'I')
     68         {
     69             op = 1;
     70             if(qL - 1 >= 0) update(1, 0, maxn, 0, qL-1);
     71             if(qR + 1 <= maxn) update(1, 0, maxn, qR+1, maxn);
     72         }
     73         else if(cmd == 'D') { op = 1; update(1, 0, maxn, qL, qR); }
     74         else if(cmd == 'C')
     75         {
     76             op = 1;
     77             if(qL - 1 >= 0) update(1, 0, maxn, 0, qL-1);
     78             if(qR + 1 <= maxn) update(1, 0, maxn, qR+1, maxn);
     79             op = 3;
     80             update(1, 0, maxn, qL, qR);
     81         }
     82         else if(cmd == 'S') { op = 3; update(1, 0, maxn, qL, qR); }
     83     }
     84     query(1, 0, maxn);
     85     bool print = false;
     86     int s = -1, e;
     87     for(int i = 0; i <= maxn; i++)
     88     {
     89         if(in[i])
     90         {
     91             if(s == -1) s = i;
     92             e = i;
     93         }
     94         else if(s != -1)
     95         {
     96             if(print) printf(" ");
     97             print = true;
     98             printf("%c%d,%d%c", s&1?'(':'[', s>>1, (e+1)>>1, e&1?')':']');
     99             s = -1;
    100         }
    101     }
    102 
    103     if(!print) puts("empty set");
    104 
    105     return 0;
    106 }
    代码君
  • 相关阅读:
    Lattice Diamond 的学习之新建工程
    candence 笔记总结
    音乐发生器的原理
    PAL/NTSC 制电视广播技术有关知识--FPGA
    以后绝对不能再犯的错误
    BZOJ1112: [POI2008]砖块Klo
    Luogu P1533 可怜的狗狗
    BZOJ4542: [Hnoi2016]大数
    BZOJ5131: [CodePlus2017年12月]可做题2
    BZOJ3083: 遥远的国度
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4462657.html
Copyright © 2011-2022 走看看