QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#853641#9732. Gathering Mushroomsucup-team191#WA 30ms13960kbC++232.9kb2025-01-11 17:57:032025-01-11 17:57:05

Judging History

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

  • [2025-01-11 17:57:05]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:13960kb
  • [2025-01-11 17:57:03]
  • 提交

answer

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)

const int N=300010,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;

int t,n,k,h[N],ti[N],bio[N],ind[N],r[N],de[N];
pair<ll,int> an[N];
vi ch[N];
vi ord;

void dfs(int i,int p=-1)
{
	ind[i]=ord.size();
	ord.pb(i);
	for (auto x: ch[i]) if (x!=p)
	{
		de[x]=de[i]+1;
		dfs(x,i);
	}
	r[i]=ord.size();
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin>>t;
	while (t--)
	{
		cin>>n>>k;
		for (int i=0;i<n;++i) cin>>ti[i],bio[i]=0,an[i]={LLINF,-1},ch[i].clear(),de[i]=0;
		for (int i=0;i<n;++i) cin>>h[i],--h[i],ch[h[i]].pb(i);
		for (int i=0;i<n;++i) if (!bio[i])
		{
			vi cu;
			int z=i;
			while (!bio[z])
			{
				bio[z]=1;
				cu.pb(z);
				z=h[z];
			}
			int cs=cu.size();
			if (bio[z]==2)
			{
				assert(0);
			}
			else
			{
				int po=-1;
				for (int j=0;j<cs;++j) if (cu[j]==z) po=j;
				vi cik;
				for (int j=po;j<cs;++j) cik.pb(cu[j]);
				int cis=cik.size();
				map<int,pair<vi,vi>> oc;
				for (int j=0;j<cis;++j) oc[ti[cik[j]]].x.pb(j);
				ord.clear();
				vi inds;
				for (auto x: cik)
				{
					ch[h[x]].erase(find(all(ch[h[x]]),x));
					inds.pb(ord.size());
					dfs(x);
					for (int j=inds.back()+1;j<(int)ord.size();++j) oc[ti[ord[j]]].y.pb(j);
				}
				
				for (auto x: oc)
				{
					int xyxs=x.y.x.size();
					if (xyxs)
					{
						ll ru=(k-1)/xyxs;
						int off=(k-1)%xyxs;
						for (int j=0;j<xyxs;++j)
						{
							int pc=x.y.x[j],pt=x.y.x[(j+off)%xyxs];
							an[cik[pc]]={ru*cis+(pt-pc+cis)%cis,x.x};
						}
					}
					int xyys=x.y.y.size();
					if (xyys)
					{
						vi sta;
						for (auto ii: x.y.y)
						{
							while (sta.size() && ii>=r[sta.back()]) sta.pop_back();
							sta.pb(ord[ii]);
							if ((int)sta.size()>=k)
							{
								an[ord[ii]]={de[ord[ii]]-de[sta[sta.size()-k]],x.x};
							}
							else if (xyxs)
							{
								int rk=k-sta.size();
								int po=upper_bound(all(inds),ii)-inds.begin()-1;
								auto cpoit=lower_bound(all(x.y.x),po);
								if (cpoit==x.y.x.end()) cpoit=x.y.x.begin();
								int cpo=cpoit-x.y.x.begin();
								ll ru=(rk-1)/xyxs,off=(rk-1)%xyxs;
								int pc=po,pt=x.y.x[(cpo+off)%xyxs];
								an[ord[ii]]={ru*cis+(pt-pc+cis)%cis,x.x};
							}
						}
					}
				}
				for (int j=cis-1;j>=0;--j) an[cik[j]]=min(an[cik[j]],pair<ll,int>(an[h[cik[j]]].x+1,an[h[cik[j]]].y));
				for (int j=cis-1;j>=0;--j) an[cik[j]]=min(an[cik[j]],pair<ll,int>(an[h[cik[j]]].x+1,an[h[cik[j]]].y));
				for (auto x: ord) an[x]=min(an[x],pair<ll,int>(an[h[x]].x+1,an[h[x]].y)),bio[x]=2;
			}
		}
		ll s=0;
		for (int i=0;i<n;++i)
		{
			//cout<<an[i].x<<' '<<an[i].y<<en;
			s+=(i+1)*1ll*an[i].y;
		}
		cout<<s<<en;
	}
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 13832kb

input:

3
5 3
2 2 1 3 3
2 5 1 2 4
5 4
2 2 1 3 3
2 5 1 2 4
3 10
1 2 3
1 3 2

output:

41
45
14

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 30ms
memory: 13960kb

input:

6000
19 48
18 19 18 19 11 9 15 19 12 18 11 18 9 18 9 18 19 11 15
12 14 18 8 1 3 19 5 13 14 15 2 14 5 19 2 19 12 9
15 23
3 1 1 3 6 1 4 1 1 6 6 4 12 4 6
14 1 8 8 6 6 12 14 6 8 5 7 14 2 5
9 140979583
4 5 8 9 2 7 6 8 2
8 9 4 6 9 2 4 7 8
4 976357580
2 3 1 3
2 1 1 4
6 508962809
4 3 4 3 4 4
4 5 4 5 5 6
13 ...

output:

3420
261
272
26
84
739
126
30
1092
1
2493
2422
168
360
298
324
2420
2520
220
225
1083
9
3486
1
796
81
265
272
600
1918
24
495
40
123
140
602
1635
702
68
66
90
288
29
588
14
234
435
3426
140
40
399
1197
19
1994
1059
32
522
672
20
390
33
2204
1613
42
21
885
4
1539
196
420
12
1205
788
720
1
556
40
17
2...

result:

wrong answer 2nd lines differ - expected: '260', found: '261'