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

#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. 如何用ZBrush雕刻出栩栩如生的头发(二)

     之前的ZBrush教程教大家使用SubTool为模型添加了杂乱头发效果的两种雕刻手法,今天将继续介绍其他方法对模型头发雕刻技巧和细节进行讲解.文章内容仅以fisker老师讲述为例,您也可以按照自己的 ...

  2. AC日记——逃跑的拉尔夫 codevs 1026 (搜索)

    1026 逃跑的拉尔夫  时间限制: 1 s    空间限制: 128000 KB    题目等级 : 黄金 Gold 题解       题目描述 Description   年轻的拉尔夫开玩笑地从一 ...

  3. 程序清单 8-8 exec函数实例,a.out是程序8-9产生的可执行程序

    /* ============================================================================ Name : test.c Author ...

  4. C++中的运算符重载

    首先思考以下几个问题: 1.什么是运算符重载? 2.为什么要重载运算符,它有什么用? 3.可以重载哪些运算符? 4.重载运算符有哪些规则? 一.基本概念 我们在程序中使用各种操作符,比如加(+).赋值 ...

  5. 使用EXISTS语句注意点

    1.使用EXISTS语句,其目标列一般用“*”,因为带EXISTS的子查询只返回真值或假值,给出列名无实际意义. 2.使用EXISTS语句一定要注意上下两个表之间要建立联系. 例如,查询所有选修了1号 ...

  6. 判断一个值是否在数组里,可以检测数字,字符串,json对象

    Array.prototype.indexOf = function (val) {//判断数组是否存在某个值,如果存在返回该值对应的索引,否则返回-1 for (var i = 0; i < ...

  7. Netty5-应答服务器

    需求: 服务端:接收客户端请求,返回当前系统时间 客户端:发起时间请求 服务端 package org.zln.netty.five.timer; import io.netty.bootstrap. ...

  8. 在PLSQL中不能使用中文作为查询条件查询数据

    解决方法:  1.在oracle服务端的注册表中找到oracle-->key_oradb11g_home1,在右侧找到NLS_LANG,将其数值数据改为SIMPLIFIED CHINESE_CH ...

  9. 转载:有关SQL server connection Keep Alive 的FAQ(2)

    转: http://blogs.msdn.com/b/apgcdsd/archive/2012/05/18/sql-server-connection-keep-alive-faq-2.aspx 在下 ...

  10. C语言 百炼成钢11

    //题目31:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 //判断第二个字母. #define _CRT_SECURE_NO_WARNINGS #include<st ...