poj3580 伸展树(区间翻转 区间搬移 删除结点 加入结点 成段更新)

  好题。我做了很久,学了大牛们的区间搬移。主要的代码都有注释。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 999999999
#define key_value ch[ch[root][1]][0]
using namespace std;
const int MAXN = ;
int pre[MAXN],lazy[MAXN],siz[MAXN],ch[MAXN][],s[MAXN],key[MAXN],tot1,tot2,root,ans[MAXN];
int n,a[MAXN],rev[MAXN];//rev表示旋转 lazy标记增加的量
/****************************/
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d lazy=%2d rev=%2d ans=%2d\n",x,ch[x][],ch[x][],pre[x],siz[x],key[x],lazy[x],rev[x],ans[x]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
/****************************/
void Newnode(int &rt,int pa,int k)
{
if(tot2)
rt = s[--tot2];
else
rt = ++tot1;
pre[rt] = pa;
key[rt] = k;
lazy[rt] = ;
siz[rt] = ;
rev[rt] = ;
ans[rt] = k;
ch[rt][] = ch[rt][] = ;
}
void pushup(int rt)
{
siz[rt] = siz[ch[rt][]] + siz[ch[rt][]] + ;
ans[rt] = key[rt];
if(ch[rt][]) //因为是min
ans[rt] = min(ans[rt],ans[ch[rt][]]);
if(ch[rt][])
ans[rt] = min(ans[rt],ans[ch[rt][]]);
}
void pushdown(int rt)
{
if(rev[rt]){
rev[ch[rt][]] ^= ;
rev[ch[rt][]] ^= ;
swap(ch[rt][],ch[rt][]);
rev[rt] = ;
}
if(lazy[rt]){
lazy[ch[rt][]] += lazy[rt];
lazy[ch[rt][]] += lazy[rt];
key[ch[rt][]] += lazy[rt];
key[ch[rt][]] += lazy[rt];
ans[ch[rt][]] += lazy[rt];
ans[ch[rt][]] += lazy[rt];
lazy[rt] = ;
}
}
void build(int &rt,int l,int r,int pa)
{
if(l > r)
return ;
int m = (l+r)/;
Newnode(rt,pa,a[m]);
build(ch[rt][],l,m-,rt);
build(ch[rt][],m+,r,rt);
pushup(rt);
}
void Init()
{
tot1 = tot2 = root = ;
siz[root] = pre[root] = lazy[root] = key[root] = rev[root] = ;
ans[root] = INF;
ch[root][] = ch[root][] = ;
Newnode(root,,-);
Newnode(ch[root][],root,-);
build(key_value,,n,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
//先pushdown将lazy压下去
void Rotate(int rt,int kind)
{
pushdown(pre[rt]);
pushdown(rt);
int y = pre[rt];
ch[y][!kind] = ch[rt][kind];
pre[ch[rt][kind]] = y;
if(pre[y]){
ch[pre[y]][ch[pre[y]][]==y] = rt;
}
pre[rt] = pre[y];
ch[rt][kind] = y;
pre[y] = rt;
pushup(y);
pushup(rt);
}
//先pushdown将lazy压下去
void splay(int rt,int goal)
{
pushdown(rt);
while(pre[rt] != goal)
{
if(pre[pre[rt]] == goal){
pushdown(pre[rt]);
pushdown(rt);
Rotate(rt,ch[pre[rt]][]==rt);
}
else {
pushdown(pre[pre[rt]]);
pushdown(pre[rt]);
pushdown(rt);
int y = pre[rt];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == rt){
Rotate(rt,!kind);
Rotate(rt,kind);
}
else {
Rotate(y,kind);
Rotate(rt,kind);
}
}
}
pushup(rt);
if(goal == )
root = rt; }
int Get_kth(int rt,int k)
{
pushdown(rt);
int t = siz[ch[rt][]] + ;
if(t == k)
return rt;
else if(t > k){
return Get_kth(ch[rt][],k);
}
else {
return Get_kth(ch[rt][],k-t);
}
pushup(rt);
}
void add(int x,int y,int z)
{
splay(Get_kth(root,x),);
splay(Get_kth(root,y+),root);
lazy[key_value] += z;
key[key_value] += z;
ans[key_value] += z;
pushup(ch[root][]);
pushup(root);//
}
int Get_min(int rt)
{
pushdown(rt);
while(ch[rt][]){
rt = ch[rt][];
pushdown(rt);
}
return rt;
}
int Get_max(int rt)
{
pushdown(rt);
while(ch[rt][]){
rt = ch[rt][];
pushdown(rt);
}
return rt;
}
int query(int x,int y)
{
splay(Get_kth(root,x),);
splay(Get_kth(root,y+),root);
return ans[key_value];
}
void Del(int x)
{
splay(Get_kth(root,x),);
splay(Get_kth(root,x+),root);
pre[key_value] = ;
key_value = ;
pushup(ch[root][]);
pushup(root);
}
void Insert(int x,int y)
{
splay(Get_kth(root,x+),);
splay(Get_kth(root,x+),root);
Newnode(key_value,ch[root][],y);
pushup(ch[root][]);
pushup(root);
}
void Reverse(int x,int y)
{
splay(Get_kth(root,x),);
splay(Get_kth(root,y+),root);
rev[key_value] ^= ;
pushup(ch[root][]);
pushup(root);
}
void Revolve(int l,int r,int t)
{
//区间平移其实就是把区间[l,c-1],[c,r]中的[c,r]放到[l,c-1]前
int len = r - l + ;//取mod
t = (t%len + len)%len;
if(!t)return ;
int c = r - t + ;
splay(Get_kth(root,c),);
splay(Get_kth(root,r+),root);
int tmp = key_value;//这里的是[c,r]的区间
key_value = ;
pushup(ch[root][]);
pushup(root);//注意这两步 这里把key_value取0 这样主要为了取走[c,r],所以要更新
splay(Get_kth(root,l),);
splay(Get_kth(root,l+),root);
key_value = tmp;
pre[key_value] = ch[root][];
pushup(ch[root][]);
pushup(root);
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
for(i=; i<=n; i++){
scanf("%d",&a[i]);
}
Init();
int q;
scanf("%d",&q);
char work[];
//debug();
//cout<<"test: "<<endl;
while(q--)
{
scanf("%s",work);
if(work[] == 'A'){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
//debug();
}
else if(work[] == 'M'){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",query(x,y));
}
else if(work[] == 'D'){
int x;
scanf("%d",&x);
Del(x);
//debug();
}
else if(work[] == 'I'){
int x,y;
scanf("%d%d",&x,&y);
Insert(x,y);
//debug();
}
else if(strcmp(work,"REVERSE") == ){
int x,y;
scanf("%d%d",&x,&y);
Reverse(x,y);
//debug();
}
else if(strcmp(work,"REVOLVE") == ){
int x,y,ft;
scanf("%d%d%d",&x,&y,&ft);
Revolve(x,y,ft);
//debug();
}
}
}
}
上一篇:vbs下载者


下一篇:使用C# 和Consul进行分布式系统协调