HDU3487 Play with Chain

SPLAY 翻转,删除段,添加段操作

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 using namespace std;
  5 const int maxn = 300005;
  6 int fa[maxn],son[2][maxn],val[maxn],siz[maxn],lz[maxn];
  7 int root,tot;
  8 void init(){
  9     memset(fa,0,sizeof(fa));
 10     memset(son,0,sizeof(son));
 11     memset(val,0,sizeof(val));
 12     memset(siz,0,sizeof(siz));
 13     memset(lz,0,sizeof(lz));
 14     root = tot = 0;
 15 }
 16 void Ins(int x){
 17     if(root==0){
 18         root = ++tot;
 19         siz[root] = 1;
 20         return;
 21     }
 22     fa[root] = ++tot;
 23     son[0][tot] = root;
 24     siz[tot] = siz[root]+1;
 25     root = tot;
 26 }
 27 void pushdown(int x){
 28     int l = son[0][x],r = son[1][x];
 29     if(lz[x]){
 30         swap(son[0][x],son[1][x]);
 31         lz[l]^=1;lz[r]^=1;
 32         lz[x] = 0;
 33     }
 34 }
 35 void pushup(int x){
 36     siz[x] = siz[son[0][x]]+siz[son[1][x]]+1;
 37 }
 38 void rota(int w,int x){
 39     int y = fa[x];
 40     son[!w][y] = son[w][x];
 41     if(son[w][x])fa[son[w][x]] = y;
 42     fa[x] = fa[y];
 43     if(fa[y])son[y==son[1][fa[y]]][fa[y]] = x;
 44     fa[y] = x;
 45     son[w][x] = y;
 46     pushup(y);pushup(x);
 47 }
 48 void splay(int x,int y){
 49     while(fa[x]!=y){
 50         if(fa[fa[x]]==y)rota(x==son[0][fa[x]],x);
 51         else {
 52             int w = fa[x]==son[0][fa[fa[x]]];
 53             if(x==son[w][fa[x]]){
 54                 rota(!w,x);rota(w,x);
 55             }
 56             else {
 57                 rota(w,fa[x]);rota(w,x);
 58             }
 59         }
 60     }
 61     if(y==0)root = x;///HERE ,x!!!!  !y
 62 }
 63 int getk(int k){
 64     int x = root;
 65     while(1){
 66         pushdown(x);
 67         if(k==siz[son[0][x]]+1)return x;
 68         if(k<=siz[son[0][x]])x = son[0][x];
 69         else {
 70             k-=(siz[son[0][x]]+1);
 71             x = son[1][x];
 72         }
 73     }
 74 }
 75 int Del(int l,int r){
 76     int x = getk(l-1),y = getk(r+1);
 77     splay(x,0);splay(y,x);
 78     int t = son[0][y];
 79     son[0][y] = 0;
 80     return t;
 81 }
 82 void rev(int l,int r){
 83     int x = getk(l-1),y = getk(r+1);
 84     splay(x,0);splay(y,x);
 85     lz[son[0][y]]^=1;
 86 }
 87 int main()
 88 {
 89     int n,m;
 90     while(~scanf("%d%d",&n,&m)){
 91         if(n+m<0)break;
 92         init();
 93         for(int i = 2;i<=n+1;++i)val[i] = i-1;
 94         for(int i = 1;i<=n+2;i++)Ins(i);
 95         while(m--){
 96             char s[10];int a,b,c;
 97             scanf("%s%d%d",s,&a,&b);
 98             if(s[0]==‘C‘){
 99                 scanf("%d",&c);
100                 int t = Del(a+1,b+1);
101                 int x = getk(c+1),y = getk(c+2);
102                 splay(x,0);splay(y,x);
103                 son[0][y] = t;
104                 fa[t] = y;
105             }
106             else rev(a+1,b+1);
107         }
108         for(int i = 1;i<n;++i)printf("%d ",val[getk(i+1)]);
109         printf("%d\n",val[getk(n+1)]);
110     }
111     return 0;
112 }
时间: 2024-10-11 10:25:54

HDU3487 Play with Chain的相关文章

HDU3487 Play With Chain [Splay]

题目传送门 题目描述 Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.He will perform two types of operation

【HDU3487】【splay分裂合并】Play with Chain

Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.He will perform two types of operations:CUT a b c

设计模式之Chain of Responsibility(职责链)(转)

Chain of Responsibility定义 Chain of Responsibility(CoR) 是用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合,唯一共同点是在他们之间传递request. 也就是说,来了一个请求,A类先处理,如果没有处理,就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条(chain)一样传递下去. 如何使用? 虽然这一段是如何使用CoR,但是也是演示什么是CoR. 有一个Handler接口: public

hdu 5293 Tree chain problem(树链剖分+树形dp)

题目链接:hdu 5293 Tree chain problem 维护dp[u], sum[u],dp[u]表示以u为根节点的子树的最优值.sum[u]表示以u节点的所有子节点的dp[v]之和.对于边a,b,w,在LCA(a,b)节点的时候进行考虑.dp[u] = min{dp[u], Sum(a,b) - Dp(a,b) + sum[u] | (ab链上的点,不包括u } #pragma comment(linker, "/STACK:1024000000,1024000000")

HDU 3980 Paint Chain(博弈 SG)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3980 Problem Description Aekdycoin and abcdxyzk are playing a game. They get a circle chain with some beads. Initially none of the beads is painted. They take turns to paint the chain. In Each turn one p

- Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead. 解决方案

<template> <div>{{hello}}</div> <button @click="addOne">add one</button> <button @click="minusOne">minus one</button> </template> 在*.vue组件里有这么一段. 报错信息: (Emitted value instead of an instan

ios中事件的响应链(Responder chain)和传递链

事件的响应链涉及到的一些概念 UIResponder类,是UIKIT中一个用于处理事件响应的基类.窗又上的所有事件触发,都由该类响应(即事件处理入又).所以,窗又上的View及控制器都是 派生于该类的,例如UIView.UIViewController等. 调用UIResponder类提供的方法或属性,我们就可以捕捉到窗又上的所有响应 事件,并进行处理. 响应者链条是由多个响应者对象连接起来的链条,其中响应者对象是能处理事 件的对象,所有的View和ViewController都是响应者对象,利

docker0: iptables: No chain/target/match by that name错误处理

今天运行这个命令时报错 docker run -it --name Haproxy --link app1:app1 --link app2:app2 -p 6302:6301 -v ~/Projects/HAProxy:/tmp haproxy /bin/bash 报错信息: docker: Error response from daemon: failed to create endpoint Haproxy on network bridge: iptables failed: ipta

struts2 中chain、redirect、redirect-action的区别

struts2 中chain.redirect.redirectaction的区别 文章摘要: 一.Chain Result:这个result调用另外的一个action,连接自己的拦截器栈和result. ?actionName (默认) - 被调用的action的名字?namespace - 被调用的action的名称空间. 如果名称空间为空,这默认为当前名称空间?method - 用于指定目标action的另一个方法被调用. 如果空,默认为excute方法Redirect Action Re