10 5 0 0 0 0 0 0 0 0 0 0 1 5 1 2 6 1 3 7 1 4 9 1 5 10 1 1 1 1 1 1 2
1 2 3 4 5 4 3 2 2 1 3
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
const int maxn = 1000000;
int add[maxn<<2];
int sum[maxn<<2];
int aa[1000000];
void PushUp(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m){
if (add[rt]){
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
sum[rt<<1] += add[rt] * (m - (m >> 1));
sum[rt<<1|1] += add[rt] * (m >> 1);
add[rt] = 0;
}
}
void build(int l,int r,int rt){
add[rt] = 0;
if (l == r){
sum[rt]=0;
// scanf("%lld",&sum[rt]);
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
if (L <= l && r <= R){
add[rt] += c;
sum[rt] += (LL)c * (r - l + 1);
return ;
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L , R , c , lson);
if (m < R) update(L , R , c , rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){
if (L <= l && r <= R){
return sum[rt];
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
int ret = 0;
if (L <= m) ret += query(L , R , lson);
if (m < R) ret += query(L , R , rson);
return ret;
}
int main(){
int N , M;
int i,j;
int a,b,c;
while(~scanf("%d%d",&N,&M)){
build(1 , N , 1);
for(i=1; i<=N; i++){
scanf("%d",&aa[i]);
}
for(i=1; i<=M; i++){
scanf("%d%d%d",&a,&b,&c);
if(a==0){
a+=1;
}
if(b==0){
b+=1;
}
update(min(a,b),max(b,a),c,1,N,1);
}
if(N==1){
printf("%d\n",aa[1]+query(1,1,1,N,1));
}
else{
for(i=1; i<=N; i++){
if(i!=N){
printf("%d ",aa[i]+query(i,i,1,N,1));
}
else{
printf("%d\n",aa[i]+query(i,i,1,N,1));
}
}
}
}
return 0;
}
原文:http://www.cnblogs.com/yinghualuowu/p/5003110.html