https://ac.nowcoder.com/acm/contest/317/J

C++版本一

std

题解:线段树 set

 

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second 
using namespace std;
const int MAXN = 1e6 + 10, INF = 2147483646;
inline int read() {
    char c = getchar(); int x = 0, f = 1; 
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M;
map<int, int> id;
int tot;
#define ls k << 1
#define rs k << 1 | 1
struct Node {
    int l, r, s, f, siz;
}T[MAXN];
void update(int k) {
    T[k].s = T[ls].s ^ T[rs].s;
}
void ps(int k, int val) {
    if(T[k].siz & 1) T[k].s ^= val; 
    T[k].f ^= val;
}
void pushdown(int k) {
    if(!T[k].f) return ;
    ps(ls, T[k].f); ps(rs, T[k].f);
    T[k].f = 0;
}
void Build(int k, int ll, int rr) {
    T[k].l = ll; T[k].r = rr; T[k].siz = rr - ll + 1; T[k].s = 0;
    if(ll == rr) {T[k].s = 0; return ;}
    int mid = (T[k].l + T[k].r) >> 1;
    Build(ls, ll, mid); Build(rs, mid + 1, rr);
}
void IntXor(int k, int ll, int rr, int val) {
    if(ll <= T[k].l && T[k].r <= rr) {ps(k, val); return ;}
    pushdown(k);
    int mid = (T[k].l + T[k].r) >> 1;
    if(ll <= mid) IntXor(ls, ll, rr, val);
    if(rr  > mid) IntXor(rs, ll, rr, val);
    update(k);
}
int Query(int k, int ll, int rr) {
    int ans = 0;
    if(ll <= T[k].l && T[k].r <= rr) return T[k].s;
    pushdown(k);
    int mid = (T[k].l + T[k].r) >> 1;
    if(ll <= mid) ans ^= Query(ls, ll, rr);
    if(rr  > mid) ans ^= Query(rs, ll, rr);
    return ans;
}
set<Pair> S[MAXN];
#define sit set<Pair>::iterator 
void Add(int l, int r, int x) {
    set<Pair> &s = S[id[x]]; 
    sit it = s.lower_bound(MP(l, r));
    if(it != s.begin()) {
        it--;
        if(it -> se > r) return ;
        if(it -> se >= l ) {
            l = min(l, it -> fi); r = max(r, it -> se);
            IntXor(1, it -> fi, it -> se, x);
            s.erase(it++);
        }
    }
    it = s.lower_bound(MP(l, r));
    while((it -> se >= l && it -> se <= r) || (it -> fi >= l && it -> fi <= r)) {
        l = min(l, it -> fi); r = max(r, it -> se);
        IntXor(1, it -> fi, it -> se, x);
        s.erase(it++);
    }
    IntXor(1, l, r, x);
    s.insert(MP(l, r));
}
void Delet(int l, int r, int x) {
    if(!id[x]) return ;
    Add(l, r, x); 
    IntXor(1, l, r, x);
    set<Pair> &s = S[id[x]]; 	
    sit it = s.lower_bound(MP(l, r));
    if(it -> fi > l) it--;
//	printf("%d %d\n", it->fi, it->se);
    if(it -> fi <= l - 1) s.insert(MP(it -> fi, l - 1));
    if(r + 1 <= it -> se) s.insert(MP(r + 1, it -> se));
    s.erase(it);
}
signed main() {
//	freopen("a.in", "r", stdin); freopen("c.out", "w", stdout);
    N = read(); M = read();
    Build(1, 1, N);
    for(int i = 1; i <= N; i++) S[i].insert(MP(INF, INF)); 
    while(M--) {
        int opt = read(), l = read(), r = read(), val = read(); 
        if(opt == 1) {
            if(!id[val]) id[val] = ++tot;
            Add(l, r, val);
        } 
        else if(opt == 2) Delet(l, r, val);
        else 
            printf("%d\n", Query(1, l, r));
    }
    return 0;
}