链接:http://cogs.pro/cogs/problem/problem.php?pid=347

题意:不断维护一个区间,要求支持的操作有:删除区间、插入区间、区间加减、查询区间最大值。

看到这个题我们直接想到splay。还是熟悉的老方法,事先建两个节点,每一次操作,不管属于任何一种操作,先将区间的右端点转到右儿子,左端点转到根节点3号孙子,即右儿子的左儿子处。

1、区间加减:直接修改,延迟标记。

2、区间删除:直接扔掉左边界对应节点即可。

3、查询:基本操作。

4、插入区间:首先找到该区间位置,然后用初始建树相同方法插入即可。

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<vector>
  5 #define lch ch[0]
  6 #define rch ch[1]
  7 #define kch ch[k]
  8 #define xch ch[k^1]
  9 using namespace std;
 10 inline int read()
 11 {    char c=getchar();int x=0,y=1;
 12     while(c<'0'||c>'9'){if(c=='-') y=-1;c=getchar();}
 13     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
 14     return x*y;
 15 }
 16 int inf=0x7fffffff,n,q;
 17 class splay
 18 {    private:
 19         struct node
 20         {    int k,s,h,lazy;
 21             node* pt,*ch[2];
 22             node(const int& key)
 23             {    this->k=key;this->s=1;this->lazy=0;
 24                 this->lch=NULL;this->rch=NULL;
 25             }
 26             inline int sz(){return this?this->s:0;}
 27             inline int key(){return this?this->k:0;}
 28             inline int qh(){return this?this->h:-inf;}
 29             ~node()
 30             {    if(this->lch) delete this->lch;
 31                 if(this->rch) delete this->rch;
 32             }
 33             inline void mt()
 34             {    if(this) this->s=this->lch->sz()+this->rch->sz()+1;
 35                 if(this) this->h=std::max(this->k,max(this->lch->qh(),this->rch->qh()));
 36             }
 37             inline void dn()
 38             {    if(this&&this->lazy)
 39                 {    if(lch) lch->lazy+=lazy,lch->k+=lazy,lch->h+=lazy;
 40                     if(rch) rch->lazy+=lazy,rch->k+=lazy,rch->h+=lazy;
 41                     lazy=0;
 42                 }
 43             }
 44             inline void Ad(int key){if(this){this->lazy+=key;this->k+=key;this->h+=key;}}
 45             inline int pos(){return this==this->pt->lch;}
 46         }*root;
 47         void rotate(node* rt,int k)
 48         {    node* tmp=rt->xch;
 49             rt->dn();tmp->dn();
 50             tmp->pt=rt->pt;
 51             if(!rt->pt) this->root=tmp;
 52             else if(rt->pt->lch==rt) rt->pt->lch=tmp;
 53             else rt->pt->rch=tmp;
 54             rt->xch=tmp->kch;
 55             if(tmp->kch) tmp->kch->pt=rt;
 56             tmp->kch=rt;rt->pt=tmp;
 57             rt->mt();tmp->mt();
 58         }
 59         void sp(node* rt,node* prt=NULL)
 60         {    while(rt->pt!=prt)
 61             {    int k=rt->pt->lch==rt;
 62                 if(rt->pt->pt==prt) rotate(rt->pt,k);
 63                 else
 64                 {    int d=rt->pt->pt->lch==rt->pt;
 65                     rotate(k==d?rt->pt->pt:rt->pt,k);
 66                     rotate(rt->pt,d);
 67                 }
 68             }
 69         }
 70         node* build(const std::vector<int>& v,int l,int r)
 71         {    if(l>r) return NULL;
 72             int mid=(l+r)>>1;node* tmp=new node(v[mid]);
 73             tmp->lch=build(v,l,mid-1);tmp->rch=build(v,mid+1,r);
 74             if(tmp->lch) tmp->lch->pt=tmp; if(tmp->rch) tmp->rch->pt=tmp;
 75             tmp->mt();
 76             return tmp;
 77         }
 78     public:
 79         ~splay(){delete this->root;}
 80         splay(const std::vector<int>& v){this->root=build(v,0,v.size()-1);}
 81         splay(){this->root=new node(-inf);this->root->rch=new node(-inf);this->root->rch->pt=this->root;}
 82         node* kth(int x)
 83         {    ++x;
 84             node* now=this->root;
 85             while(now!=NULL)
 86             {    now->dn();
 87                 int k=now->lch->sz()+1;
 88                 if(x<k) now=now->lch;
 89                 else if(x==k) return now;
 90                 else x-=k,now=now->rch;
 91             }
 92             return NULL;
 93         }
 94         void add(int x,int y,int z)
 95         {    node* tmp=this->kth(y+1),*tmp2=this->kth(x-1);
 96             this->sp(tmp2);this->sp(tmp,this->root);
 97             this->root->rch->lch->Ad(z);
 98             this->root->rch->mt();this->root->mt();
 99         }
100         void del(int x,int y)
101         {    node* tmp=this->kth(y+1),*tmp2=this->kth(x-1);
102             this->sp(tmp2);this->sp(tmp,this->root);
103             delete this->root->rch->lch;
104             this->root->rch->lch=NULL;
105             this->root->rch->mt();this->root->mt();
106         }
107         int hmax(int x,int y)
108         {    node* tmp=this->kth(y+1),*tmp2=this->kth(x-1);
109             this->sp(tmp2);this->sp(tmp,this->root);
110             return this->root->rch->lch->h;
111         }
112         void insert(int x,splay* data)
113         {    this->sp(this->kth(x));this->sp(this->kth(x+1),this->root);
114             node* tmp=data->root;data->root=NULL;
115             this->root->rch->lch=tmp;tmp->pt=this->root->rch;
116             this->root->rch->mt();this->root->mt();
117         }
118 };
119 int main()
120 {    freopen("equake.in","r",stdin);
121     freopen("equake.out","w",stdout);
122     n=read();q=read();
123     splay* tree=new splay();
124     std::vector<int>v;char ord[10];int x,y,z;
125     for(int i=1;i<=n;i++) v.push_back(read());
126     tree->insert(0,new splay(v));
127     for(int i=1;i<=q;i++)
128     {    scanf("%s",ord);
129         if(ord[0]=='R'){x=read();y=read();z=read();tree->add(x,y,z);}
130         if(ord[0]=='I')
131         {    v.clear();x=read();y=read();
132             while(y--) v.push_back(read());
133             tree->insert(x,new splay(v));
134         }
135         if(ord[0]=='M'){x=read();y=read();tree->del(x,y);}
136         if(ord[0]=='Q'){x=read();y=read();printf("%d\n",tree->hmax(x,y));}
137     }
138     return 0;
139 }

