loj#2312. 「HAOI2017」八纵八横(线性基 线段树分治)

时间:2022-05-10 19:18:37

题意

题目链接

Sol

线性基+线段树分治板子题。。

调起来有点自闭。。

#include<bits/stdc++.h> 
#define fi first
#define se second
#define pb push_back 
#define bit bitset<B + 1> 
using namespace std;
const int MAXN = 501,  B = 1001, SS = 4001;
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, Q, las[SS];
struct Graph {
    struct Edge {
        int u, v;
        bit w;
        int nxt;
    }E[3021];
    int head[MAXN], num, vis[MAXN];
    Graph() {
        memset(head, -1, sizeof(head));
        memset(vis, 0, sizeof(vis));
        num = 0;
    }
    bit P[B + 1], dis[MAXN];
    void insert(bit s) {
        for(int i = B; ~i; i--) {
            if(s[i]) {
                if(!P[i].count()) {P[i] = s; break;}
                s = s ^ P[i];
            }
        }
    }
    void dfs(int x, int fa) {
        vis[x] = 1;
        for(int i = head[x]; ~i; i = E[i].nxt) {
            int to = E[i].v; bit w = E[i].w;
            if((i ^ 1) == fa) continue;
            if(vis[to]) insert(dis[x] ^ dis[to] ^  w);
            else {
                dis[to] = dis[x] ^ w; 
                dfs(to, i);
            }
        }
    }
    void Query() {
        bit ans; ans.reset();
        for(int i = B; ~i; i--) if(ans[i] ^ (P[i][i])) ans ^= P[i];
        for(int i = B; ~i; i--) 
            if(ans[i]) {
                for(int j = i; ~j; j--) cout << ans[j];
                puts("");
                return ;
            }   
        puts("0");
    }
    void AddEdge(int x, int y, bit w) {
        E[num] = {x, y, w, head[x]};
        head[x] =num++;
    }
    void Insert(int x, int y, bit w) {
        insert(dis[x] ^ dis[y] ^ w);
    }

}bg;
struct Edge {
    int u, v;
    bit w;
}e[SS];
vector<Edge> tag[SS];

#define ls k << 1
#define rs k << 1 | 1
void Add(int k, int l, int r, int ql, int qr, Edge v) {
    if(ql > qr) return ;
    if(ql <= l && r <= qr) {tag[k].pb(v); return ;}
    int mid = l + r >> 1;
    if(ql <= mid) Add(ls, l, mid, ql, qr, v);
    if(qr  > mid) Add(rs, mid + 1, r, ql, qr, v);
}
void Down(int k, int l, int r, Graph now) {
    if(l > r) return ;
    Graph nxt = now;
    for(auto flag: tag[k]) 
        nxt.Insert(flag.u, flag.v, flag.w);
    if(l == r) {
        nxt.Query(); 
        return ;
    }
    int mid = l + r >> 1;
    Down(ls, l, mid, nxt);
    Down(rs, mid + 1, r, nxt);
}
signed main() {
    N = read(); M = read(); Q = read();
    for(int i = 1; i <= M; i++) {
        int x = read(), y = read(); bit w; cin >> w;
        bg.AddEdge(x, y, w);
        bg.AddEdge(y, x, w);
    }
    memset(las, -1, sizeof(las));
    int youducuotile = 0;
    for(int i = 1; i <= Q; i++) {
        char opt[10]; scanf("%s", opt + 1);
        if(opt[1] == 'A') {
            e[++youducuotile ].u = read(), e[youducuotile ].v = read();
            cin >> e[youducuotile ].w; las[youducuotile ] = i;
        } else if(opt[1] == 'C' && opt[2] == 'a') {
            int k = read(); 
            Add(1, 1, Q, las[k], i - 1, e[k]); 
            las[k] = -1;
        } else {
            int k = read();
            Add(1, 1, Q, las[k], i - 1, e[k]);
            cin >> e[k].w;
            las[k] = i;
        }
    }
    for(int i = 1; i <= Q; i++)
        if(~las[i])
            Add(1, 1, Q, las[i], Q, e[i]);
    bg.dfs(1, -1);
    bg.Query();
    if(Q) 
        Down(1, 1, Q, bg);
    return 0;
}