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;
}

  


最新文章

  1. Asp.net Core准备工作
  2. supermap iobect .net 7.1.2 图例的拆分
  3. 微软图表控件MsChart使用
  4. MyBatis3: There is no getter for property named &#39;code&#39; in &#39;class java.lang.String&#39;
  5. 2016 - 1 -17 GCD主队列与全局队列
  6. (六)6.14 Neurons Networks Restricted Boltzmann Machines
  7. SVN中检出(check out) 和 导出(export) 的区别
  8. chkconfig用法
  9. 17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 获取复制Master Binary Log的坐标:
  10. [LeetCode] Decode Ways [33]
  11. 【Android Developers Training】 37. 共享一个文件
  12. P2649 - 【NOIP2017】列队
  13. mapState
  14. 阿里云已买到域名价格统计js代码
  15. C#中委托的同步和异步有什么区别
  16. 2018牛客网暑假ACM多校训练赛(第六场)I Team Rocket 线段树
  17. python第六十六天--sqlalchemy
  18. 月球美容计划之最小生成树(MST)
  19. 【bzoj5004】开锁魔法II 组合数学+概率dp
  20. TF-IDF及其算法

热门文章

  1. 关于DCMTK3.6.0源代码编译的总结
  2. Linux rsync 命令详解
  3. 【ERROR】使用jquery的ajax出现error:readyState=4,status=500
  4. linux ls正则表达式
  5. JavaScript Math 对象方法
  6. asp.net文本编辑器FCKeditor使用方法详解
  7. Dom lesson1
  8. iOS 中的字体预览
  9. Lubuntu下配置Python开发环境
  10. git merge和个git rebase的区别