QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#766607#9731. Fuzzy RankingLinxRE 16ms7684kbC++232.2kb2024-11-20 17:56:262024-11-20 17:56:26

Judging History

This is the latest submission verdict.

  • [2024-11-25 12:28:53]
  • hack成功,自动添加数据
  • (/hack/1257)
  • [2024-11-20 17:56:26]
  • Judged
  • Verdict: RE
  • Time: 16ms
  • Memory: 7684kb
  • [2024-11-20 17:56:26]
  • Submitted

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define log(x) (31^__builtin_clz(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
using namespace std;
const int maxn=2e5+5;
vector<int>e[maxn],st;
int vis[maxn],inst[maxn],dfn[maxn],low[maxn],cnt;
int scc[maxn],siz[maxn],sc;
inline ll calc(int x){
	return 1ll*(x)*(x-1)/2;
}
void dfs(int x){
	low[x]=dfn[x]=++cnt;
	inst[x]=vis[x]=1;
	st.push_back(x);
	for(auto u:e[x]){
		if(!vis[u]){
			dfs(u);
			low[x]=min(low[x],low[u]);
		}
		else if(inst[u]){
			low[x]=min(low[x],dfn[u]);
		}
	}
	if(low[x]==dfn[x]){
		sc++;
		siz[sc]=0;
		while(1){
			int u=st.back();
			scc[u]=sc;
			siz[sc]++;
			st.pop_back();
			if(u==x)break;
		}
	}
	inst[x]=0;
}
void solve(){
	int n,k,q,rt;
	cin>>n>>k>>q;
	sc=cnt=0;
	vector<vector<int>>a(k+2,vector<int>(n+2));
	for(int i=1;i<=n;i++){
		vis[i]=scc[i]=0;
		e[i].clear();
	}
	for(int i=1;i<=k;i++){
		int las=0;
		for(int j=1;j<=n;j++){
			int x;
			cin>>x;
			a[i][j]=x;
			if(i==1&&j==1)rt=x;
			if(j>1)e[las].push_back(x);
			las=x;
		}
	}
	dfs(rt);
	vector<vector<pii>>t(k+5);
	vector<vector<ll>>sum(k+5);
	// for(int i=1;i<=n;i++)cout<<scc[a[4][i]]<<" ";cout<<"\n";
	for(int i=1;i<=k;i++){
		t[i]={{1,1}};
		sum[i]={0};
		for(int j=2;j<=n;j++){
			if(scc[a[i][j]]==scc[a[i][j-1]]){
				sum[i].back()+=t[i].back().second-t[i].back().first+1;
				t[i].back().second++;
			}
			else{
				t[i].push_back({j,j});
				sum[i].push_back(sum[i].back());
			}
		}
		t[i].push_back({n+1,n+1});
	}
	ll ans=0;
	for(int i=1;i<=q;i++){
		int id,l,r;
		cin>>id>>l>>r;
		id=(id+ans)%k+1;
		l=(l+ans)%n+1;
		r=(r+ans)%n+1;
		// cout<<id<<" "<<l<<" "<<r<<"\n";
		if(scc[a[id][l]]==scc[a[id][r]]){
			ans=1ll*(r-l+1)*(r-l)/2;
			cout<<ans<<"\n";
			continue;
		}
		int L=lower_bound(t[id].begin(),t[id].end(),(pii){l+1,0})-t[id].begin(),R=lower_bound(t[id].begin(),t[id].end(),(pii){r+1,0})-t[id].begin();
		R--;
		L--;
		ans=sum[id][R-1]-sum[id][L];
		int siz1=t[i][L].second-l+1,siz2=r-t[i][R].first+1;
		ans+=calc(siz1)+calc(siz2);
		cout<<ans<<"\n";
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	cin>>t;
	while(t--)solve();
}
/*
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7684kb

input:

2
5 2 2
1 2 3 4 5
5 4 3 2 1
1 0 2
1 2 1
5 3 3
1 2 3 4 5
1 3 2 4 5
1 2 3 5 4
0 0 2
0 2 3
1 0 3

output:

3
10
1
1
2

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 16ms
memory: 7656kb

input:

2000
10 10 10
4 5 3 6 8 9 2 1 7 10
4 5 6 3 8 9 1 2 10 7
5 4 3 6 8 9 1 2 7 10
4 5 6 3 8 9 1 2 7 10
4 5 3 6 8 9 2 1 10 7
4 5 6 3 8 9 1 2 10 7
5 4 6 3 8 9 1 2 7 10
5 4 6 3 8 9 1 2 10 7
4 5 6 3 8 9 2 1 7 10
5 4 3 6 8 9 2 1 10 7
3 1 6
5 7 8
0 2 3
7 9 9
2 1 9
6 1 6
7 2 3
0 0 4
1 8 1
1 8 7
10 10 10
9 10 5 ...

output:

1
1
0
0
3
2
0
2
2
4
1
0
1
1
1
1
2
4
4
3
1
6
28
0
0
10
10
6
6
15
0
3
10
6
16
6
11
1
2
1
1
6
3
3
0
4
5
3
4
1
1
3
2
4
0
3
4
4
4
4
0
0
1
1
2
0
0
1
2
1
1
0
0
1
4
3
0
4
4
1
3
6
16
16
0
11
16
1
4
15
1
4
2
0
0
2
0
1
2
4
0
0
0
0
0
0
0
0
0
0
1
0
0
6
3
0
3
4
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
1
3
1
0
0
3
3
9
1
9
4
...

result:

ok 20000 lines

Test #3:

score: -100
Runtime Error

input:

8000
5 5 10
3 5 2 4 1
3 2 5 4 1
3 5 2 4 1
3 5 2 4 1
3 5 2 4 1
1 1 1
4 1 3
1 0 3
4 2 3
1 0 1
3 2 3
1 2 3
3 0 2
1 1 3
1 1 2
5 5 10
5 3 1 2 4
3 5 1 2 4
5 3 1 2 4
3 5 1 2 4
5 3 1 2 4
0 0 1
2 0 1
4 1 2
1 3 4
2 0 1
4 3 3
1 4 4
1 3 4
0 0 4
0 0 3
5 5 10
2 3 4 1 5
5 1 4 3 2
1 3 4 2 5
2 3 4 1 5
5 1 3 4 2
1 2 ...

output:


result: