MYF

POJ 3468 A Simple Problem with Integers

题目链接

POJ 3468

方法:线段树(区间加值)

题目分析

题目大意

模版题,给定每个点的值,然后区间加上每个点都加上一个数,区间查值

解析

注意要用long long

代码

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
102
103
104
105
106
107
108
109
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, n) for (int i = 1; i <= n; i++)
#define ForK(i, k, n) for (int i = k; i <= n; i++)
#define ForD(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, n) for (int i = 0; i < n; i++)
#define RepD(i, n) for (int i = n; i >= 0; i--)
#define MemI(a) memset(a, 0, sizeof(a))
#define MemC(a) memset(a, '\0', sizeof(a))
#define vset(a,val) memset(a,val,sizeof(a))
#define pf printf
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define PI acos(-1)
#define bug pf("Hi----------\n")
#define eps 1e-8
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long ll;
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int NV = 100005;
ll add[NV<<2],sum[NV<<2];
void PushUp(ll rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(ll rt,ll 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(ll l,ll r,ll rt=1)
{
add[rt] = 0;
if (l == r)
{
scanf("%lld",&sum[rt]);

return ;
}
ll m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(ll L/*子区间*/,ll R/*子区间*/,ll c,ll l/*全区间*/,ll r/*全区间*/,ll rt=1)
{
if (L <= l && r <= R)
{
add[rt] += c;
sum[rt] += c * (r - l + 1);
return ;
}
PushDown(rt , r - l + 1);
ll m = (l + r) >> 1;
if (L <= m) update(L , R , c , lson);
if (m < R) update(L , R , c , rson);
PushUp(rt);
}
ll query(ll L/*子区间*/,ll R/*子区间*/,ll l/*全区间*/,ll r/*全区间*/,ll rt=1)
{
if (L <= l && r <= R)
{
return sum[rt];
}
PushDown(rt , r - l + 1);
ll m = (l + r) >> 1;
ll ret = 0;
if (L <= m) ret += query(L , R , lson);
if (m < R) ret += query(L , R , rson);
return ret;

}
int main(){
ll n,q,a,b,c,ans;
scanf("%lld %lld",&n,&q);
char s[2];
build(1, n, 1);
for (ll i=0; i<q; i++) {
scanf("%s",s);
if (s[0]=='Q') {
scanf("%lld %lld",&a,&b);
cout<<query( a, b, 1, n)<<endl;
}
else{
scanf("%lld %lld %lld",&a,&b,&c);
update(a, b, c, 1, n);
}
}
}