A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 47944   Accepted: 14122
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

 
 
 
 
 
 
 
 
 /* ***********************************************
Author :kuangbin
Created Time :2013/8/24 22:13:15
File Name :F:\2013ACM练习\专题学习\splay_tree_2\POJ3468.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//普通的线段树操作,区间增加一个值,查询区间的和
#define Key_value ch[ch[root][1]][0]
const int MAXN = ;
int pre[MAXN],ch[MAXN][],root,tot1;
int size[MAXN];//子树的结点数
int add[MAXN];//增量的延迟标记
int key[MAXN];
long long sum[MAXN];//子树的和
int s[MAXN],tot2;//内存池和容量
int a[MAXN];//初始的数组,建树的时候用,下标从1开始 //debug部分**********************************
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d add = %2d sum = %I64d\n",x,ch[x][],ch[x][],pre[x],size[x],add[x],sum[x]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
//以上是debug部分************************************** void NewNode(int &r,int father,int k)
{
if(tot2)r = tot2--;//取的时候是tot2--,存的时候就是++tot2
else r = ++tot1;
pre[r] = father;
size[r] = ;
add[r] = ;
key[r] = k;
sum[r] = k;
ch[r][] = ch[r][] = ;
}
//给r为根的子树增加值,把当前结点更新掉,加延迟标记
void Update_Add(int r,int ADD)
{
if(r == )return;
add[r] += ADD;
key[r] += ADD;
sum[r] += (long long)ADD*size[r];
}
void push_up(int r)
{
size[r] = size[ch[r][]] + size[ch[r][]] + ;
sum[r] = sum[ch[r][]] + sum[ch[r][]] + key[r];
}
void push_down(int r)
{
if(add[r])
{
Update_Add(ch[r][],add[r]);
Update_Add(ch[r][],add[r]);
add[r] = ;
}
}
//建树
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l+r)/;
NewNode(x,father,a[mid]);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
push_up(x);
}
int n,q;
//初始化
void Init()
{
root = tot1 = tot2 = ;
ch[root][] = ch[root][] = pre[root] = size[root] = add[root] = sum[root] = key[root] = ;
//加两个虚结点
NewNode(root,,-);
NewNode(ch[root][],root,-);
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
Build(Key_value,,n,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
//旋转,0为左旋,1为右旋
void Rotate(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);//先把y的标记下传,在把x的标记下传
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
//Splay调整,将r结点调整到goal下面
void Splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
Rotate(r,ch[pre[r]][]==r);
else
{
int y = pre[r];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == ) root = r;
}
//得到第k个结点
int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][]] + ;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][],k);
else return Get_kth(ch[r][],k-t);
} //区间增加一个值
//因为加了个空结点,所以将第l个点旋转到根结点,第r+2个点旋转到根结点的右孩子
//那么Key_value就是需要修改的区间[l,r]
void ADD(int l,int r,int D)
{
Splay(Get_kth(root,l),);
Splay(Get_kth(root,r+),root);
Update_Add(Key_value,D);
push_up(ch[root][]);
push_up(root);
}
//查询区间的和
long long Query_sum(int l,int r)
{
Splay(Get_kth(root,l),);
Splay(Get_kth(root,r+),root);
return sum[Key_value];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&q) == )
{
Init();
//debug();
int x,y,z;
char op[];
while(q--)
{
scanf("%s",op);
if(op[] == 'Q')
{
scanf("%d%d",&x,&y);
printf("%I64d\n",Query_sum(x,y));
}
else
{
scanf("%d%d%d",&x,&y,&z);
ADD(x,y,z);
}
}
}
return ;
}
 
 

最新文章

  1. leetcode--Maximum Subarray
  2. 基于GIS的旅游辐射区人口统计
  3. linux 账号管理与ACL权限设定
  4. iOS 图片加载导致内存警告
  5. QTP 10 安装及破解
  6. 转:JQuery中$.ajax()方法参数详解
  7. hdu1078 bfs
  8. 安装使用adobe_photoshop_cs6
  9. 裸机编程与OS环境编程的有关思考
  10. 蜗牛爱课 - iOS7、8模态半透明弹出框
  11. 4种Java引用浅解
  12. 最新的QT git代码到code.qt.io/cgit,还有planet.qt.io有许多博客
  13. [Swift]LeetCode509. 斐波那契数 | Fibonacci Number
  14. Excel汉字转换拼音首字母缩写的函数
  15. JAVA 变量 数据类型 运算符 知识小结
  16. Linux 多进程实现方法
  17. ubuntu18安装ubuntu kylin软件中心
  18. eclipse下JAVA的搭建
  19. CCSDS标准的LDPC编译码仿真
  20. 【洛谷 P4360】 [CEOI2004]锯木厂选址(斜率优化)

热门文章

  1. PHP获得用户的真实IP地址
  2. js中的盒子模型
  3. json在线工具
  4. nio复习总结
  5. [android]Intent跳转新的Activity可以传递数据过去
  6. jquery 绑定,mvc和webform的三种方式
  7. 使用mockito模拟静态方法
  8. 【洛谷】P2000 拯救世界
  9. windows7无声音,提示未插入扬声器或耳机的解决
  10. 在Mac上安装MongoDB