cogs347

转载于:https://www.cnblogs.com/Loser-of-Life/p/7291274.html

cogs 347 地震 splay相关推荐

  1. [cogs347]地震

    COGS:地震(平衡树) COGS上一道题...文件名是equake 还是又打了一遍板子... 加个lazy标记就行了... 注意查询时先下传标记(lazy) // It is made by XZZ ...

  2. 数据结构(Splay平衡树):COGS 339. [NOI2005] 维护数列

    339. [NOI2005] 维护数列 时间限制:3 s   内存限制:256 MB [问题描述] 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线' _ '表示实际 ...

  3. 【BZOJ1014】【tyvj3486】火星人prefix,Splay+字符串hash

    Time:2016.07.19 Author:xiaoyimi 转载注明出处谢谢 传送门1 传送门2 思路&&注意: LCP这个东西可以用后缀数组,扩展kmp什么的来做 这里加上了插入 ...

  4. 【BZOJ3196】【Tyvj1730】二逼平衡树,第一次的树套树(线段树+splay)

    传送门1 传送门2 写在前面:创造迄今最长的正常代码的记录 思路:个人感觉这个树套树就是对线段树的每个区间建一棵splay来维护,最初觉得这个方法会爆T爆M--(实际上真的可能会爆).对于5个操作,我 ...

  5. 二逼平衡树——树套树(线段树套Splay平衡树)

    题面 Bzoj3196 解析 线段树和Splay两棵树套在一起,常数直逼inf,但最终侥幸过了 思路还是比较简单, 在原数组维护一个下标线段树,再在每一个线段树节点,维护一个对应区间的权值Splay. ...

  6. 简析平衡树(三)——浅谈Splay

    前言 原本以为\(Treap\)已经很难了,学习了\(Splay\),我才知道,没有最难,只有更难.(强烈建议先去学一学\(Treap\)再来看这篇博客) 简介 \(Splay\)是平衡树中的一种,除 ...

  7. AVL树、splay树(伸展树)和红黑树比较

    AVL树.splay树(伸展树)和红黑树比较 一.AVL树: 优点:查找.插入和删除,最坏复杂度均为O(logN).实现操作简单 如过是随机插入或者删除,其理论上可以得到O(logN)的复杂度,但是实 ...

  8. \V110\Microsoft.CppCommon.targets(347,5): error MSB6006: “CL.exe”已退出,代码为 -1073741515。的解决方法

    VS2012调试工程,出现如下问题: \V110\Microsoft.CppCommon.targets(347,5): error MSB6006: "CL.exe"已退出,代码 ...

  9. bzoj1251: 序列终结者 (splay)

    splay可以用于维护序列,比如noi的维修序列,比如这道 发现当时splay没写总结,也没题解 然后重新写splay竟然耗了一个晚上 结果是因为max[0]没有附最小值!!血一样的教训 最后祭出in ...

  10. BZOJ 1503 郁闷的出纳员(splay)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...

