-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReversals and Sums.cpp
168 lines (154 loc) · 4.03 KB
/
Reversals and Sums.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "bits/stdc++.h"
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef struct item * pitem;
struct item {
int prior, value, cnt;
LL sum;
bool rev;
item(int value):prior(rng()), value(value) {
cnt = 0;
rev = 0;
sum = value;
l = r = nullptr;
}
pitem l, r;
};
namespace Treap {
int cnt (pitem it) {
return it != nullptr? it->cnt : 0;
}
LL sum(pitem it) {
return it != nullptr? it->sum : 0;
}
void upd_cnt (pitem it) {
if (it!=nullptr) {
it->cnt = cnt(it->l) + cnt(it->r) + 1;
it->sum = sum(it->l) + sum(it->r) + it->value;
}
}
void push (pitem it) {
if (it != nullptr && it->rev == true) {
it->rev = false;
swap (it->l, it->r);
if (it->l) it->l->rev ^= true;
if (it->r) it->r->rev ^= true;
}
}
void merge (pitem & t, pitem l, pitem r) {
push (l);
push (r);
if (l==nullptr || r==nullptr)
t = (l!=nullptr) ? l : r;
else if (l->prior > r->prior)
merge (l->r, l->r, r), t = l;
else
merge (r->l, l, r->l), t = r;
upd_cnt (t);
}
void split (pitem t, pitem & l, pitem & r, int key, int add = 0) {
if (t==nullptr) {
l = r = nullptr;
return;
}
push(t);
int cur_key = add + cnt(t->l);
if (key <= cur_key)
split (t->l, l, t->l, key, add), r = t;
else
split (t->r, t->r, r, key, add + 1 + cnt(t->l)), l = t;
upd_cnt (t);
}
void reverse (pitem &t, int l, int r) {
pitem t1, t2, t3;
split (t, t1, t2, l); //< split t into t1 and t2
split (t2, t2, t3, r-l+1); //< split t2 into t2 and t3
// so we have now 3 tree t1,t2 and t3
assert(t2 != NULL);
t2->rev ^= true;
merge (t, t1, t2); // merge t1 and t2 into t
// so now we have t and t3
merge (t, t, t3); // merge t and t3 into t
}
void cut (pitem &t, int l, int r) {
pitem L, mid, R,temp;
split(t, L, mid, l );
split(mid, mid, R, r - l+1);
merge(t, L, R);
merge(t, t, mid);
}
LL query (pitem &t, int l, int r) {
pitem t1, t2, t3;
split (t, t1, t2, l);
split (t2, t2, t3, r-l+1);
LL ans = t2->sum;
merge (t, t1, t2);
merge (t, t, t3);
return ans;
}
void insert (pitem & t, int key, int value) {
pitem x = new item(value);
pitem L, R;
split(t, L, R, key);
merge(L, L, x);
merge(t, L, R);
upd_cnt(t);
}
int erase (pitem & t, int key) {
assert(cnt(t) > key);
pitem L, MID, R;
split(t, L, MID, key);
split(MID, MID, R, 1);
merge(t, L, R);
upd_cnt(t);
int rt = MID->value;
delete MID;
return rt;
}
void output (pitem t, string &v) {
if (t==nullptr) return;
push (t);
output (t->l, v);
v.push_back(t->value);
output (t->r, v);
}
void output2 (pitem t) {
if (t==nullptr) return;
push (t);
// cout << "(";
output2 (t->l);
cout << (t->value) << " ";
output2 (t->r);
// cout << ")";
}
}
int main(){
// ifstream cin("test_input.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// root = new node();
pitem root = nullptr;
int n,q;
cin>>n>>q;
int ara[n];
for(int i=0;i<n;i++){
cin>>ara[i];
Treap::insert(root,i,ara[i]);
}
while(q--){
int t,l,r;
cin>>t>>l>>r;
l--;r--;
if( t == 1 ) Treap::reverse(root,l,r);
else cout<<Treap::query(root,l,r)<<" ";
}
/* for(int i=1;i<=n;i++){
cout<<s[query(root,i,i)-1];
} cout<<endl;*/
}