QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#853212#9732. Gathering Mushroomsucup-team361WA 11ms89832kbC++144.3kb2025-01-11 16:10:312025-01-11 16:10:32

Judging History

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

  • [2025-01-11 16:10:32]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:89832kb
  • [2025-01-11 16:10:31]
  • 提交

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);
	
	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);
		res[u]=min(res[u],Q((upid+1)%len,a[u],k-stk[a[u]].size())+dep[u]+1);
	}
	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];
	
	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);
	}
}

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

详细

Test #1:

score: 100
Accepted
time: 11ms
memory: 89680kb

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: 11ms
memory: 89832kb

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
250
17
84
717
126
30
1092
1
1347
2974
129
293
326
324
2398
2520
190
225
1005
9
3486
1
796
5
50
272
495
3016
32
491
40
29
140
665
1635
702
68
108
245
267
26
588
16
448
421
2928
147
40
275
1287
19
1635
780
32
522
672
17
460
32
2162
1878
35
21
885
4
1539
196
420
11
1960
899
3253
1
577
40
17
21...

result:

wrong answer 3rd lines differ - expected: '254', found: '250'