Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #define LOG(FMT...) fprintf(stderr, FMT)
- using namespace std;
- struct node {
- int x, cnt;
- bool lazy;
- node *ls, *rs;
- };
- const int N = 100010;
- int n, m;
- node* root;
- int get_cnt(node* p);
- node* build(int l, int r);
- void push_down(node* p);
- void update(node* p);
- void zig(node*& p);
- void zag(node*& p);
- void splay(node*& p, int x);
- void out(node* p);
- int main() {
- int l, r;
- scanf("%d%d", &n, &m);
- root = build(1, n);
- while (m--) {
- scanf("%d%d", &l, &r);
- if (l > 1) {
- splay(root, l - 1);
- if (r < n) {
- push_down(root);
- splay(root->rs, r - l + 2);
- root->rs->ls->lazy ^= true;
- } else
- root->rs->lazy ^= true;
- } else if (r < n) {
- splay(root, r + 1);
- root->ls->lazy ^= true;
- } else
- root->lazy ^= true;
- }
- out(root);
- putchar('\n');
- return 0;
- }
- void out(node* p) {
- static bool fst = true;
- if (!p)
- return;
- push_down(p);
- out(p->ls);
- if (fst) {
- printf("%d", p->x);
- fst = false;
- } else
- printf(" %d", p->x);
- out(p->rs);
- }
- node* build(int l, int r) {
- static node pool[N];
- static int cnt;
- if (r < l)
- return NULL;
- node* p = pool + cnt;
- ++cnt;
- int mid = (l + r) >> 1;
- p->x = mid;
- p->cnt = r - l + 1;
- p->ls = build(l, mid - 1);
- p->rs = build(mid + 1, r);
- return p;
- }
- inline int get_cnt(node* p)
- { return p ? p->cnt : 0; }
- inline void update(node* p)
- { p->cnt = 1 + get_cnt(p->ls) + get_cnt(p->rs); }
- inline void push_down(node* p) {
- if (!p->lazy)
- return;
- swap(p->ls, p->rs);
- if (p->ls)
- p->ls->lazy ^= true;
- if (p->rs)
- p->rs->lazy ^= true;
- p->lazy = false;
- }
- inline void zig(node*& p) {
- push_down(p);
- node* son = p->ls;
- push_down(son);
- p->ls = son->rs;
- son->rs = p;
- update(p);
- p = son;
- update(p);
- }
- inline void zag(node*& p) {
- push_down(p);
- node* son = p->rs;
- push_down(son);
- p->rs = son->ls;
- son->ls = p;
- update(p);
- p = son;
- update(p);
- }
- void splay(node*& p, int x) {
- push_down(p);
- if (x == get_cnt(p->ls) + 1)
- return;
- if (x <= get_cnt(p->ls)) {
- push_down(p->ls);
- if (x == get_cnt(p->ls->ls) + 1) {
- zig(p);
- return;
- }
- if (x <= get_cnt(p->ls->ls)) {
- splay(p->ls->ls, x);
- zig(p);
- zig(p);
- return;
- }
- splay(p->ls->rs, x - 1 - get_cnt(p->ls->ls));
- zag(p->ls);
- zig(p);
- return;
- }
- x -= 1 + get_cnt(p->ls);
- push_down(p->rs);
- if (x == get_cnt(p->rs->ls) + 1) {
- zag(p);
- return;
- }
- if (x <= get_cnt(p->rs->ls)) {
- splay(p->rs->ls, x);
- zig(p->rs);
- zag(p);
- return;
- }
- splay(p->rs->rs, x - 1 - get_cnt(p->rs->ls));
- zag(p);
- zag(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment