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

#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();
}
}
}
}

poj3580 伸展树(区间翻转 区间搬移 删除结点 加入结点 成段更新)的更多相关文章

  1. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  2. POJ 3468 线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  3. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  4. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  5. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  6. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  7. Codeforces295A - Greg and Array(线段树的成段更新)

    题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...

  8. 【线段树成段更新-模板】【HDU1698】Just a Hook

    题意 Q个操作,将l,r 的值改为w 问最后1,n的sum 为多少 成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更 ...

  9. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  10. hdu 1698 Just a Hook(线段树之 成段更新)

    Just a Hook                                                                             Time Limit: ...

随机推荐

  1. 欧拉函数 cojs 2181. 打表

    cojs 2181. 打表 ★☆   输入文件:sendtable.in   输出文件:sendtable.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 有一道比赛题 ...

  2. 边工作边刷题:70天一遍leetcode: day 84-3

    Meeting Rooms I/II 要点:这题和skyline类似,利用了interval start有序的特点,从左向右处理,用一个heap来动态表示当前占用rooms的时间段,所以heap的si ...

  3. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

  4. 利用appscan进行自动化定期安全测试

    Appscan的强大众所周知,如果可以自动执行定期安全测试,岂不是美事一件? 事实上,appscan提供了计划扫描的选项,配合windows的计划任务,可以按需设定. 1.打开appscan中的“工具 ...

  5. 【hibernate】<第一节>hibernate简单入门

    所需工具: ide:eclipse or myeclipse jdk:1.7 jar包:hibernate-distribution-3.6.0.Final 和对应mysql的驱动类(对应jar包如图 ...

  6. java 8-7 接口

    1. 接口的特点: A:接口用关键字interface表示 interface 接口名 {} B:类实现接口用implements表示 class 类名 implements 接口名 {} C:接口不 ...

  7. yslow性能优化的35条黄金守则

    参考Best Practices for Speeding Up Your Web Site Exceptional Performance 团队总结了一系列优化网站性能的方法,分成了7个大类35条, ...

  8. 【WIN10】基本控件

    先發個下載地址: http://yunpan.cn/cHuCqYzvsWFAL  访问密码 3470 說明一下.這個示例只是最簡單的演示,並不能提供太大的實用價值. 後面會介紹 Bing & ...

  9. scrapy 的 selector 练习

    网页结构: <html> <head> <base href='http://example.com/' /> <title>Example websi ...

  10. 【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布

    去年就知道有这个功能,不过没去深究总结过,最近有写网络博客的欲望了,于是又重新拾起这玩意儿. 具体到底是用Windows Live Writer 2012还是用Word 2013,个人觉得看个人,因为 ...