zoukankan      html  css  js  c++  java
  • bzoj 2209

    和上题一样,线段树不能区间翻转?(百度误导人那)

    就只能splay了

      1 #include<bits/stdc++.h>
      2 #define inc(i,l,r) for(int i=l;i<=r;i++)
      3 #define dec(i,l,r) for(int i=l;i>=r;i--)
      4 #define link(x) for(edge *j=h[x];j;j=j->next)
      5 #define mem(a) memset(a,0,sizeof(a))
      6 #define inf 1e9
      7 #define succ(x) (1<<x)
      8 #define NM 100000+5
      9 using namespace std;
     10 int read(){
     11     int x=0,f=1;char ch=getchar();
     12     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     13     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     14     return x*f;
     15 }
     16 int c[NM][2],a[NM],b[NM],f[NM],ll[NM],lr[NM],rr[NM],rl[NM],s[NM],z[NM],tag[NM],tmp[NM];
     17 int n,m,_x,_y,_t,root;
     18 char st[NM];
     19 void pushdown(int i){
     20     if(!i)return;
     21     if(z[i]){
     22         swap(ll[i],lr[i]);swap(rr[i],rl[i]);
     23         a[i]*=-1;s[i]*=-1;ll[i]*=-1;lr[i]*=-1;rr[i]*=-1;rl[i]*=-1;
     24         z[i]=0;z[c[i][0]]^=1;z[c[i][1]]^=1;
     25     }
     26     if(tag[i]){
     27         swap(c[i][0],c[i][1]);swap(ll[i],rl[i]);swap(rr[i],lr[i]);
     28         tag[i]=0;tag[c[i][0]]^=1;tag[c[i][1]]^=1;
     29     }
     30 }
     31 void up(int r){
     32     a[0]=b[0]=ll[0]=lr[0]=rr[0]=rl[0]=c[0][0]=c[0][1]=0;
     33     int x=c[r][0],y=c[r][1];
     34     pushdown(x);pushdown(y);
     35     b[r]=b[x]+b[y]+1;
     36     s[r]=s[x]+s[y]+a[r];
     37     ll[r]=max(ll[x],s[x]+a[r]+ll[y]);
     38     lr[r]=min(lr[x],s[x]+a[r]+lr[y]);
     39     rl[r]=max(rl[y],s[y]+a[r]+rl[x]);
     40     rr[r]=min(rr[y],s[y]+a[r]+rr[x]);
     41 }
     42 void newnode(int r,int fa,int k){
     43     a[r]=s[r]=k;b[r]=1;f[r]=fa;
     44     if(s[r]==1)ll[r]=rl[r]=1;else rr[r]=lr[r]=-1;
     45 }
     46 int build(int fa,int x,int y){
     47     int t=x+y>>1;
     48     if(x>y)return 0;
     49     c[t][0]=build(t,x,t-1);
     50     newnode(t,fa,tmp[t]);
     51     c[t][1]=build(t,t+1,y);
     52     up(t);
     53     return t;
     54 }
     55 int _d(int x){
     56     return c[f[x]][1]==x;
     57 }
     58 void rot(int x){
     59     int y=f[x],kind=!_d(x);
     60     pushdown(x);pushdown(y);
     61     c[y][!kind]=c[x][kind];f[c[x][kind]]=y;
     62     c[f[y]][_d(y)]=x;f[x]=f[y];
     63     c[x][kind]=y;f[y]=x;
     64     up(y);up(x);
     65 }
     66 void splay(int x,int goal=0){
     67     for(int y=f[x];y!=goal;y=f[x]){
     68         if(f[y]!=goal)
     69         _d(x)==_d(y)?rot(y):rot(x);
     70         rot(x);
     71     }
     72     if(!goal)root=x;
     73 }
     74 int find(int x){
     75     int r=root;
     76     while(1){
     77         pushdown(r);
     78     if(b[c[r][0]]>=x)r=c[r][0];
     79     else{
     80         x-=b[c[r][0]]+1;
     81         if(!x)return r;
     82         r=c[r][1];
     83     }
     84     }
     85 }
     86 int pick(int x,int y){
     87     y+=2;x=find(x);y=find(y);
     88     splay(x);splay(y,x);
     89     return c[y][0];
     90 }
     91 void ch(int x,int y){
     92     int r=pick(x,y);
     93     if(_t==1)z[r]^=1;else tag[r]^=1;
     94 }
     95 void query(int x,int y){
     96     int r=pick(x,y);
     97     pushdown(r);
     98     printf("%d
    ",(-lr[r]+1)/2+(rl[r]+1)/2);
     99 }
    100 void out(int r){
    101     if(!r)return;
    102     pushdown(r);
    103     out(c[r][0]);
    104     printf("%d ",max(a[r],0));
    105     out(c[r][1]);
    106 }
    107 int main(){
    108 //    freopen("data.in","r",stdin);
    109     n=read();m=read();scanf("%s",st+2);
    110     inc(i,2,n+1)tmp[i]=st[i]=='('?1:-1;
    111     build(0,1,n+2);root=(n+3)/2;
    112 //    out(root);printf("
    ");
    113     while(m--){
    114         _t=read();_x=read();_y=read();
    115         if(!_t)query(_x,_y);
    116         else ch(_x,_y);
    117 //        out(root);printf("
    ");
    118     }
    119     return 0;
    120 }
    View Code
  • 相关阅读:
    DHT(Distributed Hash Table) Translator
    Introducing shard translator
    【转】shell脚本中echo显示内容带颜色
    javac 错误: 编码GBK的不可映射字符
    一致性哈希(consistent hashing)
    在bash shell中使用getfattr查看文件扩展属性
    css3在不同型号手机浏览器上的兼容一览表
    META是什么意思?
    JS异步加载的三种方式
    AJAX中的同步加载与异步加载
  • 原文地址:https://www.cnblogs.com/onlyRP/p/5117533.html
Copyright © 2011-2022 走看看