lightmain @ 2020-05-25 23:38:39
文艺平衡树模板题,改了几个小时,在本地过了,在洛谷上却是0分;我以前的AC代码能过
求助
#include <cstdio>
#include <algorithm>
#include <cstring>
#define pr printf
#define F(i, j, k) for(register int i = j, kkllkl = k; i <= kkllkl; ++i)
#define G(i, j, k) for(register int i = j, kkllkl = k; i >= kkllkl; --i)
#define clr(a, v) memset((a), v, sizeof(a))
using namespace std;
typedef long long ll;
#define isd(x) (('0' <= (x) && (x) <= '9') || (x) == '-')
int rd() {
int ans = 0, sign = 1; char c = getchar();
while(!isd(c)) c = getchar();
if(c == '-') sign = -1, c = getchar();
while(isd(c)) ans = (ans << 3) + (ans << 1) + c - '0', c = getchar();
return sign == 1 ? ans : -ans;
}
/* --------------------------CSYZ1921------------------------- */
const int N = 100005, INF = 0x3f3f3f3f;
int n, m;
struct Splay {
#define ls (ch[u][0])
#define rs (ch[u][1])
int fa[N], ch[N][2], val[N], cnt[N], sz[N], tot, root;
bool mrk[N];
void build() {
tot = 0;
}
#define isw(x) ((x) == ch[fa[x]][1])
void resz(int u) {
sz[u] = cnt[u];
if(ls) sz[u] += sz[ls];
if(rs) sz[u] += sz[rs];
}
void pd(int u) {
if(mrk[u]) {
mrk[u] = false;
swap(ls, rs);
if(ls) mrk[ls] ^= true;
if(rs) mrk[rs] ^= true;
}
}
void rotate(int u) {
int f = fa[u], g = fa[f];
int fs = isw(f), us = isw(u), ch2 = ch[u][us ^ 1];
fa[f] = u; ch[u][us ^ 1] = f;
ch[f][us] = ch2; if(ch2) fa[ch2] = f;
if(g) ch[g][fs] = u; fa[u] = g;
resz(f); resz(u);
}
void splay(int u, int t = 0) {
for(int v; (v = fa[u]) != t && (v = fa[u]); rotate(u))
if(fa[v] && fa[v] != t)
rotate(sz[u] == sz[v] ? v : u);
if(t == 0) root = u;
}
void create(int u, int f, int x) {
if(f) ch[f][x > val[f]] = u, fa[u] = f;
else root = u;
cnt[u] = sz[u] = 1;
val[u] = x;
}
void insert(int x) {
if(root == 0) {
create(++tot, root, x);
return ;
}
int u = root;
while(1) {
++sz[u];
int v = ch[u][x > val[u]];
if(!v) {
create(++tot, u, x);
splay(tot);
return ;
} else u = v;
}
}
int kth(int k) {
int u = root;
while(u) {
pd(u);
int lsz = ls ? sz[ls] : 0;
int l = lsz + 1;
if(k < l) {
u = ls;
} else if(l == k) {
return u;
} else {
u = rs;
k -= l;
}
}
}
int Swap(int l, int r) {
int L = kth(l);
int R = kth(r + 2);
splay(L); splay(R, L);
mrk[ch[R][0]] ^= true;
}
void dfs(int u) {
pd(u);
if(ls) dfs(ls);
if(1 <= val[u] && val[u] <= n) pr("%d ", val[u]);
if(rs) dfs(rs);
}
} sp;
int main() {
// freopen("P3391_1.in", "r", stdin);
// freopen("P3391_1_2.out", "w", stdout);
n = rd(); m = rd();
sp.insert(-INF);
sp.insert(INF);
F(i, 1, n)
sp.insert(i);
F(i, 1, m) {
int l = rd(), r = rd();
if(l < r) sp.Swap(l, r);
}
sp.dfs(sp.root);
return 0;
}
/*
5 3
1 3
1 3
1 4
*/
by 可爱 @ 2020-05-26 10:57:06
@gdjcwsk smg啊,怎么就建议删帖了……
by 一只书虫仔 @ 2020-05-26 11:03:09
@gdjcwsk ?
by 一只书虫仔 @ 2020-05-26 11:03:26
@gdjcwsk 求助帖怎么了,和洛谷全站帮助有什么关系?
by 一只书虫仔 @ 2020-05-26 11:04:26
@gdjcwsk 为什么建议删帖啊
by 一只书虫仔 @ 2020-05-26 11:04:46
@gdjcwsk 您自己把您的回答删了吧
by 一只书虫仔 @ 2020-05-26 11:05:56
@lightmain 是不是你谷评测姬卡炸或者数据加强了啊 qwq
by 可爱 @ 2020-05-26 11:09:53
@gdjcwsk xswl
by zhanghengrui @ 2020-05-26 11:21:25
@lightmain
void Swap(int l, int r)
by lightmain @ 2020-05-26 11:37:00
Swap是库函数吗
by lightmain @ 2020-05-26 11:37:50
@zhanghengrui 但是我的那份AC代码用的也是void Swap(int l,int r)
...