A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 141040 | Accepted: 43753 | |
Case Time Limit: 2000MS |
Description
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.
Input
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.
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
Source
线段树,区间查询及更改区间的值(给一个区间所有的数都加上一个值)
/*
* 白书的板子
* 认真思考,仔细理解
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
const int MAX_N=1e5+7;
const int DAT_SIZE=1<<20;
int N,Q;
int A[MAX_N];
char T[MAX_N];
int L[MAX_N],R[MAX_N],X[MAX_N];
// 线段树
ll data[DAT_SIZE],datb[DAT_SIZE];
/*
* 对区间[a, b)同时加x
* k 是节点的编号,对应的是区间[l, r)
*/
void add(int a, int b, int x, int k,int l,int r)
{
if(a <= l && r <= b) data[k] += x;
else if(l < b && a < r)
{
datb[k] += (min(b,r) - max(a,l))*x;
add(a, b, x, k * 2 + 1, l, (l + r) / 2);
add(a, b, x, k * 2 + 2, (l + r) / 2, r);
}
}
/*
* 计算[a, b)的和
* k 是节点的编号,对应的是区间[l, r)
*/
ll sum(int a, int b, int k, int l, int r)
{
if(b <= l || r <= a) return 0;
else if(a <= l && r <= b) return data[k] * (r - l) + datb[k];
else
{
ll res = (min(b, r) - max(a, l)) * data[k];
res += sum(a, b, k * 2 + 1, l, (l + r) / 2);
res += sum(a, b, k * 2 + 2, (l + r) / 2, r);
return res;
}
}
int main()
{
ios::sync_with_stdio(0);
cin>>N>>Q;
for(int i=0;i<N;i++)
{
cin>>A[i];
add(i,i+1,A[i],0,0,N);
}
for(int i=0;i<Q;i++)
{
cin>>T[i];
if(T[i]=='C')
{
cin>>L[i]>>R[i]>>X[i];
add(L[i]-1, R[i], X[i], 0, 0, N);
// cout<<T[i]<<L[i]<<R[i]<<X[i]<<endl;
}
else
{
cin>>L[i]>>R[i];
cout<<sum(L[i]-1, R[i], 0, 0, N)<<endl;
// cout<<T[i]<<L[i]<<R[i]<<endl;
}
}
return 0;
}
感觉跟没写一样呀,别人都是用的lazy
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████ ┃+
* ┃ ┃ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃
* ┃ ┃ + + + +
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ + 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃ +
* ┃ ┗━━━┓ + +
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
/*--------- Hongjie ----------*/
// #include<bits/stdc++.h>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<deque>
#include<cmath>
#include<cstdio>
#include<bitset>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int ,int> P;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6+7;
ll data[MAXN],datb[MAXN];
int n,q;
void init(int _n) {//建造一个完全二叉树
n = 1;
while(n<_n) n<<=1;
memset(data,0,sizeof(ll)*(n*4));
memset(datb,0,sizeof(ll)*(n*4));
}
void update(int k,int a) {
k += n-1;
datb[k] = a;
while(k>0) {
k = (k-1)/2;
datb[k] = datb[k*2+1]+datb[k*2+2];
}
}
void add(int a,int b,int x,int k,int l,int r){
if(a<=l && r<=b)//[l,r) in [a,b)
data[k] += x;
else if(l<b && a<r) {// [l,r) 相交 [a,b)
datb[k] += x*(min(b,r)-max(a,l));
add(a,b,x,k*2+1,l,(l+r)/2);
add(a,b,x,k*2+2,(l+r)/2,r);
}
}
ll sum(int a,int b,int k,int l,int r) {
if(b<=l || r<=a) return 0;//[l,r) not in [a,b)
else if(a<=l && r<=b) return data[k]*(r-l)+datb[k];//[l,r) in [a,b)
else {
ll res = data[k]*(min(b,r) - max(a,l));
res += sum(a,b,k*2+1,l,(l+r)/2);
res += sum(a,b,k*2+2,(l+r)/2,r);
return res;
}
}
int main(){
// freopen("../in.txt","r",stdin);
// freopen("../out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
while(cin>>n>>q) {
int n_ = n;
init(n);
for(int i=0;i<n_;++i) {
int x;
cin>>x;
update(i,x);
}
string ch;
int a,b,c;
for(int i=0;i<q;++i) {
cin>>ch;
if(ch[0]=='C') {
cin>>a>>b>>c;
add(a-1,b,c,0,0,n);
}
else{
cin>>a>>b;
cout<<sum(a-1,b,0,0,n)<<endl;
}
}
}
return 0;
}