求助为什么我的程序跑得这么慢,最后吸氧才勉强跑过
查看原帖
求助为什么我的程序跑得这么慢,最后吸氧才勉强跑过
183603
SUNCHAOYI废物生成器楼主2022/7/28 19:39

求助为什么我的程序跑得这么慢,最后吸氧才勉强跑过。

大致是按照 OI-Wiki\texttt{OI-Wiki} 上写的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define init(x) memset (x,0,sizeof (x))
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 2e5 + 5;
const int MOD = 1e9 + 7;
inline int read ();
int n,m,cnt,root[MAX];
struct T
{
	int dl,dr,dep,fa;
} tree[MAX << 5];
inline int build (int cur,int l,int r);
inline int merge (int cur,int l,int r,int x,int y);
inline int getfa (int cur,int x);
inline int query (int cur,int l,int r,int x);
int main ()
{
//	freopen (".in","r",stdin);
//	freopen (".out","w",stdout);
	n = read ();m = read ();
	root[0] = build (1,1,n);
	for (register int i = 1;i <= m;++i)
	{
		int ty = read (),x,y;
		if (ty == 1)
		{
			x = read (),y = read ();	
			int dx = getfa (root[i - 1],x),dy = getfa (root[i - 1],y);
			if (tree[dx].dep < tree[dy].dep) swap (dx,dy);	
			root[i] = merge (root[i - 1],1,n,dy,dx);
		}
		if (ty == 2) root[i] = root [read ()];
		if (ty == 3)
		{
			int x = read (),y = read ();
			root[i] = root[i - 1];
			printf ("%d\n",getfa (root[i - 1],x) == getfa (root[i - 1],y));
		}
	}
	return 0;
}
inline int read ()
{
    int s = 0;int f = 1;
    char ch = getchar ();
    while ((ch < '0' || ch > '9') && ch != EOF)
	{
        if (ch == '-') f = -1;
        ch = getchar ();
    }
    while (ch >= '0' && ch <= '9')
	{
        s = s * 10 + ch - '0';
        ch = getchar ();
    }
    return s * f;
}
inline int build (int cur,int l,int r)
{
	cur = ++cnt;
	if (l == r) 
	{
		tree[cur].fa = l;
		return cur;
	}
	int mid = (l + r) >> 1;
	tree[cur].dl = build (tree[cur].dl,l,mid);
	tree[cur].dr = build (tree[cur].dr,mid + 1,r);
	return cur;
}
inline int merge (int cur,int l,int r,int x,int y)
{
	int nw = ++cnt;
	tree[nw] = tree[cur];
	if (l == r) 
	{
		tree[nw].fa = y;
		tree[y].dep = tree[nw].dep + 1;
		return nw;
	}
	int mid = (l + r) >> 1;
	if (x <= mid) tree[nw].dl = merge (tree[cur].dl,l,mid,x,y);
	else tree[nw].dr = merge (tree[cur].dr,mid + 1,r,x,y);
	return nw;
}
inline int getfa (int cur,int x)
{
	int nw = query (cur,1,n,x);
	if (x == nw) return x;
	else return getfa (cur,nw);
}
inline int query (int cur,int l,int r,int x)
{
	if (l == r) return tree[cur].fa;
	int mid = (l + r) >> 1;
	if (x <= mid) return query (tree[cur].dl,l,mid,x);
	else return query (tree[cur].dr,mid + 1,r,x); 
}
2022/7/28 19:39
加载中...