結果

問題 No.1024 Children in a Row
ユーザー chocoruskchocorusk
提出日時 2020-03-09 19:02:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 1,775 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 77,700 KB
実行使用メモリ 39,396 KB
最終ジャッジ日時 2023-10-12 07:36:17
合計ジャッジ時間 7,008 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
25,480 KB
testcase_01 AC 8 ms
25,504 KB
testcase_02 AC 8 ms
25,476 KB
testcase_03 AC 8 ms
25,544 KB
testcase_04 AC 8 ms
25,560 KB
testcase_05 AC 8 ms
25,520 KB
testcase_06 AC 8 ms
25,552 KB
testcase_07 AC 9 ms
25,508 KB
testcase_08 AC 8 ms
25,492 KB
testcase_09 AC 9 ms
25,540 KB
testcase_10 AC 176 ms
31,332 KB
testcase_11 AC 177 ms
31,124 KB
testcase_12 AC 164 ms
31,280 KB
testcase_13 AC 169 ms
31,388 KB
testcase_14 AC 168 ms
31,224 KB
testcase_15 AC 178 ms
31,364 KB
testcase_16 AC 153 ms
30,452 KB
testcase_17 AC 144 ms
33,452 KB
testcase_18 AC 114 ms
27,684 KB
testcase_19 AC 196 ms
32,048 KB
testcase_20 AC 193 ms
32,120 KB
testcase_21 AC 185 ms
32,100 KB
testcase_22 AC 200 ms
32,036 KB
testcase_23 AC 175 ms
30,820 KB
testcase_24 AC 176 ms
30,784 KB
testcase_25 AC 173 ms
30,788 KB
testcase_26 AC 150 ms
33,712 KB
testcase_27 AC 119 ms
27,972 KB
testcase_28 AC 146 ms
39,364 KB
testcase_29 AC 114 ms
39,396 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+1], c2=s[l1]-s[l2+1], c3=s[r1]-s[l1];
			if(k[i]<=c1) ans=myon(k[i], 0, l2+1);
			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+1, 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+1]-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+1);
			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