1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using
namespace std;
typedef
long long
ll;
const
ll maxn = 200010;
struct
node{
ll key,lazy;
node *l,*r;
node(){
key = 0;lazy = 0; l = NULL; r = NULL;
}
}e[maxn*3];ll ne = 0;
node* build(ll l,ll r){
node* now = e + ne ++;
if (l ^ r){
ll mid = (l+r)>>1;
now->l = build(l,mid);
now->r = build(mid+1,r);
}
return
now;
}
void
pd(node* now,ll l,ll r){
if (now->l != 0){
ll mid = (l+r)>>1;
now->l->lazy += now->lazy; now->l->key += (mid-l+1)*now->lazy;
now->r->lazy += now->lazy; now->r->key += (r-mid)*now->lazy;
now->lazy = 0;
}
}
void
add(node* now,ll l,ll r,ll ls,ll rs,ll v){
if (ls == l && r == rs){
now->lazy += v;
now->key += (r-l+1)*v;
}
else {
ll mid = (l+r)>>1;
pd(now,l,r);
if (ls >= mid+1) add(now->r,mid+1,r,ls,rs,v);
else
if (rs <= mid) add(now->l,l,mid,ls,rs,v);
else
add(now->l,l,mid,ls,mid,v),add(now->r,mid+1,r,mid+1,rs,v);
now->key = now->l->key + now->r->key;
}
}
ll ask(node* now,ll l,ll r,ll ls,ll rs){
if (ls == l && rs == r) return
now->key;
else {
pd(now,l,r);
ll mid = (l+r)>>1;
if (ls >= mid+1) return
ask(now->r,mid+1,r,ls,rs);
else
if (rs <= mid) return
ask(now->l,l,mid,ls,rs);
else
return ask(now->l,l,mid,ls,mid)+ask(now->r,mid+1,r,mid+1,rs);
}
}
node* root;
ll n,m;
ll a[maxn];
void
test(node* now,ll l,ll r){
cout <<l << " " <<r<< " " <<now->key<< " " <<now->lazy<<endl;
if (l^r){
ll mid = (l+r)>>1;
test(now->l,l,mid);
test(now->r,mid+1,r);
}
}
int
main(){
freopen ( "cs.in" , "r" ,stdin);
scanf ( "%lld" ,&n);
root = build(1,n);
for (ll i = 1; i <= n; i++){
scanf ( "%lld" ,&a[i]);
add(root,1,n,i,i,a[i]);
}
scanf ( "%lld" ,&m);
while (m--){
ll temp = 0;
scanf ( "%lld" ,&temp);
if (temp == 1){
ll ls,rs,v;
scanf ( "%lld%lld%lld" ,&ls,&rs,&v);
add(root,1,n,ls,rs,v);
}
else
if (temp == 2){
ll ls,rs;
scanf ( "%lld%lld" ,&ls,&rs);
printf ( "%lld\n" ,ask(root,1,n,ls,rs));
}
}
return
0;
}
|