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

#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. pace.js – 加载进度条插件

    这儿只是简单介绍一下这个插件pace.js. 在页面中引入Pace.js,页面就会自动监测你的请求(包括Ajax请求),在事件循环滞后,会在页面记录加载的状态以及进度情况.此插件的兼容性很好,可以兼容 ...

  2. css 字体不撑开默认块级元素问题

    问题原因是行高的元素没有随字体大小而改变,设置line-hight属性和字体同时变换

  3. aop介绍

  4. f2fs解析(六)

    f2fs中有对一个bitmap进行操作的函数,感觉很巧妙,和大家分享一下: 1333 static inline void f2fs_change_bit(unsigned int nr, char ...

  5. django字段设置null和blank的区别

    null 这个选项跟数据库有关. null=True的话,数据库中该字段是NULL,即允许空值:null=False(默认)的话,数据库中该字段是NOT NULL,即不允许空值. blank 这个选项 ...

  6. Week2学习过程报告

    一.学习内容 1. 熟悉Linux系统下的开发环境   2. 熟悉vi的基本操作   3. 熟悉gcc编译器的基本原理   4. 熟练使用gcc编译器的常用选项   5 .熟练使用gdb调试技术    ...

  7. 20145222黄亚奇《Java程序设计》第2周学习总结

    教材学习内容总结 类型: 整数:short(占2字节).int(4).long(8). 浮点数:float(4).double(8) 字符:char(2) 布尔:boolean类型表示true与fal ...

  8. KMP算法的Next数组详解

    转载请注明来源,并包含相关链接. 网上有很多讲解KMP算法的博客,我就不浪费时间再写一份了.直接推荐一个当初我入门时看的博客吧:http://www.cnblogs.com/yjiyjige/p/32 ...

  9. 从日常开发说起,浅谈HTTP协议是做什么的。

    引言 HTTP协议作为Web开发的基础一直被大多数人所熟知,不过相信有很多人只知其一不知其二.比如咱们经常用到的session会话机制是如何实现的,可能很多人都说不出来吧.其实session会话就是H ...

  10. Bootstrap系列 -- 38. 基础导航条

    在制作一个基础导航条时,主要分以下几步: 第一步:首先在制作导航的列表(<ul class=”nav”>)基础上添加类名“navbar-nav” 第二步:在列表外部添加一个容器(div), ...