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

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

最新文章

  1. javascript的几种继承
  2. Java找不到或无法加载主类
  3. Tomcat的SSL证书配置以及Tomcat+Nginx实现SSL配置
  4. 笔记本设置wifi热点
  5. 多个mysql增加多个服务
  6. POJ 3233 矩阵乘法
  7. 洛谷 P1331 海战
  8. jquery实现菜单功能(单击展开或者关闭)-一般应用于后台
  9. java 二叉树实现
  10. 深入java----java内存区域及对象的创建
  11. Java之Java7新特性之try资源句式
  12. 编写blog第一天
  13. RS485 通信接收时丢失0x11等数据
  14. 单端测序(Single-read)和双端测序(Paired-end和Mate-pair)的关系
  15. 在 VS Code 和 Chrome 中调试
  16. beanshell获取响应结果数据
  17. node.js fs、http使用
  18. 冰与火之歌居然是在 DOS 系统上写出来的
  19. 关于VC++的增量链接(Incremental Linking)
  20. Java中DAO的实现

热门文章

  1. 树形DP codevs 1814 最长链
  2. 双向广搜 codevs 3060 抓住那头奶牛
  3. mac在xampp下使用yii2.0开发环境配置
  4. Css 常用属性
  5. Linux下各个目录的作用
  6. Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
  7. 一款漂亮实用的Android开源日期控件timessquare
  8. Android -- 桌面悬浮,仿360
  9. 学习Shell脚本编程(第3期)_在Shell程序中使用的参数
  10. 【转载】cocs2dx中c++与c#互调