zoukankan      html  css  js  c++  java
  • 【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)

    http://uoj.ac/problem/31

    纪念伟大的没有调出来的splay。。。

    竟然那个find那里写错了!!!!!!!!!!!!!

    以后要记住:一定要好好想过!

    (正解的话我就不写了,太简单了。。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    typedef long long ll;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << (#x) << " = " << (x) << endl
    #define error(x) (!(x)?puts("error"):0)
    #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    
    const int N=200005;
    struct node *null;
    struct node {
    	node *c[2], *f;
    	int s, k, sum;
    	bool rev;
    	node(int _k=0) { c[0]=c[1]=f=null; s=1; k=sum=_k; rev=0; }
    	void pushup() { s=1+c[0]->s+c[1]->s; sum=k+c[0]->sum+c[1]->sum; }
    	void setc(node *x, bool d) { c[d]=x; x->f=this; }
    	bool d() { return f->c[1]==this; }
    	void upd() {
    		if(this==null) return;
    		rev=!rev;
    		swap(c[0], c[1]);
    	}
    	void pushdown() {
    		if(rev) {
    			rev=0;
    			c[0]->upd();
    			c[1]->upd();
    		}
    	}
    }*root;
    void rot(node *x) {
    	node *f=x->f;
    	f->pushdown(); x->pushdown(); bool d=x->d();
    	f->f->setc(x, f->d());
    	f->setc(x->c[!d], d);
    	x->setc(f, !d);
    	f->pushup();
    	if(f==root) root=x;
    }
    void splay(node *x, node *f=null) {
    	x->pushdown();
    	while(x->f!=f) {
    		if(x->f->f==f) rot(x);
    		else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
    	}
    	x->pushup();
    }
    void Pr(node *x) {
    	if(x==null) return;
    	Pr(x->c[0]);
    	printf("%c", x->k?'(':')');
    	Pr(x->c[1]);
    }
    void P(node *x=root) {
    	Pr(x); puts("");
    }
    void init() { null=new node; null->s=0; null->c[0]=null->c[1]=null->f=null; root=null; }
    void build(char *s, int l, int r, node *&x) {
    	if(l>r) return;
    	int mid=(l+r)>>1;
    	x=new node(s[mid]=='(');
    	if(l==r) return;
    	build(s, l, mid-1, x->c[0]);
    	build(s, mid+1, r, x->c[1]);
    	if(l<=mid-1) x->c[0]->f=x;
    	if(mid+1<=r) x->c[1]->f=x;
    	x->pushup();
    }
    void build(char *s) {
    	int len=strlen(s);
    	node *x=null;
    	root=new node();
    	x=new node();
    	root->setc(x, 1);
    	x=new node();
    	build(s-1, 1, len, x);
    	root->c[1]->setc(x, 0);
    	root->c[1]->pushup();
    	root->pushup();
    }
    node* find(node *x) {
    	if(x==null) return null;
    	x->pushdown();
    	if(x->c[0]->sum==0 && x->k) return x; //这里啊啊啊
    	if(x->c[0]->sum) return find(x->c[0]);
    	return find(x->c[1]);
    }
    node* sel(node *x, int k) {
    	if(x==null) return null;
    	x->pushdown();
    	int s=x->c[0]->s;
    	if(s==k) return x;
    	if(s<k) return sel(x->c[1], k-s-1);
    	else return sel(x->c[0], k);
    }
    node* getrange(int l, int r) {
    	splay(sel(root, l-1));
    	splay(sel(root, r+1), root);
    	return root->c[1]->c[0];
    }
    int getpos(int l, int r) {
    	node *x=getrange(l, r);
    	x=find(x); //dbg(x->k);
    	splay(x);
    	return x->c[0]->s;
    }
    void fix(int l, int r) {
    	node *x=getrange(l, r); //P(x);
    	x->upd();
    	root->c[1]->pushup();
    	root->pushup();
    }
    int getinf(int l) {
    	node *r=getrange(l, l);
    	return r->k;
    }
    char s[N];
    int ans;
    int x[N], y[N], n, len;
    // int ifind(int now) {
    // 	int ret=now;
    // 	while(ret<=len) if(s[ret]=='(') return ret; else ++ret;
    // 	return 0;
    // }
    // void fix(int l, int r) {
    // 	while(l<r) swap(s[l], s[r]), ++l, --r;
    // }
    int main() {
    	scanf("%s", s+1);
    	init();
    	len=strlen(s+1);
    	//dbg(s+1);
    	n=len>>1;
    	build(s+1);
    	//dbg(getpos(2, len));
    	//fix(1, getpos(2, len));
    	//node *xx=sel(root, 1); dbg(xx->s); P();
    	for1(i, 1, n) {
    		//printf("%c
    ", getinf(i)?'(':')');
    		if(getinf(i)!=1) {
    			++ans;
    			int pos=getpos(i+1, len);
    			if(!pos) { puts("error"); return 0; }
    			x[ans]=i;
    			y[ans]=pos;
    			fix(i, pos);
    			//dbg(s+1);
    		}
    		//P();
    	}
    	printf("%d
    ", ans);
    	for1(i, 1, ans) {
    		printf("%d %d
    ", x[i], y[i]);
    	}
    	return 0;
    }
    

      


  • 相关阅读:
    (二十九)动态单元格
    (二十八)QQ好友列表的展开收缩
    (二十七)QQ好友列表的实现
    (二十六)静态单元格(Cell)
    (二十五)键盘的设置与TextField细节处理
    poj 1734 Sightseeing trip
    BZOJ 2200: [Usaco2011 Jan]道路和航线
    LUOGU P1073 最优贸易
    poj 3662 Telephone Lines
    poj 3539 Elevator
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/4148959.html
Copyright © 2011-2022 走看看