Académique Documents
Professionnel Documents
Culture Documents
// main.cpp
// practice
//
// Created by Mahmud on 01/24/18.
// Copyright © 2017 Mahmud. All rights reserved.
//
#include <iostream>
#include <ctime>
#include <climits>
#include <functional>
struct node {
int value;
int priority;
int subtreeSize;
bool reversed;
node *leftChild;
node *rightChild;
node () {
}
};
typedef node* tree;
int N, Q;
int low, high;
tree t = NULL;
int printed = 0;
void Print(tree t) {
if (!t) return;
Print(t->leftChild);
cout << t->value;
printed ++;
if (printed < N) cout << " ";
Print(t->rightChild);
}
int main() {
srand(unsigned(time(NULL)));
function<void(tree&, int low, int high)> Reverse = [](tree &t, int low, int
high) {
tree l, r;
Split(t, l, r, low - 1);
tree l1, r1;
Split(r, l1, r1, high - low + 1);
l1->reversed ^= true;
Merge(r, l1, r1);
Merge(t, l, r);
};
fastInput(N);
fastInput(Q);
for (int i = 1; i <= N; i ++) Insert(t, i, i);
while (Q --) {
fastInput(low);
fastInput(high);
Reverse(t, low, high);
}
Print(t); cout << endl;
return 0;
}