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. Nmap备忘单:从探索到漏洞利用(Part 4)

    这是我们的Nmap备忘单的第四部分(Part 1. Part 2. Part 3).本文中我们将讨论更多东西关于扫描防火墙,IDS / IPS 逃逸,Web服务器渗透测试等.在此之前,我们应该了解一下 ...

  2. RPM常用组合【转载】

    RPM常用组合 -ivh:安装显示安装进度--install--verbose--hash -Uvh:升级软件包--Update: -qpl:列出RPM软件包内的文件信息[Query Package ...

  3. CC MayClg 15 T3

    www.codechef.com/MAY15/problems/CHAPD 一道比较神奇的题目... 看到题目后自己yy出了个傻逼算法...然后对拍都是对的...提交都是错的...然后一看" ...

  4. 做网站用UTF-8还是GB2312 & 各国语言对应字符集

    经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, WordPress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...

  5. 无论IT代码系统还是人生都是有惯性的

    是的,这和IT系统和代码没有什么关系:鸡汤式的文章,看烦了的就关掉吧,想看的请听我碎碎念.惯性本是物理学研究的问题,这里没有要研究物理学里的惯性.惯性无时无刻地发生在我们的日常生活中,只是你我都没有察 ...

  6. HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. DP:Sumsets(POJ 2229)

     数的集合问题 题目大意:给定你一个整数m,你只能用2的k次幂来组合这个数,问你有多少种组合方式? 这一题一看,天啦太简单了,完全背包?是不是? 不过的确这一题可以用完全背包来想,但是交题绝对是TLE ...

  8. codeforces B. Flag Day 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/B 题目意思:输入n个人和m场舞蹈,给出每场舞蹈(只有3个人参与)中参与的舞者的编号,你需要为这些舞 ...

  9. VelocityTracker简介

    android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率.用addMovement(MotionEvent)函数将Mot ...

  10. sublime text 3 使用过程总结记录

    自定义的设置: "save_on_focus_lost": true //在文件失去焦点的时候自动保存