QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#376146#4895. Lovely Dogsmarher0 569ms112728kbC++144.2kb2024-04-03 21:45:512024-04-03 21:45:51

Judging History

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

  • [2024-04-03 21:45:51]
  • 评测
  • 测评结果:0
  • 用时:569ms
  • 内存:112728kb
  • [2024-04-03 21:45:51]
  • 提交

answer

#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e5+500,M=2e7+19260+817;

struct node
{
	int u,v,nxt;
}e[N];

int n,d,head[N],cnt,pri[N],cc,a[N],fir[N],f[N],mx[N],tot,tl[M],tr[M],sa[M],sb[M],ro[N],ans[N],sz[N];ll sum[M];
vector<pair<ll,ll>>p[N];vector<int>in[N];int st[M],top;pair<ll,ll>g[N];

void init()
{
	// d=20;
	for(int i=2;i<=n;i++)
	{
		if(!a[i])pri[++cc]=fir[i]=i;
		for(int j=1;j<=cc&&i*pri[j]<=n;j++)
		{
			a[i*pri[j]]=1;fir[i*pri[j]]=pri[j];
			if(i%pri[j]==0)break;
		}
	}
	p[1].push_back(make_pair(1,1));f[1]=1;
	for(int i=2;i<=n;i++)
	{
		int num=0,x=i;ll k=1;
		while(x%fir[i]==0)++num,x/=fir[i];
		mx[i]=max(mx[x],num);
		if(mx[i]<=d)f[i]=-f[i/fir[i]];
		else continue;
		for(int j=1;j<=d-num+1&&k<=n;j++)k*=fir[i];
		if(k==1)continue;
		int c=0;
		for(auto t:p[x])
		{
			// p[i].push_back(t);
			t.first*=k;t.second*=-1;
			if(t.first<=n)g[c++]=t;
		}
		int pos1=0,pos2=0;
		for(int j=0;j<c+p[x].size();j++)
		{
			if(pos1==p[x].size())p[i].push_back(g[pos2++]);
			else if(pos2==c)p[i].push_back(p[x][pos1++]);
			else if(p[x][pos1]<g[pos2])p[i].push_back(p[x][pos1++]);
			else p[i].push_back(g[pos2++]);
		}
	}
	for(int i=1;i<=n;i++)if(f[i]!=0)for(int j=i;j<=n&&f[j]!=0;j+=i)in[j].push_back(i);
}

#define mid ((l+r)>>1)

void add(int&x)
{
	if(top)x=st[top--];
	else x=++tot;
}

void erase(int x)
{
	st[++top]=x;tl[x]=tr[x]=sum[x]=sa[x]=sb[x]=0;
}

void merge(int&a,int b,int l,int r)
{
	if(!a||!b)return void(a=a+b);
	if(l==r)
	{
		sa[a]+=sa[b];sb[a]+=sb[b];sum[a]=1ll*sa[a]*sb[a];
		return;
	}
	merge(tl[a],tl[b],l,mid);merge(tr[a],tr[b],mid+1,r);
	sum[a]=sum[tl[a]]+sum[tr[a]];erase(b);
}

int sta[N<<1],pos,da[N],db[N];

void insert(int&x,int l,int r)
{
	if(!x)add(x);
	if(l==r)
	{
		int t=pos;
		while(sta[pos]==sta[t])sa[x]+=da[pos],sb[x]+=db[pos],++pos;
		sum[x]=1ll*sa[x]*sb[x];
		return;
	}
	if(sta[pos]<=mid&&sta[pos]>=l)insert(tl[x],l,mid);
	if(sta[pos]>mid&&sta[pos]<=r)insert(tr[x],mid+1,r);
	sum[x]=sum[tl[x]]+sum[tr[x]];
}

void dfs(int x,int fa)
{
	add(ro[x]);sz[x]=(mx[a[x]]*2<=d?1:0);
	if(f[a[x]])
	{
		int top=0;pos=1;
		int p1=0,p2=0,w=a[x],n1=p[w].size(),n2=in[w].size();
		while(p1<n1||p2<n2)
		{
			if(p1==n1)sta[++top]=in[w][p2++],da[top]=0,db[top]=f[w];
			else if(p2==n2)sta[++top]=p[w][p1].first,da[top]=f[w]*p[w][p1].second,db[top]=0,++p1;
			else if(p[w][p1].first>in[w][p2])sta[++top]=in[w][p2++],da[top]=0,db[top]=f[w];
			else sta[++top]=p[w][p1].first,da[top]=f[w]*p[w][p1].second,db[top]=0,++p1;
		}
		sta[top+1]=-1;insert(ro[x],1,n);
	}
	for(int i=head[x];i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,x);sz[x]+=sz[v];
		merge(ro[x],ro[v],1,n);
	}
	ans[x]=sum[ro[x]]+sz[x];
}

