QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#78026#4839. Smaller LCAs8194272WA 1693ms408380kbC++143.6kb2023-02-16 14:39:202023-02-16 14:39:21

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-16 14:39:21]
  • 评测
  • 测评结果:WA
  • 用时:1693ms
  • 内存:408380kb
  • [2023-02-16 14:39:20]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<random>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>

#define int long long
#define fi first
#define se second
#define max Max
#define min Min
#define abs Abs
#define lc (x<<1)
#define rc (x<<1|1)
#define mid ((l+r)>>1)
#define pb(x) push_back(x)
#define lowbit(x) ((x)&(-(x)))
#define fan(x) ((((x)-1)^1)+1)
#define mp(x,y) make_pair(x,y)
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
#define SZ(x) ((int)(x.size()))
#define INF 0x3f3f3f3f

using namespace std;

inline int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();}
	return ans*f;
}

inline void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x/10) write(x/10);
	putchar((char)(x%10)+'0');
}

template<typename T>inline T Abs(T a){return a>0?a:-a;};
template<typename T,typename TT>inline T Min(T a,TT b){return a<b?a:b;}
template<typename T,typename TT> inline T Max(T a,TT b){return a<b?b:a;}

const int N=5e6+5;
int n,dfn[N],low[N],dep[N],up[N][20],nm[N],nm2[N],ans[N],cnt;

struct Edge
{
	int v,ne;
}e[N*2];
int head[N],tot;

inline void add(int u,int v)
{
	e[++tot]=(Edge){v,head[u]};
	head[u]=tot;
}

struct BIT
{
	int c[N];
	inline void add(int x,int v)
	{
		for(;x<=n;x+=lowbit(x))
			c[x]+=v;
	}
	inline int query(int x)
	{
		int res=0;
		for(;x;x-=lowbit(x))
			res+=c[x];
		return res;
	}
	inline int query(int l,int r)
	{
		return query(r)-query(l-1);
	}
	inline void clear()
	{
		for(int i=1;i<=n;++i)
			c[i]=0;
	}
}sm;

struct node
{
	int x,p,w;
	bool operator < (const node &t)const
	{
		return x<t.x;
	}
};

vector<node> T1,T2;

void dfs1(int u,int fa)
{
	dfn[u]=++cnt;
	dep[u]=dep[fa]+1;
	up[u][0]=fa;
	for(int i=1;i<=19;++i)
		up[u][i]=up[up[u][i-1]][i-1];
	for(int i=head[u];i;i=e[i].ne)
	{
		int v=e[i].v;
		if(v==fa) continue;
		dfs1(v,u);
	}
	low[u]=cnt;
}

inline int LCA(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=19;i>=0;--i)
		if(dep[up[x][i]]>=dep[y])
			x=up[x][i];
	if(x==y) return x;
	for(int i=19;i>=0;--i)
		if(up[x][i]!=up[y][i])
			x=up[x][i],y=up[y][i];
	return up[x][0];
}

int id[N];

bool cmp(int x,int y)
{
	return up[x][0]<up[y][0];
}

void dfs2(int u,int fa)
{
	for(int i=head[u];i;i=e[i].ne)
	{
		int v=e[i].v;
		if(v==fa) continue;
		nm[v]+=nm[u];
		dfs2(v,u);
		nm2[u]+=nm2[v];
	}
	ans[u]=nm[u]+nm2[u];
}

signed main()
{
	n=read();
	for(int i=1;i<n;++i)
	{
		int u=read(),v=read();
		add(u,v);add(v,u);
	}
	dfs1(1,0);
	for(int i=1;i<=n;++i)
		for(int j=i;i*j<=n;++j)
		{
			int lca=LCA(i,j);
			if(i*j<lca) nm2[up[lca][0]]++;
			
			T1.push_back((node){i*j,i,1});
			T1.push_back((node){i*j,j,1});
			T1.push_back((node){i*j,lca,-1});
			if(up[lca][0]) T1.push_back((node){i*j,up[lca][0],-1});
			
			T2.push_back((node){i*j,i,1});
			T2.push_back((node){i*j,j,1});
			T2.push_back((node){i*j,lca,-2});
		}
	sort(T1.begin(),T1.end());
	for(int i=1,j=-1;i<=n;++i)
	{
		while(j+1<SZ(T1)&&T1[j+1].x<i)
		{
			++j;
			sm.add(dfn[T1[j].p],T1[j].w);
		}
			
		nm[i]+=sm.query(dfn[i],low[i]);id[i]=i;
	}
	sort(id+1,id+1+n,cmp);
	sort(T2.begin(),T2.end());
	sm.clear();
	for(int i=1,j=-1;i<=n;++i)
	{
		while(j+1<SZ(T2)&&T2[j+1].x<up[id[i]][0])
			++j,sm.add(dfn[T2[j].p],T2[j].w);
		nm[id[i]]-=sm.query(dfn[id[i]],low[id[i]]);
	}
	dfs2(1,0);
	for(int i=1;i<=n;++i)
		write(n*(n+1)/2-ans[i]),puts("");
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 15600kb

input:

5
1 2
4 2
2 5
3 5

output:

15
15
15
15
14

result:

ok 5 number(s): "15 15 15 15 14"

Test #2:

score: -100
Wrong Answer
time: 1693ms
memory: 408380kb

input:

300000
40632 143306
32259 40632
225153 143306
269774 225153
289668 40632
191636 269774
85717 191636
58564 191636
156509 143306
289939 40632
247103 269774
40257 40632
98149 289668
142277 143306
291616 40257
46813 225153
56324 143306
277154 142277
53903 289668
114266 32259
152231 58564
241151 152231
4...

output:

44999437117
44999842142
44999850089
44999849108
44999170267
44999714570
44999466179
44999229280
44999142449
44999190190
44999836276
44999197524
44999338599
44999958057
44999648574
44999623622
44999182318
44999376086
44999387109
44999186155
44999789336
44999371637
44999617008
44999226295
45000023312
...

result:

wrong answer 2nd numbers differ - expected: '44999604051', found: '44999842142'