• Bzoj4129: Haruna’s Breakfast


    题面

    Bzoj

    Sol

    树上带修改莫队

    (mex)可以对数字也分块
    数字大于(n)就设为(n+1)
    查询就找到那个不满的块,在块内找到(mex)

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    
    template <class Int>
    IL void Input(RG Int &x){
        RG int z = 1; RG char c = getchar(); x = 0;
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        x *= z;
    }
    
    const int maxn(5e4 + 5);
    
    int n, q, first[maxn], cnt, w[maxn], sum1[maxn], sum2[300], ans[maxn], sz;
    int dfn[maxn], st[20][maxn << 1], lg[maxn << 1], deep[maxn], idx, fa[maxn];
    int sta[maxn], bl[maxn], blo, num, vis[maxn], tot1, tot2;
    
    struct Edge{
    	int to, next;
    } edge[maxn << 1];
    
    struct Query{
    	int l, r, id, t;
    
    	IL int operator <(RG Query B) const{
    		if(bl[l] != bl[B.l]) return bl[l] < bl[B.l];
    		if(bl[r] != bl[B.r]) return bl[r] < bl[B.r];
    		return t < B.t;
    	}
    } qry[maxn];
    
    struct Change{
    	int x, y;
    } mdy[maxn];
    
    IL void Add(RG int u, RG int v){
    	edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
    }
    
    IL void Dfs(RG int u){
    	dfn[u] = ++idx, st[0][idx] = u;
    	RG int l = sta[0];
    	for(RG int e = first[u]; e != -1; e = edge[e].next){
    		RG int v = edge[e].to;
    		if(dfn[v]) continue;
    		deep[v] = deep[u] + 1, fa[v] = u;
    		Dfs(v);
    		st[0][++idx] = u;
    		if(sta[0] - l < blo) continue;
    		for(++num; sta[0] != l; --sta[0]) bl[sta[sta[0]]] = num;
    	}
    	sta[++sta[0]] = u;
    }
    
    IL void Chk(RG int &x, RG int u, RG int v){
    	x = deep[u] < deep[v] ? u : v;
    }
    
    IL int LCA(RG int u, RG int v){
    	u = dfn[u], v = dfn[v];
    	if(u > v) swap(u, v);
    	RG int log2 = lg[v - u + 1], t;
    	Chk(t, st[log2][u], st[log2][v - (1 << log2) + 1]);
    	return t;
    }
    
    IL void Update(RG int x){
    	if(vis[x]){
    		--sum1[w[x]];
    		if(!sum1[w[x]]) --sum2[w[x] / sz];
    	}
    	else{
    		if(!sum1[w[x]]) ++sum2[w[x] / sz];
    		++sum1[w[x]];
    	}
    	vis[x] ^= 1;
    }
    
    IL void Adjust(RG int x, RG int &y){
    	if(vis[x]) Update(x), swap(w[x], y), Update(x);
    	else swap(w[x], y);
    }
    
    IL void Modify(RG int u, RG int v){
    	while(u != v){
    		if(deep[u] > deep[v]) swap(u, v);
    		Update(v), v = fa[v];
    	}
    }
    
    IL int Calc(){
    	for(RG int i = 0; ; ++i){
    		if(sum2[i] == sz) continue;
    		for(RG int j = 0; j < sz; ++j)
    			if(!sum1[i * sz + j]) return i * sz + j;
    	}
    	return 233;
    }
    
    IL void Init_Graph(){
    	Input(n), Input(q);
    	blo = pow(n, 0.2 / 0.3), sz = sqrt(n);
    	for(RG int i = 1; i <= n; ++i){
    		Input(w[i]), first[i] = -1;
    		if(w[i] > n) w[i] = n + 1;
    	}
    	for(RG int i = 1, u, v; i < n; ++i) Input(u), Input(v), Add(u, v), Add(v, u);
    	Dfs(1);
    	for(++num; sta[0]; --sta[0]) bl[sta[sta[0]]] = num;
    	for(RG int i = 2; i <= idx; ++i) lg[i] = lg[i >> 1] + 1;
    	for(RG int j = 1; j <= lg[idx]; ++j)
    		for(RG int i = 1; i + (1 << j) - 1 <= idx; ++i)
    			Chk(st[j][i], st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
    }
    
    IL void Init_Task(){
    	for(RG int i = 1, op, x, y; i <= q; ++i){
    		Input(op), Input(x), Input(y);
    		if(op){
    			if(dfn[x] > dfn[y]) swap(x, y);
    			qry[++tot1] = (Query){x, y, tot1, tot2};
    		}
    		else mdy[++tot2] = (Change){x, y};
    	}
    	sort(qry + 1, qry + tot1 + 1);
    }
    
    int main(RG int argc, RG char* argv[]){
    	Init_Graph(), Init_Task();
    	RG int j = 0, lca = LCA(qry[1].l, qry[1].r);
    	while(j < qry[1].t) ++j, Adjust(mdy[j].x, mdy[j].y);
    	Modify(qry[1].l, qry[1].r);
    	Update(lca), ans[qry[1].id] = Calc(), Update(lca);
    	for(RG int i = 2; i <= tot1; ++i){
    		while(j < qry[i].t) ++j, Adjust(mdy[j].x, mdy[j].y);
    		while(j > qry[i].t) Adjust(mdy[j].x, mdy[j].y), --j;
    		lca = LCA(qry[i].l, qry[i].r);
    		Modify(qry[i - 1].l, qry[i].l), Modify(qry[i - 1].r, qry[i].r);;
    		Update(lca), ans[qry[i].id] = Calc(), Update(lca);
    	}
    	for(RG int i = 1; i <= tot1; ++i) printf("%d
    ", ans[i]);
        return 0;
    }
    
  • 相关阅读:
    《NoSQL精粹》读书笔记
    react+flux编程实践(一) 基础篇
    MongoDB索引(一) --- 入门篇:学习使用MongoDB数据库索引
    (译+注解)node.js的C++扩展入门
    深入解析Javascript异步编程
    (译)package.json详解
    Protobuf学习
    Redis学习
    MySQL学习-常用命令整理
    TCP/IP-TCP
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8729244.html
Copyright © 2020-2023  润新知