[BZOJ3212][POJ3468]A Simple Problem with Integers
题目大意:
维护一个长度为$n(n\leq100000)$的数列,支持区间加、区间求和两种操作,操作共$m(m\leq100000)$次。
思路:
Splay区间操作。
#include<cstdio>
#include<cctype>
typedef long long int64;
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) if(ch=='-') neg=true;
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return neg?-x:x;
}
inline char getalpha() {
register char ch;
while(!isalpha(ch=getchar()));
return ch;
}
const int N=;
class SplayTree {
private:
int par[N],ch[N][],size[N],root;
int64 val[N],sum[N],tag[N];
void push_down(const int &p) {
if(ch[p][]) {
tag[ch[p][]]+=tag[p];
val[ch[p][]]+=tag[p];
sum[ch[p][]]+=tag[p]*size[ch[p][]];
}
if(ch[p][]) {
tag[ch[p][]]+=tag[p];
val[ch[p][]]+=tag[p];
sum[ch[p][]]+=tag[p]*size[ch[p][]];
}
tag[p]=;
}
void push_up(const int &p) {
size[p]=size[ch[p][]]+size[ch[p][]]+;
sum[p]=sum[ch[p][]]+sum[ch[p][]]+val[p];
}
void rotate(const int &x) {
const int y=par[x],z=par[y];
push_down(y),push_down(x);
const int b=x==ch[y][];
par[ch[x][b]=par[ch[y][!b]=ch[x][b]]=y]=x;
par[ch[z][ch[z][]==y]=x]=z;
push_up(y),push_up(x);
}
void splay(int x,const int &goal) {
for(register int y=par[x],z=par[y];y!=goal;rotate(x),z=par[y=par[x]]) {
if(z!=goal) rotate((x==ch[y][])^(y==ch[z][])?x:y);
}
if(!goal) root=x;
}
int find(int x) {
for(register int y=root;;y=ch[y][size[ch[y][]]+<x]) {
push_down(y);
if(par[y]&&y==ch[par[y]][]) x-=size[ch[par[y]][]]+;
if(size[ch[y][]]+==x) return y;
}
}
public:
void build(const int &n) {
for(register int i=;i<=n+;i++) {
par[ch[i][]=i+]=i;
if(i&&i<=n) val[i+]=getint();
}
size[n+]=;
splay(n+,);
}
void modify(const int &l,const int &r,const int &x) {
splay(find(l),);
splay(find(r+),root);
tag[ch[ch[root][]][]]+=x;
val[ch[ch[root][]][]]+=x;
sum[ch[ch[root][]][]]+=x*size[ch[ch[root][]][]];
}
int64 query(const int &l,const int &r) {
splay(find(l),);
splay(find(r+),root);
return sum[ch[ch[root][]][]];
}
};
SplayTree t;
int main() {
const int n=getint(),m=getint();
t.build(n);
for(register int i=;i<m;i++) {
const int opt=getalpha(),l=getint(),r=getint();
if(opt=='C') t.modify(l,r,getint());
if(opt=='Q') printf("%lld\n",t.query(l,r));
}
return ;
}
[BZOJ3212][POJ3468]A Simple Problem with Integers的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- bzoj3212 Pku3468 A Simple Problem with Integers 线段树
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 So ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
随机推荐
- HDU 1535 S-Nim(SG函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Android stadio litepal
今天看到技术交流群里有人招聘Android,要求会litepal. 我立马百度了下.嗯,我的学习技术的精神,是值得称赞的. litepal就是操作数据库的一个框架.git地址: https://git ...
- android基础知识杂记
Activity中获取视图组件对象:public View findViewById(@IdRes int id) 该方法以组件的资源ID为参数,返回一个视图对象View,需要强转成具体的视图类对象. ...
- IOS笔记047-代理传值和block传值
在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...
- Html语言的标签讲解
一.head头部中的内容: 1.<meta charset="UTF-8"> <--!告诉浏览器什么编码--> 2.<meta http-equiv= ...
- 【转】unity自带寻路Navmesh入门教程(二)
http://liweizhaolili.blog.163.com/blog/static/16230744201271210237616/ 上一节简单介绍了NavMesh寻路的基本用法,这次来介绍一 ...
- FreeBSD查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令
FreeBSD查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令 来源 https://www.liurongxing.com/freebsd-tips.html 来源 http://blog.51c ...
- bzoj 4292: [PA2015]Równanie
Description 对于一个正整数n,定义f(n)为它十进制下每一位数字的平方的和.现在给定三个正整数k,a,b,请求出满足a<=n<=b且k*f(n)=n的n的个数. Input ...
- 妹子(girls)
妹子(girls) 题目描述 万人迷皮皮轩收到了很多妹子的礼物,由于皮皮轩觉得每个妹子都不错,所以将她们礼物的包装盒都好好保存,但长此以往皮皮轩的房间里都堆不下了,所以只能考虑将一些包装盒放进其他包装 ...
- Teleportation(tel)
Teleportation(tel) 题目描述 Zy大帝拥有n个星球,因为距离非常遥远,所以Zy在他所居住的1号星球和他的军事基地霸中所在的2号星球建造了两个传送门,这样从1号星球到2号星球就只需要2 ...