BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者
Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 3778 Solved: 1583
[Submit][Status][Discuss]
Description
网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。
Input
第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。
Output
对于每个第3种操作,给出正确的回答。
Sample Input
1 1 3 2
1 2 4 -1
2 1 3
3 2 4
Sample Output
【数据范围】
N<=50000,M<=100000。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
const int N=1e5+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,Q,op,l,r,v;
struct node{
int fa,ch[],size,rev,mx,v,tag;
}t[N];
int root;
inline int wh(int x){return t[pa].ch[]==x;}
inline void update(int x){
t[x].size=t[lc].size+t[rc].size+;
t[x].mx=max(t[x].v,max(t[lc].mx,t[rc].mx));
}
inline void pushDown(int x){
if(t[x].rev){
swap(lc,rc);
if(lc) t[lc].rev^=;
if(rc) t[rc].rev^=;
t[x].rev=;
}
if(t[x].tag){
int v=t[x].tag;
if(lc) t[lc].tag+=v,t[lc].v+=v,t[lc].mx+=v;
if(rc) t[rc].tag+=v,t[rc].v+=v,t[rc].mx+=v;
t[x].tag=;
}
}
int build(int l,int r,int f){//printf("build %d %d %d\n",l,r,f);
if(l>r) return ;
int x=(l+r)>>;
lc=build(l,x-,x);rc=build(x+,r,x);
t[x].fa=f;
t[x].rev=t[x].tag=;t[x].v=t[x].mx=;
update(x);
return x;
}
inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(g) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
void splay(int x,int tar){
for(;t[x].fa!=tar;rotate(x))
if(t[pa].fa!=tar) rotate(wh(x)==wh(pa)?pa:x);
if(tar==) root=x;
} inline int kth(int k){
int ls=,x=root;
while(x){
pushDown(x);
int _=ls+t[lc].size;
if(_<k&&k<=_+) return x;
if(k<=_) x=lc;
else ls=_+,x=rc;
}
return -;
}
void add(int l,int r,int v){//printf("add %d %d %d\n",l,r,v);
int p=kth(l);splay(p,);
int x=kth(r+);splay(x,root);
t[lc].tag+=v;t[lc].v+=v;t[lc].mx+=v;
}
void rev(int l,int r){
int p=kth(l);splay(p,);
int x=kth(r+);splay(x,root);
t[lc].rev^=;
}
void getmax(int l,int r){
int p=kth(l);splay(p,);
int x=kth(r+);splay(x,root);
printf("%d\n",t[lc].mx);
} void print(int x){
if(x==) return;
pushDown(x);
if(lc) print(lc);
if(x!=&&x!=n+) printf("%d ",t[x].v);
if(rc) print(rc);
}
int main(){
//freopen("in.txt","r",stdin);
n=read();Q=read();
t[].mx=-INF;
root=build(,n+,);
//for(int i=1;i<=n+2;i++) printf("hi %d %d %d\n",i,t[i].v,t[i].size);
while(Q--){
op=read();l=read();r=read();
if(op==){v=read();add(l,r,v);}
else if(op==) rev(l,r);
else if(op==) getmax(l,r);
//print(root);puts("");
}
}
BZOJ 1251: 序列终结者 [splay]的更多相关文章
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- BZOJ 1251 序列终结者(Splay)
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术 ...
- BZOJ 1251: 序列终结者
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3773 Solved: 1579 [Submit][Status][Dis ...
- bzoj 1251: 序列终结者 平衡树,fhqtreap
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1251 思路 好简单的模板题 不过还是wrong了好几发 叶子节点要注意下,不能使用 遇到就不 ...
- 【BZOJ】1251: 序列终结者(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...
- 1251. 序列终结者【平衡树-splay】
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
- 【BZOJ1251】序列终结者 Splay
一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...
- CODEVS 4655 序列终结者-splay(区间更新、区间翻转、区间最值)
4655 序列终结者 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 网上有许多题,就是给定一个序列,要 ...
- [bzoj1251]序列终结者——splay
题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...
随机推荐
- oracle函数案例以及分页案例
--日期函数select sysdate from dual--返回两个日期select months_between(to_date('2017-1-7','yyyy-mm-dd'),to_date ...
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...
- C#开发微信门户及应用(33)--微信现金红包的封装及使用
我在上篇随笔<C#开发微信门户及应用(32)--微信支付接入和API封装使用>介绍为微信支付的API封装及使用,其中介绍了如何配置好支付环境,并对扫码支付的两种方式如何在C#开发中使用进行 ...
- Android Service
一.在MainAcitivity界面启动Service : public class MyService extends Service intent = new Intent(MainActivi ...
- iPhone开发学习
Xcode6中怎么添加空工程模板 NSBundle 使用(程序的资源) 本地存储数据的简单方式:NSUserDefaults UIApplication隐藏statusBarStyle ...
- Socket简单使用
客户端代码: import java.io.*; import java.net.*; public class DailyAdviceClient { public void go(){ try{ ...
- java文档注释--javadoc的用法
1.前言 Java中有三种注释方式.前两种分别是 // 和 /* */,主要用于代码的注释,以此来方便代码的可读性.第三种被称作说明注释或文档注释,它以 /** 开始,以 */结束,文档注释允许你在程 ...
- GJM : Unity3D HIAR -【 快速入门 】 四、创建 Hello World
创建 Hello World 本文将介绍如何在 Windows 系统下,使用 HiAR SDK 创建一个简单的 AR 应用.在开始之前,请先完成下列准备工作: 注册 HiAR 帐户 获取 AppKey ...
- MySQL大数据优化
我们考虑的情况是在你的数据量很大的情况下,千万级别的数据量.不要当我们的请求响应时间已经让我无法忍受的时候,再来想起来优化,可能有点迟了.因为可能会丢失很多潜在的价值客户.所以,在我们当初设计表,或者 ...
- 你必须收藏的Github技巧
一秒钟把Github项目变成前端网站 GitHub Pages大家可能都知道,常用的做法,是建立一个gh-pages的分支,通过setting里的设置的GitHub Pages模块可以自动创建该项目的 ...