結果

問題 No.1024 Children in a Row
ユーザー chocoruskchocorusk
提出日時 2020-03-09 19:09:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,763 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 78,296 KB
実行使用メモリ 38,672 KB
最終ジャッジ日時 2023-10-12 07:37:17
合計ジャッジ時間 7,195 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,472 KB
testcase_01 AC 9 ms
12,400 KB
testcase_02 AC 9 ms
12,464 KB
testcase_03 AC 8 ms
12,440 KB
testcase_04 AC 9 ms
12,480 KB
testcase_05 AC 8 ms
12,440 KB
testcase_06 AC 8 ms
12,480 KB
testcase_07 AC 8 ms
12,468 KB
testcase_08 AC 8 ms
12,436 KB
testcase_09 AC 8 ms
12,440 KB
testcase_10 AC 206 ms
28,884 KB
testcase_11 AC 147 ms
28,224 KB
testcase_12 AC 154 ms
28,560 KB
testcase_13 AC 153 ms
28,992 KB
testcase_14 AC 151 ms
28,340 KB
testcase_15 AC 152 ms
28,932 KB
testcase_16 AC 143 ms
27,852 KB
testcase_17 AC 139 ms
31,128 KB
testcase_18 AC 106 ms
25,408 KB
testcase_19 AC 169 ms
31,360 KB
testcase_20 AC 172 ms
31,360 KB
testcase_21 AC 170 ms
31,360 KB
testcase_22 AC 169 ms
31,352 KB
testcase_23 AC 155 ms
30,108 KB
testcase_24 AC 154 ms
30,116 KB
testcase_25 AC 152 ms
30,132 KB
testcase_26 AC 135 ms
33,032 KB
testcase_27 AC 116 ms
27,380 KB
testcase_28 AC 136 ms
38,672 KB
testcase_29 AC 105 ms
38,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;
int n, m;
int a[200020], b[200020], k[200020];
int c[200020], d[400040];
int a1[200020], b1[200020];
vector<int> g[400040];
bool root[400040];
int l[400040], r[400040], rev[400040];
int pr[400040];
int s[400040], t[200020];
int main()
{
	scanf("%d %d", &n, &m);
	for(int i=0; i<m; i++){
		scanf("%d %d %d", &a[i], &b[i], &k[i]);
		a[i]--; b[i]--;
	}
	iota(c, c+n, 0);
	iota(d, d+n, 0);
	fill(root, root+n+m, 1);
	int cnt=n;
	for(int i=0; i<m; i++){
		pr[cnt]=c[a[i]];
		c[a[i]]=cnt++;
		d[c[a[i]]]=a[i];
		root[c[a[i]]]=0;
		a1[i]=c[a[i]], b1[i]=c[b[i]];
		g[b1[i]].push_back(a1[i]);
	}
	cnt=0;
	auto dfs=[&](auto dfs, int x)->void{
		l[x]=cnt++;
		rev[l[x]]=x;
		for(int i=(int)g[x].size()-1; i>=0; i--) dfs(dfs, g[x][i]);
		r[x]=cnt;
	};
	for(int i=0; i<n; i++){
		if(root[i]) dfs(dfs, i);
	}
	for(int i=0; i<n+m; i++) if(i==c[d[i]]) s[l[i]+1]++;
	for(int i=0; i<n+m; i++) s[i+1]+=s[i];
    for(int i=1; i<=n+m; i++) if(s[i]>s[i-1]) t[s[i]]=i;
	auto myon=[&](int k, int l, int r){
        return d[rev[t[s[l]+k]-1]];
	};
	for(int i=0; i<m; i++){
		int ans, l1=l[a1[i]], r1=r[a1[i]], l2=l[pr[a1[i]]], r2=r[pr[a1[i]]];
		if(l2<l1){
			int c1=s[l2], c2=s[l1]-s[l2], c3=s[r1]-s[l1];
			if(k[i]<=c1) ans=myon(k[i], 0, l2);
			else if(k[i]<=c1+c3) ans=myon(k[i]-c1, l1, r1);
			else if(k[i]<=c1+c3+c2) ans=myon(k[i]-c1-c3, l2, l1);
			else ans=myon(k[i]-c1-c2-c3, r1, n+m);
		}else{
			int c1=s[l1], c2=s[r1]-s[l1], c3=s[l2]-s[r1];
			if(k[i]<=c1) ans=myon(k[i], 0, l1);
			else if(k[i]<=c1+c3) ans=myon(k[i]-c1, r1, l2);
			else if(k[i]<=c1+c3+c2) ans=myon(k[i]-c1-c3, l1, r1);
			else ans=myon(k[i]-c1-c2-c3, l2+1, n+m);
		}
		printf("%d\n", ans+1);
	}
    return 0;
}
0