好像我写麻烦了, 其实只用线段树维护区间每个数反转, 区间赋值就可以。
#include<bits/stdc++.h> using namespace std; const int N = (int)1e5 + 7; int n, q, pos, a[N]; char op[N]; struct Bit { int a[N]; void modify(int x, int v) { for(int i = x; i < N; i += i & -i) { a[i] += v; } } int query(int x) { int ans = 0; for(int i = x; i; i -= i & -i) { ans += a[i]; } return ans; } } bit; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 struct SegmentTree { int a[N << 2], lazy[N << 2], flip[N << 2]; inline void push(int rt) { if(lazy[rt]) { a[rt << 1] = lazy[rt]; a[rt << 1 | 1] = lazy[rt]; lazy[rt << 1] = lazy[rt]; flip[rt << 1] = 0; lazy[rt << 1 | 1] = lazy[rt]; flip[rt << 1 | 1] = 0; lazy[rt] = 0; } if(flip[rt]) { a[rt << 1] = -a[rt << 1]; a[rt << 1 | 1] = -a[rt << 1 | 1]; if(lazy[rt << 1]) lazy[rt << 1] = -lazy[rt << 1]; else flip[rt << 1] ^= 1; if(lazy[rt << 1 | 1]) lazy[rt << 1 | 1] = -lazy[rt << 1 | 1]; else flip[rt << 1 | 1] ^= 1; flip[rt] = 0; } } void update(int L, int R, int val, int l, int r, int rt) { if(R < l || r < L || R < L) return; if(L <= l && r <= R) { a[rt] = val; lazy[rt] = val; flip[rt] = 0; return; } push(rt); int mid = l + r >> 1; update(L, R, val, lson); update(L, R, val, rson); } void update2(int L, int R, int l, int r, int rt) { if(R < l || r < L || R < L) return; if(L <= l && r <= R) { a[rt] = -a[rt]; if(lazy[rt]) lazy[rt] = -lazy[rt]; else flip[rt] ^= 1; return; } push(rt); int mid = l + r >> 1; update2(L, R, lson); update2(L, R, rson); } int query(int p, int l, int r, int rt) { if(l == r) return a[rt]; push(rt); int mid = l + r >> 1; if(p <= mid) return query(p, lson); else return query(p, rson); } } Tree; void show() { for(int i = 1; i <= n; i++) { int ans = 0; if(a[i] != 0) { if(abs(a[i]) > pos) ans = abs(a[i]) * Tree.query(abs(a[i]), 0, 100000, 1); else ans = (bit.query(abs(a[i])) & 1) ? -a[i] : a[i]; } printf("%d%c", ans, " "[i == n]); } } int main() { scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); pos = 100000; while(q--) { int x; scanf("%s%d", op, &x); if(*op == '>') { if(x >= 0) { pos = min(pos, x); Tree.update(x + 1, 100000, -1, 0, 100000, 1); } else { pos = min(pos, -x - 1); Tree.update(-x, 100000, -1, 0, 100000, 1); Tree.update2(1, -x - 1, 0, 100000, 1); bit.modify(1, 1); bit.modify(-x, -1); } } else { if(x <= 0) { pos = min(pos, -x); Tree.update(-x + 1, 100000, 1, 0, 100000, 1); } else { pos = min(pos, x - 1); Tree.update(x, 100000, 1, 0, 100000, 1); Tree.update2(1, x - 1, 0, 100000, 1); bit.modify(1, 1); bit.modify(x, -1); } } } show(); return 0; } /* */