您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
输出一行n个数字,表示原始序列经过m次变换后的结果
N,M<=100000
平衡树
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> using namespace std; const int maxn = 1E5 + 10; int n,m,ans[maxn]; class data{ private: struct Node{ Node *ch[2]; int num,mark,siz; }*root,*tot,pool[maxn]; void maintain(Node *&x) { int s0 = (x->ch[0] == NULL)?0:x->ch[0]->siz; int s1 = (x->ch[1] == NULL)?0:x->ch[1]->siz; x->siz = s0+s1+1; } void rotate(Node *&x,int d) { Node *y = x->ch[d]; //y->fa = x->fa; x->ch[d] = y->ch[d^1]; //y->ch[d^1]->fa = x; y->ch[d^1] = x; //x->fa = y; maintain(x); x = y; maintain(x); } int Insert(Node *&x,int num) { int ret = 0; if (x == NULL) { x = ++tot; x->ch[0] = x->ch[1] = NULL; x->num = num; x->siz = 1; x->mark = 0; return 1; } int d = (x->num > num)?0:1; int s0 = (x->ch[0] == NULL)?0:x->ch[0]->siz; ret = s0+1; int sum = Insert(x->ch[d],num); //x->ch[d]->fa = x; maintain(x); return ret + sum; } void pushdown(Node *&x) { if (x->mark) { swap(x->ch[0],x->ch[1]); if (x->ch[0] != NULL) x->ch[0]->mark ^= 1; if (x->ch[1] != NULL) x->ch[1]->mark ^= 1; x->mark = 0; } } /*Node* find(Node *x,int rank) { pushdown(x); int s0 = (x->ch[0] == NULL)?0:x->ch[0]->siz; if (s0+1 == rank) return x; else if (s0+1 > rank) return find(x->ch[0],rank); else return find(x->ch[1],rank-s0-1); }*/ void splay(Node *&x,int rank) { pushdown(x); int s0 = (x->ch[0] == NULL)?0:x->ch[0]->siz; if (s0+1 != rank) { int d; if (s0+1 >= rank) d = 0; else d = 1,rank = rank - s0 - 1; pushdown(x->ch[d]); int s1 = (x->ch[d]->ch[0] == NULL)?0:x->ch[d]->ch[0]->siz; if (s1+1 != rank) { int d2; if (s1+1 >= rank) d2 = 0; else d2 = 1,rank = rank - s1 - 1; splay(x->ch[d]->ch[d2],rank); if (d == d2) rotate(x,d); else rotate(x->ch[d],d2); } rotate(x,d); } } void DFS(Node *x,int sum) { pushdown(x); int s0 = (x->ch[0] == NULL)?0:x->ch[0]->siz; ans[s0+1+sum] = x->num; if (x->ch[0] != NULL) DFS(x->ch[0],sum); if (x->ch[1] != NULL) DFS(x->ch[1],sum+s0+1); } public: data() { root = NULL; tot = pool; } int Ins(int num) { return Insert(root,num); } void setmark() { //pushdown(root); //pushdown(root->ch[1]); root->ch[1]->ch[0]->mark ^= 1; } void spl(int rank,int typ) { if (!typ) splay(root,rank); else { pushdown(root); int s0 = (root->ch[0] == NULL)?0:root->ch[0]->siz; splay(root->ch[1],rank-s0-1); } } void dfs() { DFS(root,0); } }; int main() { #ifdef YZY freopen("yzy.txt","r",stdin); #endif static data tree; cin >> n >> m; for (int i = 0; i <= n+1; i++) tree.spl(tree.Ins(i),0); //tree.dfs(); while (m--) { int l,r; scanf("%d%d",&l,&r); tree.spl(l,0); tree.spl(r+2,1); tree.setmark(); //tree.dfs(); } tree.dfs(); for (int i = 2; i <= n+1; i++) { printf("%d ",ans[i]); } return 0; }