题目大意:
  维护一个长度为$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的更多相关文章

  1. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  2. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  3. 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 ...

  4. poj------(3468)A Simple Problem with Integers(区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   ...

  5. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   ...

  6. bzoj3212 Pku3468 A Simple Problem with Integers 线段树

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2046  So ...

  7. BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2530  So ...

  8. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  9. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

随机推荐

  1. activity切换交互动画

    activity切换的时候,想要有动画,那么... 1.想要有效果的activity设置theme <activity android:name=".MainActivity" ...

  2. 面试常问 Java基础 冒泡排序

    冒泡排序就是对一个数组进行排序. 用双层for循环就可以解决. 第一层,决定排序的次数. n个数,进行n-1次排序就行了. 第二层,把相邻的两个数比较,谁小,放到后面和下一个数字比较,谁小谁放到后面, ...

  3. 如何排查Java内存泄漏?看完我给跪了!

    没有经验的程序员经常认为Java的自动垃圾回收完全使他们免于担心内存管理.这是一个常见的误解:虽然垃圾收集器做得很好,但即使是最好的程序员也完全有可能成为严重破坏内存泄漏的牺牲品.让我解释一下. 当不 ...

  4. React-表单操作

    用户在表单填入的内容,属于用户跟组件的互动,所以不能用 this.props 读取 <!DOCTYPE html> <html lang="zh-cn"> ...

  5. python 学习分享-函数篇

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...

  6. Windows命令行中pip install jieba,但没有安装到anaconda3中

    系统混淆了python3环境下的pip和anaconda3环境下的pip. 找到Anaconda3的Scripts目录,我这里是C:\Users\Diane\Anaconda3\Scripts 将该目 ...

  7. python的重重之器(生成器、迭代器、装饰器)

    一.装饰器 1.定义:本质是函数,装饰其他函数就是为其他函数添加附件功能. 2.原则: a.不能修改被装饰的函数的源代码: b.不能修改被装饰的函数的调用方式: 实例: #!/usr/bin/env ...

  8. sqlserver创建存储过程返回table

    --创建存储过程test create procedure [dbo].[test] ( @I_MTR NVARCHAR (MAX), @I_TYPE NVARCHAR (MAX), @I_FAC N ...

  9. nyoj 题目20 吝啬的国度

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...

  10. 使用xcache加速PHP运行

    XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编 ...