-
Notifications
You must be signed in to change notification settings - Fork 0
/
17408.cpp
95 lines (73 loc) · 2.35 KB
/
17408.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
#include<iostream>
#include<vector>
#include<algorithm>
#define N 100005
using namespace std;
int n, arr[N], a1, a2;
vector<int> st[N*3];
vector<int> make_tree(int left, int right, int index)
{
if(left==right) return st[index] = vector<int>{arr[left], 0};
int mid=(left+right)/2;
make_tree(left, mid, index*2);
make_tree(mid+1, right, index*2+1);
reverse(st[index*2].begin(), st[index*2].end());
reverse(st[index*2+1].begin(), st[index*2+1].end());
merge(st[index*2].begin(), st[index*2].end(), st[index*2+1].begin(), st[index*2+1].end(), back_inserter(st[index]));
reverse(st[index].begin(), st[index].end());
st[index].resize(2);
reverse(st[index*2].begin(), st[index*2].end());
reverse(st[index*2+1].begin(), st[index*2+1].end());
return st[index];
}
vector<int> update_tree(int left, int right, int index)
{
if(a1<left || right<a1) return st[index];
if(left==right) return st[index] = vector<int>{a2, 0};
int mid=(left+right)/2;
vector<int> l = update_tree(left, mid, index*2);
vector<int> r = update_tree(mid+1, right, index*2+1);
reverse(l.begin(), l.end());
reverse(r.begin(), r.end());
st[index] = vector<int>();
merge(l.begin(), l.end(), r.begin(), r.end(), back_inserter(st[index]));
reverse(st[index].begin(), st[index].end());
st[index].resize(2);
return st[index];
}
vector<int> get_tree(int left, int right, int index)
{
if(a2<left || right<a1) return vector<int>();
if(a1<=left && right<=a2) return st[index];
int mid=(left+right)/2;
vector<int> l = get_tree(left, mid, index*2);
vector<int> r = get_tree(mid+1, right, index*2+1);
reverse(l.begin(), l.end());
reverse(r.begin(), r.end());
vector<int> ret;
merge(l.begin(), l.end(), r.begin(), r.end(), back_inserter(ret));
reverse(ret.begin(), ret.end());
ret.resize(2);
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int m, c, i;
cin >> n;
for(i=1; i<=n; i++) cin >> arr[i];
make_tree(1, n, 1);
cin >> m;
for(i=1; i<=m; i++){
cin >> c >> a1 >> a2;
if(c==1) update_tree(1, n, 1);
if(c==2){
vector<int> res = get_tree(1, n, 1);
printf("%d\n", res[0]+res[1]);
}
}
return 0;
}