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\n", 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\n", ans);
for1(i, 1, ans) {
printf("%d %d\n", x[i], y[i]);
}
return 0;
}

  


【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)的更多相关文章

  1. uoj problem 31 猪猪侠再战括号序列

    题目大意: 给定一个长度为2n的括号序列.定义一个关于区间[l,r]的翻转操作为位置平移对调. 即翻转")))()("可以得到"()()))((" 用不超过n次 ...

  2. uoj #31. 【UR #2】猪猪侠再战括号序列 贪心

    #31. [UR #2]猪猪侠再战括号序列 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/31 Descript ...

  3. 学长小清新题表之UOJ 31.猪猪侠再战括号序列

    学长小清新题表之UOJ 31.猪猪侠再战括号序列 题目描述 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫\(\_CallMeGGBond\). 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领 ...

  4. UOJ#31 【UR #2】猪猪侠再战括号序列

    传送门http://uoj.ac/problem/31 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫_CallMeGGBond. 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领域的兴趣:尤其 ...

  5. 【UR #2】猪猪侠再战括号序列

    UOJ小清新题表 题目摘要 UOJ链接 有一个由 \(n\) 个左括号 "(" 和 \(n\) 个右括号 ")" 组成的序列.每次操作时可以选定两个数 \(l, ...

  6. 【UR #2】猪猪侠再战括号序列 题解

    题目链接 前言 是的没脑子选手只会做签到题. 思路分析 一开始把题目看成反转括号的状态,直接浪费 \(40\ mins\) . 我们考虑把不确定的"正确括号"转换成一个固定的括号序 ...

  7. 【BZOJ-2329&2209】括号修复&括号序列 Splay

    2329: [HNOI2011]括号修复 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1007  Solved: 476[Submit][Statu ...

  8. bzoj 2209: [Jsoi2011]括号序列 splay

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 833  Solved: 392[Submit][Status ...

  9. BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1111  Solved: 541[Submit][Statu ...

随机推荐

  1. Linux下列格式化工具 - column

    [root@localhost ~]# mount/dev/sda2 on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type ...

  2. 工作中常用shell之ssh登陆不用输入"yes"

    ip="192.168.5.166"ssh $ip -o StrictHostKeyChecking=no           //ssh登陆不用输入"yes" ...

  3. 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续

    目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...

  4. local variable 'xxx' referenced before assignment

    这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...

  5. 如何让你的scrapy爬虫不再被ban

    前面用scrapy编写爬虫抓取了自己博客的内容并保存成json格式的数据(scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据)和写入数据库(scrapy爬虫成长日记之将抓取内容写入 ...

  6. Android drawable的自动缩放

    今天在写程序时发现,一张图片被自动放大了,后来发现,这张图片放在了drawable-zh文件夹下,这个文件夹没有指定屏幕密度!于是将drawable-zh改为drawable-zh-nodpi,问题解 ...

  7. MFC 颜色选择对话框、颜色按钮

    COLORREF color=RGB(0,255,0); unsigned char r=GetRValue(color); unsigned char g=GetGValue(color); uns ...

  8. 【2016-07-11】Qt远程部署失败,提示"没有那个文件或目录"的解决方法

    首先明确一下,这里的部署失败与网络连接.ssh/scp/sftp等无关. 一般出现在删除了远端上的可执行文件,而本地程序未做明显改动时远程部署执行的时候. Qt应用程序输出中的提示信息如下: 究其原因 ...

  9. mongoose学习笔记1--基础知识2

    Schema简述 Schema —— 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力,仅仅只是数据库模型在程序片段中的一种表现,可以说是数据属性模型(传统 ...

  10. Fedora 启动sshd服务:

    .先确认是否已安装ssh服务: [root@localhost ~]# rpm -qa | grep openssh-server openssh-server-.3p1-.fc12.i686 (这行 ...