Description

 

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

Output

输出一行n个数字,表示原始序列经过m次变换后的结果

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

Source

splay区间操作大裸题不解释。

 #include<queue>
#include<cstdio>
#include<cstdlib>
using namespace std; #define maxn 100010
const int inf = << ;
queue <int> team;
struct TREE
{
int ch[maxn][],fa[maxn],cnt,root;
int key[maxn],size[maxn],t[maxn]; inline int newnode()
{
if (team.empty()) return ++cnt;
int ret = team.front(); team.pop();
return ret;
} inline TREE()
{
root = newnode();
key[root] = -inf; ++t[root];
updata(root);
add(inf);
} inline int find(int rank,int have,int now)
{
if (have+size[ch[now][]]<rank&&have+size[ch[now][]]+t[now]>=rank) return now;
if (have+size[ch[now][]]+t[now]<rank) return find(rank,have+size[ch[now][]]+t[now],ch[now][]);
else return find(rank,have,ch[now][]);
} inline void updata(int now) { size[now] = size[ch[now][]] + size[ch[now][]] + t[now]; } inline void rotate(int x)
{
int y = fa[x],z = fa[y],l = ch[y][] != x,r = l ^ ;
if (z != ) ch[z][ch[z][] != y] = x;
fa[x] = z; fa[y] = x; fa[ch[x][r]] = y;
ch[y][l] = ch[x][r]; ch[x][r] = y;
updata(y); updata(x);
} inline void splay(int x,int aim)
{
int p = fa[aim];
while (fa[x] != p)
{
int y = fa[x],z = fa[y];
if (z != p)
{
if ((ch[z][] == y)^(ch[y][] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
if (aim == root) root = x;
} inline int search(int x)
{
int now = root;
while (now)
{
if (key[now] == x) break;
now = ch[now][x > key[now]];
}
return now;
} inline void add(int x)
{
int now = root,pre = ;
while (now)
{
pre = now;
now = ch[now][x > key[now]];
}
now = newnode();
fa[now] = pre; ch[pre][x > key[pre]] = now;
key[now] = x; ++t[now];
pre = now;
while (now)
{
updata(now);
now = fa[now];
}
splay(pre,root);
} inline int qrank(int x)
{
int now = root,ret = ;
while (key[now] != x)
{
if (x < key[now]) now = ch[now][];
else ret += size[ch[now][]] + t[now],now = ch[now][];
}
return ret + size[ch[now][]] + ;
} inline void insert(int x)
{
int p = search(x);
if (p)
{
splay(p,root);
++t[p];
updata(p);
}
else add(x);
} inline void del(int x)
{
int now = search(x),k = qrank(x);
int p = find(k-,,root),q = find(k + t[now],,root);
splay(p,root);
splay(q,ch[p][]);
if (--t[now])
{
updata(now);
updata(q);
updata(p);
}
else
{
ch[q][] = ; fa[now] = ;
updata(q);
updata(p);
team.push(now);
}
} inline int ask(int x,int sign)
{
int now = root,ret;
while (now)
{
if (sign)
{
if (key[now] > x)
ret = now,now = ch[now][];
else now = ch[now][];
}
else
{
if (key[now] < x)
ret = now,now = ch[now][];
else now = ch[now][];
}
}
return key[ret];
}
}tree; inline int read()
{
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main()
{
freopen("3224.in","r",stdin);
freopen("3224.out","w",stdout);
int T = read();
while (T--)
{
int opt = read();
if (opt == ) tree.insert(read());
else if (opt == ) tree.del(read());
else if (opt == ) printf("%d\n",tree.qrank(read())-);
else if (opt == ) printf("%d\n",tree.key[tree.find(read()+,,tree.root)]);
else if (opt == ) printf("%d\n",tree.ask(read(),));
else printf("%d\n",tree.ask(read(),));
}
fclose(stdin); fclose(stdout);
return ;
}

BZOJ 3223 文艺平衡树的更多相关文章

  1. [题解]bzoj 3223 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3884  Solved: 2235[Submit][Sta ...

  2. bzoj 3223 文艺平衡树 - Splay

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3884  Solved: 2235[Submit][Sta ...

  3. BZOJ 3223 文艺平衡树 [codevs3303翻转区间]

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...

  4. bzoj 3223 文艺平衡树 splay 区间翻转

    Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17715  Solved: 7769[Submit][Status][ ...

  5. bzoj 3223 文艺平衡树 Splay 打标志

    是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...

  6. [bzoj3224]普通平衡树/3223文艺平衡树

    这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...

  7. 3223. 文艺平衡树【平衡树-splay】

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  8. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

随机推荐

  1. 为什么Android没有iOS那么顺滑

    虽然很多Android手机的配置都比iPhone要高,比如大多数Andorid手机的内存都有1GB,而iPhone 4S只有512MB内存,但用过iPhone的人都知道Android手机在使用的时候总 ...

  2. Imatest 崩溃

    在使用Imatest时候,发现选取chart图的区域时候.Imatest停止工作,例如以下图.百度半天没有找到类似的问题,事实上这个问题在之前的公司也碰到过一回,卸载重N次,无效.如今又碰到了,问之前 ...

  3. 写一个函数,参数为$n,生成一个数组,其元素为1~$n,各元素位置随机排列,不得重复

    function rand_array($n){ $array=range(1,$n); shuffle($array); return $array; }

  4. 嵌入式Linux系统Bootloader启动调试技术(回想)

    嵌入式系统搭建过程中,对于系统平台搭建project师最初的一步一般是移植Bootloader ,当然移植有几个级别,通常最常见的是參考的EVM 的硬件有了改动(如更改了FLASH ,更改了SDRAM ...

  5. oracle:变长数组varray,嵌套表,集合

    创建变长数组类型 ) );  这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...

  6. atoi、stoi、strtoi区别

    首先atoi和strtol都是c里面的函数,他们都可以将字符串转为int,它们的参数都是const char*,因此在用string时,必须调c_str()方法将其转为char*的字符串.或者atof ...

  7. react初识

    如下是在研究中记录的笔记: 1,作用:局部的更新dom结构;虚拟dom保证性能2,和mvc不同,mvc是对于技术上的分离(分类),而react是组件上的分离,每个视图模块分离,复用,以视图模块为单位3 ...

  8. 美好头标ToolBar

    ActionBar我相信是每一位合格的程序员都用过的组件,也是每一个程序员都会抱怨的组件,因为他不能实现复杂的自定义.为此Google推出了比ActionBar更为美好的组件ToolBar. 本文重点 ...

  9. decimal to hexadecimal,binary and octonary.

    Here is a simple algorithm about 'decimal' to 'dexadecimal',and the implementation code: /* Convert ...

  10. 灵活运用绑定变量---declare匿名块使用绑定变量

    declare        type cur01 is ref cursor;     v_cur cur01;        v_match123 varchar2(2000);        v ...