結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
12,484 KB
testcase_01 AC 7 ms
12,416 KB
testcase_02 AC 7 ms
12,480 KB
testcase_03 AC 7 ms
12,504 KB
testcase_04 AC 8 ms
12,512 KB
testcase_05 AC 7 ms
12,492 KB
testcase_06 AC 7 ms
12,496 KB
testcase_07 AC 7 ms
12,516 KB
testcase_08 AC 7 ms
12,504 KB
testcase_09 AC 7 ms
12,520 KB
testcase_10 AC 148 ms
28,924 KB
testcase_11 AC 147 ms
28,240 KB
testcase_12 AC 149 ms
28,664 KB
testcase_13 AC 155 ms
28,940 KB
testcase_14 AC 152 ms
28,436 KB
testcase_15 AC 156 ms
28,944 KB
testcase_16 AC 141 ms
27,784 KB
testcase_17 AC 132 ms
31,236 KB
testcase_18 AC 108 ms
25,416 KB
testcase_19 AC 173 ms
31,364 KB
testcase_20 AC 170 ms
31,352 KB
testcase_21 AC 173 ms
31,312 KB
testcase_22 AC 174 ms
31,300 KB
testcase_23 AC 161 ms
30,108 KB
testcase_24 AC 157 ms
30,068 KB
testcase_25 AC 155 ms
30,188 KB
testcase_26 AC 138 ms
33,044 KB
testcase_27 AC 114 ms
27,272 KB
testcase_28 AC 139 ms
38,676 KB
testcase_29 AC 109 ms
38,676 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, n+m);
		}
		printf("%d\n", ans+1);
	}
    return 0;
}
0