We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
struct node{
int cnt, pr, info;
long long sum;
bool rev;
node *l, *r, *par;
node(){
cnt = 1;
pr = info = 0;
sum = 0;
rev = false;
l = r = par = 0;
}
};
typedef node* tree;
const int MAX_BUF = 2e6;
int szbuf;
node buf[MAX_BUF];
struct explicit_treap{
tree root;
explicit_treap(){
root = 0;
szbuf = 0;
}
int random(){
return ((rand() & ((1 << 15) - 1)) << 15) ^ rand();
}
tree allocate(int info){
tree cur = &buf[szbuf++];
cur->pr = random();
cur->info = info;
cur->sum = info;
return cur;
}
int get_cnt(tree t){
return !t ? 0 : t->cnt;
}
long long get_sum(tree t){
return !t ? 0 : t->sum;
}
void rev(tree t){
if(!t)
return;
t->rev ^= 1;
}
void push(tree t){
if(!t) return;
if(t->rev){
swap(t->l, t->r);
rev(t->l), rev(t->r);
t->rev ^= 1;
}
}
void update(tree t){
if(t){
t->cnt = get_cnt(t->l) + get_cnt(t->r) + 1;
t->sum = get_sum(t->l) + get_sum(t->r) + t->info;
t->par = 0;
}
}
void split(tree t, int key, tree& l, tree& r){
push(t);
if(!t){
l = r = 0;
return;
}
int ckey = get_cnt(t->l);
if(ckey < key){
split(t->r, key - ckey - 1, t->r, r);
l = t;
}else{
split(t->l, key, l, t->l);
r = t;
}
update(l), update(r);
}
tree merge(tree l, tree r){
push(l), push(r);
if(!l || !r)
return !l ? r : l;
tree t;
if(l->pr > r->pr){
l->r = merge(l->r, r);
t = l;
}else{
r->l = merge(l, r->l);
t = r;
}
update(t);
return t;
}
long long get_sum(int l, int r){
tree p1, p2, p3;
split(root, r + 1, p2, p3);
split(p2, l, p1, p2);
long long ans = get_sum(p2);
root = merge(p1, merge(p2, p3));
return ans;
}
void append(int number){
tree t = allocate(number);
root = merge(root, t);
}
};
void tswap(explicit_treap& t1, int l1, int r1,
explicit_treap& t2, int l2, int r2){
tree p1, p2, p3, q1, q2, q3;
Swaps and Sum
You are viewing a single comment's thread. Return to all comments →
c++
undef NDEBUG
ifdef ssu1
define _GLIBCXX_DEBUG
endif
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
using namespace std;
struct node{ int cnt, pr, info; long long sum; bool rev; node *l, *r, *par; node(){ cnt = 1; pr = info = 0; sum = 0; rev = false; l = r = par = 0; } };
typedef node* tree;
const int MAX_BUF = 2e6; int szbuf; node buf[MAX_BUF];
struct explicit_treap{
};
void tswap(explicit_treap& t1, int l1, int r1, explicit_treap& t2, int l2, int r2){ tree p1, p2, p3, q1, q2, q3;
}
int main() {
ifdef ssu1
endif
ifdef ssu1
endif
}