这个题弄了很久,原来一直是那个标记下放那里有个地方没有下放导致一直TLE /* ID: mnlm1991 PROG: hdoj 3487 play with the chain LANG: C++ */ #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<vector> #include<algorithm> #include<string> #include<map> #include<set> #include<stack> #include<bitset> #include<queue> #include<iostream> using namespace std; const int MaxN = 300010; const int oo = 1000000001; typedef int DataType; int N; int M; int father[MaxN]; int son[MaxN][2]; int size[MaxN]; int root; bool flag[MaxN]; DataType data[MaxN]; int nodecnt; void Init() { memset(flag, 0, sizeof(flag)); size[0] = 0; data[1] = -oo; father[1] = 0; son[1][0] = 0; son[1][1] = 2; size[1] = 2; data[2] = oo; father[2] = 1; son[2][0] = son[2][1] = 0; size[2] = 1; root = 1; nodecnt = 3; return; } void Reserve(int x) { if (flag[x]) { flag[x] = false; swap(son[x][0], son[x][1]); if (son[x][0]) { flag[son[x][0]] ^= 1; } if (son[x][1]) { flag[son[x][1]] ^= 1; } } return; } void Updata(int x) { size[x] = size[son[x][0]] + size[son[x][1]] + 1; } void Rotate(int x, bool w) { int y = father[x]; Reserve(y); Reserve(x); son[y][!w] = son[x][w]; if (son[x][w] != 0) { father[son[x][w]] = y; } father[x] = father[y]; if (father[y] != 0) { if (son[father[y]][0] == y) { son[father[y]][0] = x; } else { son[father[y]][1] = x; } } father[y] = x; son[x][w] = y; Updata(y); return; } void Splay(int x, int y) { Reserve(x); while (father[x] != y) { if (father[father[x]] == y) { Rotate(x, x == son[father[x]][0]); } else { if (father[x] == son[father[father[x]]][0]) { if (x == son[father[x]][0]) { Rotate(father[x], 1); Rotate(x, 1); } else { Rotate(x, 0); Rotate(x, 1); } } else { if (x == son[father[x]][1]) { Rotate(father[x], 0); Rotate(x, 0); } else { Rotate(x, 1); Rotate(x, 0); } } } } Updata(x); if (0 == y) { root = x; } return; } void Insert(DataType v) { int x = root; while (1) { size[x]++; if (v < data[x]) { if (son[x][0] == 0) { son[x][0] = nodecnt; break; } else { x = son[x][0]; } } else { if (son[x][1] == 0) { son[x][1] = nodecnt; break; } else { x = son[x][1]; } } } data[nodecnt] = v; size[nodecnt] = 1; father[nodecnt] = x; son[nodecnt][0] = son[nodecnt][1] = 0; nodecnt++; Splay(x, 0); return; } int Getk(int v) { int x = root; while (Reserve(x), size[son[x][0]] + 1 != v) { if (v <= size[son[x][0]]) { x = son[x][0]; } else { v -= size[son[x][0]] + 1; x = son[x][1]; } } Splay(x, 0); return x; } void Cut(int l, int r, int ll) { l = Getk(l - 1); r = Getk(r + 1); Splay(l, 0); Splay(r, l); int x = son[r][0]; size[l] -= size[x]; size[r] -= size[x]; son[r][0] = 0; ll = Getk(ll); Splay(ll, 0); int z = son[ll][1]; while (Reserve(z), son[z][0] != 0) { z = son[z][0]; } Splay(z, ll); size[z] += size[x]; size[ll] += size[x]; son[z][0] = x; father[x] = z; return; } void Flip(int l, int r) { l = Getk(l - 1); r = Getk(r + 1); Splay(l, 0); Splay(r, l); flag[son[r][0]] ^= 1; return; } int cnt; stack <int> s; void PrintAns(int x) { while (!s.empty()) { s.pop(); } int p = root; while (p || !s.empty()) { if (p) { Reserve(p); s.push(p); p = son[p][0]; } else { p = s.top(); s.pop(); if (cnt > 0 && cnt <= N) { printf("%d%c", data[p], cnt == N ? '/n' : ' '); } cnt++; p = son[p][1]; } } /* Reserve(x); if (son[x][0] != 0) { PrintAns(son[x][0]); } if (cnt > 0 && cnt <= N) { printf("%d%c", data[x], cnt == N ? '/n' : ' '); } cnt++; if (son[x][1] != 0) { PrintAns(son[x][1]); }*/ return; } int main() { // freopen("hdoj-3487.in", "r", stdin); // freopen("hdoj-3487.out", "w", stdout); char op[10]; int l, r, ll; while (scanf("%d%d", &N, &M), N >= 0 || M >= 0) { Init(); int i; int j; for (i = 1; i <= N; i++) { Insert(i); } while (M--) { scanf("%s", op); if (op[0] == 'C') { scanf("%d%d%d", &l, &r, &ll); Cut(l + 1, r + 1, ll + 1); } else { scanf("%d%d", &l, &r); Flip(l + 1, r + 1); } } cnt = 0; PrintAns(root); } return 0; }