题目大意:
  维护一个长度为$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. Linux基本命令运行

    文件基本操作: 增删查改: 创建文件:touch(创建文件和修改文件或者目录的时间戳),vim.vi(编辑/创建文件),mkdir(创建文件目录) 移动和修改文件名:mv 删除文件:rm –rf(强制 ...

  2. wim

    wim 编辑 WIM是英文Microsoft Windows Imaging Format(WIM)的简称,它是Windows基于文件的映像格式.WIM 映像格式并非现在相当常见的基于扇区的映像格式, ...

  3. 使用 Region,RegionManager 在 XNA 中创建特殊区域(十八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  4. [oldboy-django][5python基础][高级特性]generator生成器

    # 生成器基础 - 定义 在循环的时候不断推算下一个元素的值,而不是一下子创建空间存储所有元素,这样节省空间. 并且在适当的条件结束循环,这种一边循环一边计算的机制,称为generator生成器 - ...

  5. IE IE8 iframe的onload方法分析 IE浏览器onload事件失效

    判断iframe是否加载完成的完美方法 IE 支持 iframe 的 onload 事件,不过是隐形的,需要通过 attachEvent 来注册. 第二种方法比第一种方法更完美(采用readystat ...

  6. BZOJ 1864:[Zjoi2006]三色二叉树(树DP)

    三色二叉树 问题描述 输入 仅有一行,不超过500000个字符,表示一个二叉树序列. 输出 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. 样例输入 1122002010 ...

  7. Codeforces 1038F Wrap Around (Codeforces Round #508 (Div. 2) F) 题解

    写在前面 此题数据量居然才出到\(n=40\)???(黑人问号)以下给出一个串长\(|S|=100,n=10^9\)的题解. 题目描述 给一个长度不超过\(m\)的01串S,问有多少个长度不超过\(n ...

  8. Codeforeces 954C Matrix Walk

    题目大意 考虑一个 $x\times y$ 的矩阵 $A_{x\times y}$ ,$A_{i,j} = (i-1)x+y$ . 从矩阵中的某个位置出发,每次可向上下左右移动一步,每到一个位置,记录 ...

  9. HDU 2065 "红色病毒"问题 ——快速幂 生成函数

    $A(x)=1+x^2/2!+x^4/4!...$ $A(x)=1+x^1/1!+x^2/2!...$ 然后把生成函数弄出来. 暴力手算. 发现结论. 直接是$4^{n-1}+2^{n-1}$ 然后快 ...

  10. [BZOJ2456] mode(一道很有意思的题)

    传送门 看到这个题的第一反应是离散化+线段树乱搞.. eeeeeeeeeeee感觉数据结构学傻了,其实直接存下来,sort一遍,n/2的位置的就是答案 当然前提是空间够的话 1m的空间连数组都开不下 ...