QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#165240#5410. 树特卡克林Lynkcat#Compile Error//C++204.2kb2023-09-05 16:55:432024-07-04 01:52:47

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 01:52:47]
  • 评测
  • [2023-09-05 16:55:43]
  • 提交

answer

#include<bits/stdc++.h>
#include<bits/extc++.h>
#define poly vector<int>
#define IOS ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define mt make_tuple
#define pa pair < int,int >
#define fi first
#define se second
#define inf 1e18
#define mod 998244353
#define sz(x) ((int)((x).size()))
#define int ll
#define N 100005
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
int n,m;
int ffa[N],rt[N];
poly G[N];
int s[N],siz[N],son[N];
poly all;
int mx[N];
tree<pa, null_type, less<pa>, rb_tree_tag, tree_order_statistics_node_update> t;
void dfs(int k,int fa)
{
    ffa[k]=fa;
    rt[k]=rt[fa];
    for (auto u:G[k])
    {
        if (u==fa) continue;
        dfs(u,k);
    }
} 
void makert(int k)
{
    rt[k]=k;
    dfs(k,k);
}
void gt(int k,int fa)
{
    s[k]=k;
    siz[k]=1;son[k]=0;
    mx[k]=k;
    for (auto u:G[k])
    {
        if (u==fa) continue;
        // cout<<k<<"->"<<u<<endl;
        gt(u,k);
        siz[k]+=siz[u];
        if (siz[u]>siz[son[k]]||siz[u]==siz[son[k]]&&mx[u]>mx[son[k]]) son[k]=u;
    }
    mx[k]=max(mx[k],mx[son[k]]);
    for (auto u:G[k])
    {
        if (u==fa) continue;
        if (u!=son[k]) all.push_back(s[u]);
        else s[k]^=s[u];
    }
}
void BF()
{
    for (int i=1;i<=n;i++) rt[i]=i;
    for (int i=1;i<=m;i++)
    {
        int op;
        cin>>op;
        if (op==1)
        {
            int x,y;
            cin>>x>>y;
            G[y].push_back(x);
            G[x].push_back(y);
            makert(rt[x]);
        }
        else
        {
            int x,y;
            cin>>x>>y;
            if (ffa[x]==y) swap(x,y);
            G[x].erase(find(G[x].begin(),G[x].end(),y));
            G[y].erase(find(G[y].begin(),G[y].end(),x));
            makert(y);
        }
        int k;
        cin>>k;
        for (int j=1;j<=n;j++)
            if (rt[j]==j) gt(j,j),all.push_back(s[j]);
        sort(all.begin(),all.end());
        cout<<all[(k-1)%all.size()]<<'\n';
        poly().swap(all);
    }
}
inline int tp(int k)
{
    while (ffa[k]!=k)
    {
        if (son[ffa[k]]!=k) return k;
        k=ffa[k];
    }
    return k;
}
inline void updmx(int k,int x)
{
    int res=x;
    while (ffa[k]!=k)
    {
        res=max(res,k);
        mx[k]=res;
        if (son[ffa[k]]!=k) return;
        k=ffa[k];
    }
    return;
}
void jump(int k)
{
    // cout<<"jump to "<<k<<endl;
    if (ffa[k]==k) 
    {
        t.insert(mp(s[k],k));
        return;
    }
    if (son[ffa[k]]==k)
    {
        jump(ffa[k]);
        return;
    }
    int fa=ffa[k];
    if (!son[fa]||siz[k]>siz[son[fa]]||siz[k]==siz[son[fa]]&&mx[k]>mx[son[fa]])
    {
        t.erase(mp(s[tp(fa)],tp(fa)));
        if (son[fa])
        {
            s[tp(fa)]^=s[son[fa]];
            t.insert(mp(s[son[fa]],son[fa]));
        }
        son[fa]=k;
        s[tp(k)]^=s[k];
        updmx(fa,mx[k]);
        // t.insert(mp(s[tp(k)],tp(k)));
        jump(fa);
    }
    else
    {
        t.insert(mp(s[k],k));
        t.erase(mp(s[tp(fa)],tp(fa)));
        jump(fa);
    }
}
void BF1()
{
    mt19937_64 rnd(12312313123);
    for (int i=1;i<=n;i++) ffa[i]=i,s[i]=i,mx[i]=i,siz[i]=1,t.insert(mp(s[i],i));
    for (int ii=1;ii<=m;ii++)
    {
        int op=1;
        // cin>>op;
        int x,y;
        if (op==1)
        {
            x=rnd()%(ii)+1;
            y=ii+1;
            // cin>>x>>y;
            ffa[y]=x;
            int u=x;
            while (ffa[u]!=u) siz[u]+=siz[y],u=ffa[u];
            siz[u]+=siz[y];
            t.erase(mp(s[y],y));
            jump(y);
        }
        else
        {
            int x,y;
            return;
        }
        int k;
        k=rnd()%1000+1;
        // cin>>k;
        cout<<(*t.find_by_order((k-1)%t.size())).fi<<'\n';
        for (int i=1;i<=n;i++)
            if (ffa[i]!=i)
            {
                int fa=ffa[i];
                assert(siz[son[fa]]>siz[i]||siz[son[fa]]==siz[i]&&mx[son[fa]]>=mx[i]);
            }
    }
}
void BellaKira()
{
    cin>>n>>m;
    if (n<=1000&&m<=5000) BF();
    else BF1();
}
signed main()
{
	IOS;
	cin.tie(0);
	int T=1;
	while (T--)
	{
		BellaKira();
	}
}

详细

This language is not supported yet.