最新文章

  1. python最简单的爬取邮箱地址_python简单爬虫,抓取邮箱
  2. 内核compiler.h的学习
  3. 78. Leetcode 264. 丑数 II (堆-技巧二-多路归并)
  4. django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)...
  5. 想不到,那些让我半夜偷偷收藏的沙雕表情包,竟是出自AI之手
  6. 绝对不忽悠、暑期择机功能该咋选?
  7. LeetCode 2032. 至少在两个数组中出现的值(哈希/位运算)
  8. 【LeetCode笔记】136. 只出现一次的数字(Java、位运算)
  9. 芸众商城社交电商系统V2.2.64
  10. MS SQL安装提示
  11. stackexchange.mysql_StackExchange.Redis加载Lua脚本进行模糊查询的批量删除和修改
  12. 分层结构的生活例子_AI的分层强化学习与人脑神经机制的联系
  13. jquery 添加扩展方法及为选择的对象添加方法
  14. 什么是自然语言处理技术
  15. 重启计算机突然断网,今天电脑总是突然断网,怎么回事
  16. 幼儿园计算机和网络安全情况,幼儿园网络安全自查报告
  17. vue-print 实现打印功能
  18. 无视硬件检测直接运行Win10混合现实门户
  19. 电脑打开后,或者锁屏后打开,屏幕变黄——亲测解决办法
  20. 解决上网认证系统 IP 更改后 Ubuntu 等 Linux 系统无法上网的问题

热门文章

  1. 用小马激活的千万小心!!
  2. 问道法宝升级经验统计
  3. 靶基因高通量测序建库流程介绍
  4. 二维码在Access中的使用
  5. 坦克大战游戏c语言,C语言开发坦克大战游戏
  6. HTML_龙湖地产界面自制
  7. blowfish java_Java语言实现Blowfish加密算法完整代码分享
  8. html短信验证登录
  9. 金蝶K3系统单据对应ICTemplate表单ID信息
  10. 「转录组」WGCNA实战原理两不误