-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPOJ_3468_A Simple Problem with Integers_区间树.cpp
120 lines (113 loc) · 2.9 KB
/
POJ_3468_A Simple Problem with Integers_区间树.cpp
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
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <cstdio>
using namespace std;
#define LL long long
typedef struct interval_tree{
int low,high;
LL sum;//存储当前子树结点值之和
LL mark;//当前区间标记
interval_tree *left,*right;
}interval_tree,*pInterval_tree;
void create(pInterval_tree &pit,LL *&height,int low,int high,int &index)//创建区间树
{
pit=new interval_tree;
pit->low=low,pit->high=high;
pit->left=NULL,pit->right=NULL;
pit->mark=0;//标记初始化为0
if(low!=high){
create(pit->left,height,low,(low+high)>>1,index);
create(pit->right,height,((low+high)>>1)+1,high,index);
pit->sum=pit->left->sum+pit->right->sum;
}else{
pit->sum=height[index];
++index;
}
}
void modify(pInterval_tree &pit,int low,int high,int mark)//修改结点值
{
if(pit->low<=high && pit->high>=low){
if(pit->low==low && pit->high==high){
pit->sum+=(pit->high-pit->low+1)*mark;
pit->mark+=mark;
}else{
if(pit->mark){
pit->left->mark+=pit->mark;
pit->left->sum+=(pit->left->high-pit->left->low+1)*pit->mark;
pit->right->mark+=pit->mark;
pit->right->sum+=(pit->right->high-pit->right->low+1)*pit->mark;
pit->mark=0;
}
if(high<=((pit->low+pit->high)>>1)){
modify(pit->left,low,high,mark);
}else if(low>=((pit->low+pit->high)>>1)+1){
modify(pit->right,low,high,mark);
}else{
modify(pit->left,low,(pit->low+pit->high)>>1,mark);
modify(pit->right,((pit->low+pit->high)>>1)+1,high,mark);
}
pit->sum=pit->left->sum+pit->right->sum;
}
}
}
void query(pInterval_tree &pit,int low,int high,LL &sum)//查询区间树
{
if(pit->low<=high && pit->high>=low){//pit所表示的区间和[low,high]区间相交
if(pit->low==low && pit->high==high){
sum+=pit->sum;
}else{
if(pit->mark){
pit->left->mark+=pit->mark;
pit->left->sum+=(pit->left->high-pit->left->low+1)*pit->mark;
pit->right->mark+=pit->mark;
pit->right->sum+=(pit->right->high-pit->right->low+1)*pit->mark;
pit->mark=0;
}
if(high<=((pit->low+pit->high)>>1)){
query(pit->left,low,high,sum);
}else if(low>=((pit->low+pit->high)>>1)+1){
query(pit->right,low,high,sum);
}else{
query(pit->left,low,(pit->low+pit->high)>>1,sum);
query(pit->right,((pit->low+pit->high)>>1)+1,high,sum);
}
}
}
}
void del(pInterval_tree &pit,int low,int high)
{
if(low!=high){
del(pit->left,low,(low+high)>>1);
del(pit->right,((low+high)>>1)+1,high);
}else{
delete pit;
}
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF){
LL *height=new LL[n];
for(int i=0;i<n;++i){
scanf("%I64d",&height[i]);
}
pInterval_tree pit=NULL;
int index=0;
create(pit,height,1,n,index);
int low,high,mark;
char op;
for(int i=0;i<q;++i){
scanf("\n%c%d%d",&op,&low,&high);
if('C'==op){
scanf("%d",&mark);
modify(pit,low,high,mark);
}else{
LL sum=0;
query(pit,low,high,sum);
printf("%I64d\n",sum);
}
}
delete[]height;
del(pit,1,n);
}
return 0;
}