题目大意:
  维护一个长度为$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. HDU 1535 S-Nim(SG函数)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 【SHELL】Linux下安装Oracle Client

    一.新建Oracle脚本存储目录并上传文件 [root@A04-Test-172]# mkdir -p /tmp/instance_oracle #新建存储目录 [root@A04-Test-172 ...

  3. 谈一谈Tomcat中webSocket,Jetty WebSocket 和Spring WebSocket以及github中Java-WebSocket.jar分别对Socket协议的角色定位以及效果的不同点;

    开局先上一张图:(http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html)   当前截图来自于apache的tomcat官网(问:为 ...

  4. uoj206 [APIO2016]最大差分

    ref #include "gap.h" #include <iostream> #include <cstdio> using namespace std ...

  5. IOS开发学习笔记023-UIToolBar的使用

    这里使用代码实现 大概过程: 1.创建工具条 2.创建插入条 3.添加头像.标签.删除按钮 4.点击头像获取标签信息 做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添 ...

  6. copy & deepcopy

    1 import copy 2 3 字典参照列表结论,看是否有深层嵌套. 4 a = {'name':1,'age':2} 5 b = a 6 a['name'] = 'ff' 7 print(a) ...

  7. 看似不是dfs的dfs HDU-1455

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. windows批处理 打开exe后关闭cmd

    start "" "程序路径.exe"    这样调用就OK啦.如: start "" "D:\123.exe" 如果下 ...

  9. 团队Alpha版本冲刺(一)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...

  10. C# 泛型的入门理解(来自网络)

    using System.Collections; class Program { //做个比较 static void Main(string[] args) { //new对象 Cls a1 = ...