3468 -- A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 204955 | Accepted: 63007 | |
Case Time Limit: 2000MS |
You have N integers, A1, A2, ... , 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.
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
You need to answer all Q commands in order. One answer in a line.
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
4 55 9 15
- #include
- #include
- using namespace std;
- typedef long long ll;
- const int inf = 100010;
- int n , m ;
- ll ly[inf<<2] , tree[inf<<2] ;
- //用longlong类型,数较大时相加的和较大
- //tree[] 用来实现完全二叉树 需要四倍空间
- void pushup(int u)
- {
- tree[u] = tree[u<<1] + tree[u<<1 | 1]; //更新结点 左右儿子的和
- }
- void buildtree(int l , int r , int u ) //用完全二叉树建一个线段树
- {
- //从根开始往下建树
- ly[u]=0;
- if( l == r ) //最底层叶子
- {
- cin>>tree[u];
- return ;
- }
- int mid = (l + r) >>1 ;
- buildtree(l , mid , u<<1); //左子树
- //右子树
- buildtree(mid + 1 , r , u<<1 | 1); //u<<1|1 <--> u*2+1 位运算计算更快
- //更新父节点
- pushup(u);
- }
- void pushdown(int u,int len)//下传lazy
- {
- if(ly[u]){
- ly[u<<1] += ly[u];//父节点lazy下传到左儿子
- ly[u<<1 | 1] += ly[u];//到右儿子
- tree[u<<1] += (len - (len>>1)) * ly[u]; //左根节点加上(儿子每个数加上c的和)
- tree[u<<1 | 1] += (len>>1) * ly[u];
- ly[u] = 0;
- }
- }
-
- void update (int a , int b , int c , int l , int r , int u)//更新区间
- {
- int len = r - l + 1;
- if( a <= l && b >= r){//区间已覆盖,直接计算
- tree[u] += len* c;
- ly[u] += c; //lazy标记加上c
- return ;
- }
- pushdown(u , len);//lazy下传
- int mid = (l + r) >>1 ;
- if(mid >= a) update(a , b , c , l , mid , u<<1);//更新左子树
- if(mid < b) update(a , b , c , mid+1 , r , u<<1 | 1);//更新右子树
- pushup(u);//更新父节点
- }
-
- ll query(int a, int b, int l, int r, int u)//计算区间和
- {
- if(a <= l && b >= r) return tree[u];//满足lazy , 直接返回值
- int len = r - l + 1;
- pushdown(u , len);//下传
- int mid = (l + r) >>1 ;
- ll sum =0;
- if(mid >= a ) sum += query(a, b, l , mid , u<<1);
- if(mid < b) sum += query(a , b , mid+1 , r , u<<1 | 1);
- return sum;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin>>n>>m;
- buildtree(1,n,1);
- while(m--)
- {
- char str;
- int a , b , c;
- cin>>str;
- if(str == 'C'){
- cin>>a>>b>>c;
- update(a,b,c,1,n,1);
- }else if(str == 'Q'){
- cin>>a>>b;
- cout<<query(a,b,1,n,1)<
- }
- }
- return 0;
- }