关于C++UB问题与O2
  • 板块学术版
  • 楼主Starlight237
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/2/13 23:32
  • 上次更新2023/11/5 03:18:24
查看原帖
关于C++UB问题与O2
75765
Starlight237楼主2021/2/13 23:32

P2042,实在找不出来哪里有UB了,但是开O2能AC,关掉就WA了。

求教。

#include<bits/stdc++.h>
using namespace std;
#define reg register
#define ll long long
const int N = 5e5 + 10;
struct Node {
	int siz, v, s, lx, rx, ms, tag, rev, fa, ch[2];
	Node () {ch[0] = ch[1] = 0;}
	Node (int siz, int v, int s, int lx, int rx, int ms, int fa, int ls, int rs) :
		siz(siz), v(v), s(s), lx(lx), rx(rx), ms(ms), fa(fa) {ch[0] = ls, ch[1] = rs;}
} spl[N];
int root, buc[N], *ptr = buc, n, m, id[N], a[N];
inline void pushup(int x) {
	reg int ls = spl[x].ch[0], rs = spl[x].ch[1];
	spl[x].siz = spl[ls].siz + spl[rs].siz + 1,
	spl[x].s = spl[ls].s + spl[rs].s + spl[x].v,
	spl[x].lx = max(spl[ls].lx, spl[ls].s + spl[x].v + spl[rs].lx),
	spl[x].rx = max(spl[rs].rx, spl[ls].rx + spl[x].v + spl[rs].s),
	spl[x].ms = max(max(spl[ls].ms, spl[rs].ms), spl[ls].rx + spl[x].v + spl[rs].lx);
}
inline void pushdown(int k) {
	reg Node &ls = spl[spl[k].ch[0]], &rs = spl[spl[k].ch[1]];
	spl[k].tag && (
		spl[k].rev = spl[k].tag = 0,
		&ls != spl && (ls.tag = 1, ls.v = spl[k].v, ls.s = spl[k].v * ls.siz),
		&rs != spl && (rs.tag = 1, rs.v = spl[k].v, rs.s = spl[k].v * rs.siz),
		spl[k].v >= 0 ?
			&ls != spl && (ls.lx = ls.rx = ls.ms = ls.s),
			&rs != spl && (rs.lx = rs.rx = rs.ms = rs.s)
		: (
			&ls != spl && (ls.lx = ls.rx = 0, ls.ms = spl[k].v),
			&rs != spl && (rs.lx = rs.rx = 0, rs.ms = spl[k].v)
		), 0
	);
	spl[k].rev && (
		spl[k].rev = 0, ls.rev ^= 1, rs.rev ^= 1,
		swap(ls.lx, ls.rx), swap(rs.lx, rs.rx),
		swap(ls.ch[0], ls.ch[1]), swap(rs.ch[0], rs.ch[1]), 0
	);
}
#define connect(x, y, son) (spl[x].fa = y, spl[y].ch[son] = x)
inline void rotate(int x) {
	reg int y = spl[x].fa, z = spl[y].fa, k = spl[y].ch[1] == x;
	connect(spl[x].ch[k ^ 1], y, k), connect(y, x, k ^ 1),
	z ? (connect(x, z, spl[z].ch[1] == y), 0) : (spl[x].fa = z);
	pushup(y), pushup(x);
}
inline void splay(int x, int g) {
	for (reg int y, z; spl[x].fa != g; rotate(x))
		y = spl[x].fa, z = spl[y].fa,
		z != g && (rotate((spl[y].ch[0] == x) & (spl[z].ch[0] == y) ? x : y), 0);
	g == 0 && (root = x);
}
void recyc(int x) {
	if (spl[x].ch[0]) recyc(spl[x].ch[0]);
	if (spl[x].ch[1]) recyc(spl[x].ch[1]);
	*ptr-- = x;
	spl[x] = Node();
}
inline int kth(int k) {
	reg int x = root, y;
	if (spl[x].siz < k) return 0;
	for (;;) {
		pushdown(x);
		y = spl[x].ch[0];
		if (spl[y].siz + 1 < k) {k -= spl[y].siz + 1, x = spl[x].ch[1]; continue;}
		if (spl[y].siz >= k) {x = y; continue;}
		return x;
	}
}
inline int query(int l, int r) {
	reg int L = kth(l - 1), R = kth(r + 1);
	splay(L, 0), splay(R, L);
	return spl[spl[R].ch[0]].s;
}
inline int qryms(int l, int r) {
	reg int L = kth(l - 1), R = kth(r + 1);
	splay(L, 0), splay(R, L);
	return spl[spl[R].ch[0]].ms;
}
inline void modify(int l, int r, int val) {
	reg int L = kth(l - 1), R = kth(r + 1);
	splay(L, 0), splay(R, L);
	reg int k = spl[R].ch[0];
	spl[k].v = val, spl[k].tag = 1, spl[k].s = val * spl[k].siz;
	if (val >= 0) spl[k].lx = spl[k].rx = spl[k].ms = spl[k].s;
	else spl[k].lx = spl[k].rx = 0, spl[k].ms = val;
	pushup(R), pushup(L);
}
inline void rever(int l, int r) {
	reg int L = kth(l - 1), R = kth(r + 1);
	splay(L, 0), splay(R, L);
	reg int x = spl[R].ch[0];
	spl[x].tag || (
		spl[x].rev ^= 1,
		swap(spl[x].lx, spl[x].rx),
		swap(spl[x].ch[0], spl[x].ch[1]),
		pushup(R), pushup(L), 0
	);
}
inline void erase(int l, int r) {
	reg int L = kth(l - 1), R = kth(r + 1);
	splay(L, 0), splay(R, L);
	reg int x = spl[R].ch[0];
	recyc(x), spl[R].ch[0] = 0,
	pushup(R), pushup(L);
}
void build(int f, int l, int r) {
	int mid = l + r >> 1, now = id[mid], pre = id[f];
	if (l == r) {
		spl[now].ms = spl[now].s = a[l],
		spl[now].tag = spl[now].rev = 0,
		spl[now].lx = spl[now].rx = max(a[l], 0),
		spl[now].siz = 1;
	}
	l < mid && (build(mid, l, mid - 1), 0);
	mid < r && (build(mid, mid + 1, r), 0);
	spl[now].v = a[mid], spl[now].fa = pre;
	pushup(now);
	spl[pre].ch[mid >= f] = now;
}
void ins(int k, int tot) {
	for (reg int i = 1; i <= tot; ++i) scanf("%d", a + i);
	for (reg int i = 1; i <= tot; ++i) id[i] = *++ptr;
	build(0, 1, tot);
	reg int z = id[1 + tot >> 1], L = kth(k + 1), R = kth(k + 2);
	splay(L, 0), splay(R, L);
	connect(z, R, 0);
	pushup(R), pushup(L);
}
int main() {
	memset(spl, 0, sizeof spl), memset(buc, 0, sizeof buc);
	for (reg int i = 0; i < N; ++i) buc[i] = i;
	scanf("%d%d", &n, &m);
	spl[0].ms = a[1] = a[n + 2] = -0x3f3f3f3f;
	for (reg int i = 1; i <= n; ++i) scanf("%d", a + i + 1);
	for (reg int i = 1; i <= n + 2; ++i) id[i] = *++ptr;
	build(0, 1, n + 2);
	root = n + 3 >> 1;
	reg int k, tot, v;
	for (; m; --m) {
		char str[10]; scanf("%s", str);
		if (str[2] != 'X') scanf("%d%d", &k, &tot);
		switch(str[2]) {
			case 'S' :
				ins(k, tot);
				break;
			case 'L' :
				erase(k + 1, k + tot);
				break;
			case 'K' :
				scanf("%d", &v);
				modify(k + 1, k + tot, v);
				break;
			case 'V' :
				rever(k + 1, k + tot);
				break;
			case 'T' :
				printf("%d\n", query(k + 1, k + tot));
				break;
			case 'X' :
				printf("%d\n", spl[root].ms);
				break;
		}
	}
	return 0;
}
2021/2/13 23:32
加载中...