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
The sums may exceed the range of 32-bit integers.
#include<iostream>//C++提交
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
ll tree[N << 2], lazy[N << 2];
void pushup(int rt){
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
void pushdown(int rt,int len){
if(lazy[rt]){
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1] += lazy[rt] * (len - (len >> 1));
tree[rt << 1 | 1] += lazy[rt] * (len >> 1);
lazy[rt] = 0;
}
}
void build(int rt,int l,int r){
lazy[rt] = 0;
if (l == r)
{
scanf("%lld", &tree[rt]);
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt,int l,int r,int L,int R,int val){
if(L<=l&&r<=R){
tree[rt] += val* (r - l + 1);
lazy[rt] += val;
return;
}
pushdown(rt, r - l + 1);
int mid = (l + r) >> 1;
if(L<=mid)
update(rt << 1, l, mid, L, R, val);
if(R>mid)
update(rt << 1 | 1, mid + 1, r, L, R, val);
pushup(rt);
}
ll query(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R){
return tree[rt];
}
int mid = (l + r) >> 1;
pushdown(rt, r - l + 1);
ll ans = 0;
if(L<=mid)
ans += query(rt << 1, l, mid, L, R);
if(mid<R)
ans += query(rt << 1 | 1, mid + 1, r, L, R);
return ans;
}
int main(){
int n, m;
while(~scanf("%d%d",&n,&m)){
build(1, 1, n);
char s[5];
int l, r, c;
while(m--){
// printf("--%d--", m);
scanf("%s", s);
scanf("%d%d", &l, &r);
if(s[0]=='Q')
printf("%lld\n", query(1, 1, n, l, r));
else {
scanf("%d", &c);
update(1, 1, n, l, r, c);
}
}
}
return 0;
}
//因为long long的写法上wa好多次。。
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct tree{
// int l, r;
ll sum,lazy;
} Tree[N << 2];
ll ans = 0;
void pushup(int rt)
{
Tree[rt].sum = Tree[rt << 1].sum + Tree[rt << 1 | 1].sum;
}
void pushdown(int rt,int x){
if(Tree[rt].lazy){
Tree[rt << 1].lazy += Tree[rt].lazy;
Tree[rt << 1 | 1].lazy += Tree[rt].lazy;
Tree[rt << 1].sum += Tree[rt].lazy * (x - (x >> 1));
Tree[rt << 1 | 1].sum += Tree[rt].lazy * (x >> 1);
Tree[rt].lazy = 0;
}
}
void build(int rt,int l,int r){
Tree[rt].lazy = 0;
if(l==r){
scanf("%lld", &Tree[rt].sum);
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt,int L,int R,int val,int l,int r){
if(L<=l&&r<=R){
Tree[rt].lazy += val;
Tree[rt].sum += val * (r - l + 1);
return;
}
pushdown(rt, r - l + 1);
int mid = (l + r) >> 1;
if(L<=mid)
update(rt << 1, L, R, val, l, mid);
if(R>mid)
update(rt << 1 | 1, L, R, val, mid + 1, r);
pushup(rt);
}
void query(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y){
ans+=Tree[rt].sum;
return;
}
pushdown(rt,r-l+1);
int mid = (r + l) >> 1;
if (x <= mid)
query(rt << 1, l, mid, x, y);
if(y>mid)
query(rt << 1 | 1, mid + 1, r, x, y);
}
int main(){
int n, m;
char s[5];
int l, r, c;
while(~scanf("%d%d",&n,&m)){
build(1, 1, n);
while(m--){
// printf("--%d--", m);
scanf("%s", s);
scanf("%d%d", &l, &r);
if(s[0]=='Q'){
ans = 0;
query(1, 1, n, l, r);
printf("%lld\n", ans);
}
else {
scanf("%d", &c);
update(1, l, r, c,1, n);
}
}
}
return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main{
static int N=(int)1e5+1,n,m,k;
// static int tree[]=new int [N<<2];
static long lazy[]=new long [N<<2];
static long sum[]=new long [N<<2];
// static long a[]=new long [N];
public static void main(String[] args)throws IOException {
InputStream in=System.in;
InputReader sc=new InputReader(in);
OutputStream op=System.out;
PrintWriter out=new PrintWriter(op);
int x,y,z;
char ch;
int t=sc.nextInt();
for(int h=1;h<=t;h++){
n=sc.nextInt();
int q=sc.nextInt();
build(1,1,n,sc);
while(q-->0) {
ch=sc.next().charAt(0);
x=sc.nextInt();y=sc.nextInt();
if(ch=='Q') out.println(query(x,y,1,n,1));
else {
z=sc.nextInt();
update(x,y,1,n,1,z);
}
}
}
out.close();
}
static void build(int rt,int l,int r,InputReader sc) {
if(l==r) {
sum[rt]=sc.nextLong();
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid,sc);
build(rt<<1|1,mid+1,r,sc);
pushup(rt);
}
static void update(int x,int y,int l,int r,int rt,int val) {
if(x<=l&&r<=y) {
sum[rt]+=val*(r-l+1);
lazy[rt]+=val;
return;
}
pushdown(rt,r-l+1);
int mid=(l+r)>>1;
if(l<=mid) update(x,y,l,mid,rt<<1,val);
if(r>mid) update(x,y,mid+1,r,rt<<1|1,val);
pushup(rt);
}
static long query(int x,int y,int l,int r,int rt) {
if(x<=l&&r<=y) return sum[rt];
pushdown(rt,r-l+1);
long ans=0;
int mid=(l+r)>>1;
if(x<=mid) ans=query(x,y,l,mid,rt<<1);
if(y>mid) ans+=query(x,y,mid+1,r,rt<<1|1);
return ans;
}
static void pushup(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
static void pushdown(int rt,int len) {
if(lazy[rt]!=0) {
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(len-len>>1);
sum[rt<<1|1]+=lazy[rt]*(len>>1);
lazy[rt]=0;
}
}
}
class InputReader{
public StringTokenizer tokenizer;
public BufferedReader reader;
public InputReader(InputStream stream) {
reader=new BufferedReader(new InputStreamReader(stream),32768);
tokenizer =null;
}
public String next() {
while(tokenizer==null||!tokenizer.hasMoreTokens()) {
try {
tokenizer=new StringTokenizer(reader.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}