QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#853249#9732. Gathering Mushroomsucup-team361WA 12ms89540kbC++144.6kb2025-01-11 16:18:272025-01-11 16:18:29

Judging History

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

  • [2025-01-11 16:18:29]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:89540kb
  • [2025-01-11 16:18:27]
  • 提交

answer

// what is matter? never mind. 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define ull unsigned long long
//#define int long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}

#define mod 1000000007
struct modint{
	unsigned int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,2,n-1)fac[i]=fac[i-1]*i;
	ifac[n-1]=1/fac[n-1];
	Rep(i,n-2,0)ifac[i]=ifac[i+1]*(i+1),iv[i+1]=ifac[i+1]*fac[i];
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f3f3f3f3f

int n,k,vis[maxn],a[maxn],to[maxn];
vi e[maxn];
pii res[maxn];

pii operator +(pii a,int b){
	return mkp(a.fi+b,a.se);
}

bool on[maxn];
int q[maxn],len;

int ps[maxn];
int cnt[maxn];
vi buc[maxn];

pii Q(int id,int col,int k){
	int sz=buc[col].size()/2;
	if(!sz) return mkp(inf,col);

//	for(int x:buc[col]) cout<<x<<" " ; cout<<" bucs\n";
	
	int cnt=(k-1)/sz;
	int res=0;
	
	res+=cnt*len;
	k-=cnt*sz;
	
	auto it=lower_bound(ALL(buc[col]),id);
	it+=(k-1);
	
	res+=(*it)-id;

	return mkp(res,col);
}

vi stk[maxn];
int dep[maxn];
int upid;

void dfs(int u,int pa){
	vis[u]=1;
	dep[u]=dep[pa]+1;
	stk[a[u]].pb(u);
	if(stk[a[u]].size()>=k) {
		int anc=stk[a[u]][stk[a[u]].size()-k];
		res[u]=min(res[u],mkp(dep[u]-dep[anc],a[u]));
	}else{
		int tmp=k-(stk[a[u]].size()-1);
//		cout<<"TAT "<<u<<" "<<tmp<<endl;
		res[u]=min(res[u],Q((upid+1)%len,a[u],k-stk[a[u]].size())+dep[u]);
	}
	for(int v:e[u]){
		if(v==pa||on[v]) continue;
		res[v]=min(res[v],res[u]+1);
		dfs(v,u);
	}
	stk[a[u]].pop_back();
}

void solve(int u){
	while(!vis[u]) vis[u]=1,u=to[u];
	len=0; 
	while(!on[u]) q[len++]=u,on[u]=1,u=to[u];
	
//	cout<<"solve "<<u<<"\n";
//	For(i,0,len-1) cout<<q[i]<<" "; cout<<endl;
	
	For(i,0,len-1) buc[a[q[i]]].clear();
	For(i,0,len-1) {
		ps[i]=buc[a[q[i]]].size();
		buc[a[q[i]]].pb(i);
	}
	For(i,0,len-1) {
		buc[a[q[i]]].pb(i+len);
	}
	
	For(i,0,len-1) {
		int u=q[i];
		res[u]=Q(i,a[u],k);
	}
	For(_,0,2) {
		Rep(i,len-1,0) 
			res[q[i]]=min(res[q[i]],res[q[(i+1)%len]]+1);
	}
	For(i,0,len-1){
		int u=q[i];
		upid=i;
		dfs(u,0);
	}
	For(i,0,len-1){
		buc[a[q[i]]].clear();
	}
}

void work()
{
	n=read(),k=read();
	For(i,1,n)e[i].clear(),vis[i]=0,res[i]=mkp(inf,-1),on[i]=0,dep[i]=0,assert(!stk[i].size());
	
	For(i,1,n)a[i]=read();
	For(i,1,n)to[i]=read(),e[to[i]].pb(i);
	
	For(i,1,n) if(!vis[i]) solve(i);
	
//	For(i,1,n) cout<<res[i].fi<<" "<<res[i].se<<"\n"; cout<<" res\n";
	
	int out=0;
	For(i,1,n)out+=res[i].se*i;
	cout<<out<<endl;
}

signed main()
{
//	freopen("my.out","w",stdout);
	int T=read();
	while(T--)work();
	return 0;
}
/*
1
9 140979583
4 5 8 9 2 7 6 8 2
8 9 4 6 9 2 4 7 8

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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 12ms
memory: 87564kb

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
260
254
17
84
759
126
30
1092
1
2493
2422
168
321
298
324
2424
2520
110
228
1107
9
3486
1
796
5
50
272
600
3196
32
495
40
27
140
665
1635
702
68
96
90
288
29
588
16
234
445
2928
140
40
227
1197
19
1542
1082
32
522
672
20
390
32
2204
1938
42
21
885
4
1539
196
420
11
1709
801
720
1
535
40
17
2152...

result:

wrong answer 4th lines differ - expected: '26', found: '17'