zoukankan      html  css  js  c++  java
  • 【UR #2】 猪猪侠再战括号序列

    SOL:

      我们用平衡树维护就好啦。

       

    // getlazy
    #include<bits/stdc++.h>
    using namespace std;
    inline int rop() {
        static int x=23333;
        return x^=x<<13,x^=x>>17,x^=x<<5;
    } 
    #define Mid (l+r>>1) 
    #define rr NULL
    struct T{
        int val,key,rev,usd,siz;
        T* son[2];
        inline T(int x) {
            key=x; val=rop(); rev=0; son[0]=son[1]=rr; 
            siz=1; usd=(x==1);
        }
        inline void up() {
            siz=1; usd=(key==1);
            if (son[0]) {siz+=son[0]->siz;usd|=son[0]->usd;}
            if (son[1]) {siz+=son[1]->siz;usd|=son[1]->usd;}
        }
        inline void down() {
            if (!rev) return;
            swap(son[0],son[1]);
            if (son[0]) son[0]->rev^=1;
            if (son[1]) son[1]->rev^=1; 
            rev=0; 
        }
    }*rt,*x,*y,*z;
    #define N 200007
    void split(T* now,int k,T* &x,T* &y) {
        if (!now)  { x=rr,y=rr; return;}
        now->down();
        int cmp=(now->son[0]?now->son[0]->siz:0)+1;
        if (k<cmp) y=now,split(y->son[0],k,x,y->son[0]);
        else x=now,split(x->son[1],k-cmp,x->son[1],y);
        now->up();
    }
    T* merge(T* x,T* y) {
        if (!x) return y; if (!y) return x;
        x->down(); y->down();
        if (x->val<y->val) {
            x->son[1]=merge(x->son[1],y);
            x->up(); return x;
        } y->son[0]=merge(x,y->son[0]);
        y->up(); return y;
    }
    int find(T* x){
        x->down();
        if (x->son[0]&&x->son[0]->usd) return find(x->son[0]);
        if (x->key>0) return (x->son[0]?x->son[0]->siz:0)+1;
        return (x->son[0]?x->son[0]->siz:0)+1+find(x->son[1]);
    }
    int n,a[N],t;
    T* build(int l,int r){
        if (l>r) return rr;
        T* x=new T(a[Mid]);
        x->son[0]=build(l,Mid-1);
        x->son[1]=build(Mid+1,r);
        x->up(); return x;
    }
    #define in(x) (x=='('||x==')')
    #define pu(x) (x=='('?1:-1)
    int sum=0,l[N],r[N],tot;
    int read(int *a) {
        int t=0;char c=getchar();
        for (;!in(c);c=getchar());
        for (;in(c);c=getchar()) a[++t]=pu(c),sum+=a[t];
        if (sum) return 0;
        return t;
    }
    void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
    inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('
    '); }
    inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
    void dfs(T* x){
        if (!x) return;
        x->down();
        if (x->son[0]) dfs(x->son[0]);
        writel(x->key);
        if (x->son[1]) dfs(x->son[1]);
    }
    signed main () {
    //    freopen("a.in","r",stdin);
        n=read(a);
        if (n==0) {writeln(-1); return 0;}
        rt=build(1,n);
    //    dfs(rt),putchar('
    ');
        for (int i=1;i<=n;i++) {
            split(rt,1,x,rt);
            if (sum+(x->key)<0) {
                rt=merge(x,rt);
                t=find(rt);
                split(rt,t,y,z);
                y->rev^=1;
                rt=merge(y,z);
    //            dfs(rt),putchar('
    ');
    //            writel(i); writeln(i+t-1);
                l[++tot]=i; r[tot]=i+t-1;
                split(rt,1,x,rt);
            }
            sum+=x->key;
        }
        writeln(tot);
        for (int i=1;i<=tot;i++)
         writel(l[i]),writeln(r[i]);
        return 0;
    }
  • 相关阅读:
    差分隐私 differential privacy privSQL ||sql query ||sql查询系统||PrivateSQL:A Differentially Private SQL Query Engine论文笔记
    分冶法解决大整数相乘 最近对问题
    数论 矩阵交集
    STl 优先队列 C++
    备份mysql函数和存储过程
    Idea 注解模板
    excel导出
    帆软常用小技巧
    js + java文件下载
    try/finally
  • 原文地址:https://www.cnblogs.com/rrsb/p/8849301.html
Copyright © 2011-2022 走看看