namespace IO {
#define iL (1 << 20)
char ibuf[iL], *iS = ibuf + iL, *iT = ibuf + iL;
#define gc() ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, iL, stdin), iS == iT ? EOF : *iS ++) : *iS ++)
template<class T> inline void read(T &x) {
  x = 0;int f = 0;char ch = gc();
  for (; !isdigit(ch); f |= ch == '-', ch = gc());
  for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc());
  x = (f == 1 ? ~ x + 1 : x);
}
template<class T, class... Args> inline void read(T &x, Args&... args) { read(x), read(args...); }
template<class T> inline void readch(T &x) { char ch = gc(); for (; !isalpha(ch); ch = gc()); x = ch; }
char Out[iL], *iter = Out;
#define flush() fwrite(Out, 1, iter - Out, stdout), iter = Out
template<class T> inline void write(T x, char ch = '\n') {
  T l, c[35];
  if (x < 0) *iter ++ = '-', x = ~ x + 1;
  for (l = 0; !l || x; c[l] = x % 10, l++, x /= 10);
  for (; l; -- l, *iter ++ = c[l] + '0');*iter ++ = ch;
  flush();
}
template<class T, class... Args> inline void write(T x, Args... args) { write(x, ' '), write(args...); }
} // IO
using namespace IO;

int main()
{
	// freopen("in","r",stdin);
	// freopen("out","w",stdout);
	read(n,d);
	// n=2e5;
	init();
	for(int i=1;i<n;i++)
	{
		int u,v;read(u,v);
		e[++cnt]=(node){u,v,head[u]};head[u]=cnt;
		e[++cnt]=(node){v,u,head[v]};head[v]=cnt;
	}
	for(int i=1;i<=n;i++)read(a[i]);
	dfs(1,0);
	for(int i=1;i<=n;i++)write(ans[i]/2);
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 54736kb

input:

20 2
18 8
18 11
13 19
10 8
9 11
4 8
9 15
9 17
2 1
13 18
20 18
1 8
12 17
7 16
5 11
16 15
6 19
14 16
1 3
2 15 5 13 20 6 16 18 9 19 17 7 14 10 11 3 1 12 4 8

output:

9
0
1
0
0
1
0
4
3
0
4
1
2
0
2
1
1
4
1
0

result:

wrong answer 1st words differ - expected: '16', found: '9'

Subtask #2:

score: 0
Wrong Answer

Test #24:

score: 0
Wrong Answer
time: 3ms
memory: 53016kb

input:

2000 1
134 1468
867 1750
351 1220
1690 1888
1685 134
585 282
1142 643
206 271
260 1833
1987 770
1029 1667
322 1371
341 518
601 915
119 893
1933 1502
951 1785
1056 1630
1957 1208
96 55
1508 1212
331 427
505 151
1378 1486
1545 697
1459 629
202 997
180 1917
1638 1177
1244 1896
302 658
1433 1605
1318 19...

output:

-108
-1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-2
-1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
0
5
0
0
0
0
1
0
0
-1
0
0
0
-1
0
0
0
0
0
0
0
-1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-1
-1
0
-5
0
0
0
0
0...

result:

wrong answer 1st words differ - expected: '581', found: '-108'

Subtask #3:

score: 0
Time Limit Exceeded

Test #45:

score: 0
Time Limit Exceeded

input:

200000 20
117994 12616
53490 106425
103660 50033
132640 78252
58384 19939
69183 10015
39098 165030
179856 130356
65245 57831
18234 83378
4240 154896
177149 102260
4634 180087
132390 19627
98506 60775
1890 120740
87908 21917
41323 192721
181885 96684
69412 139951
9800 38301
59025 29879
186185 81402
1...

output:


result:


Subtask #4:

score: 0
Wrong Answer

Test #50:

score: 0
Wrong Answer
time: 339ms
memory: 103700kb

input:

200000 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61...

output:

735
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '-44916', found: '735'

Subtask #5:

score: 0
Wrong Answer

Test #55:

score: 0
Wrong Answer
time: 346ms
memory: 104996kb

input:

200000 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61...

output:

735
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '-44916', found: '735'

Subtask #6:

score: 0
Wrong Answer

Test #78:

score: 0
Wrong Answer
time: 90ms
memory: 64500kb

input:

50000 1
8097 41839
17674 41774
40520 8024
5786 38261
20664 43471
1217 49276
11185 40807
14186 25584
31704 14814
42333 41475
13053 39565
45938 30104
5826 39463
5031 10814
43784 6042
58 33849
42978 18978
36307 33276
34769 4351
27884 37532
27528 29431
29451 39345
10946 9667
19016 47269
7911 30103
10308...

output:

53
0
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-3
0
0
0
0
0
0
2
0
0
1
0
-2
0
0
1
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
-1
-2
0
0
0
0
0
0
0
1
0
-1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
0
-...

result:

wrong answer 1st words differ - expected: '-9152', found: '53'

Subtask #7:

score: 0
Wrong Answer

Test #103:

score: 0
Wrong Answer
time: 569ms
memory: 112728kb

input:

200000 1
118863 188865
188022 168616
118976 119404
178852 33449
81624 40431
151228 160976
68943 136313
57200 117631
147789 139875
100240 55537
164811 145415
103548 186750
15010 168029
155731 107005
69836 1502
86171 122700
83448 131948
189162 94464
128210 2509
49724 183329
174782 192641
27687 71315
1...

output:

735
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-3
0
0
0
0
0
0
0
0
0
-2
0
0
0
0
0
0
0
0
0
-1
2
0
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
3
0
0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-1
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '-44916', found: '735'