結果

問題 No.1024 Children in a Row
ユーザー chocoruskchocorusk
提出日時 2020-03-05 18:19:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 1,741 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 78,032 KB
実行使用メモリ 38,192 KB
最終ジャッジ日時 2023-10-12 07:36:01
合計ジャッジ時間 7,926 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
23,532 KB
testcase_01 AC 6 ms
23,520 KB
testcase_02 AC 6 ms
23,468 KB
testcase_03 AC 7 ms
23,544 KB
testcase_04 AC 7 ms
23,544 KB
testcase_05 AC 7 ms
23,480 KB
testcase_06 AC 6 ms
23,520 KB
testcase_07 AC 7 ms
23,540 KB
testcase_08 AC 7 ms
23,540 KB
testcase_09 AC 7 ms
23,540 KB
testcase_10 AC 191 ms
30,048 KB
testcase_11 AC 186 ms
30,024 KB
testcase_12 AC 191 ms
30,020 KB
testcase_13 AC 202 ms
30,212 KB
testcase_14 AC 167 ms
29,956 KB
testcase_15 AC 160 ms
30,080 KB
testcase_16 AC 178 ms
29,192 KB
testcase_17 AC 167 ms
32,224 KB
testcase_18 AC 147 ms
26,484 KB
testcase_19 AC 215 ms
30,828 KB
testcase_20 AC 221 ms
30,864 KB
testcase_21 AC 214 ms
30,808 KB
testcase_22 AC 234 ms
30,800 KB
testcase_23 AC 175 ms
29,596 KB
testcase_24 AC 158 ms
29,568 KB
testcase_25 AC 192 ms
29,624 KB
testcase_26 AC 141 ms
32,524 KB
testcase_27 AC 122 ms
26,788 KB
testcase_28 AC 163 ms
38,128 KB
testcase_29 AC 112 ms
38,192 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];
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];
	auto myon=[&](int k, int l, int r){
		return d[rev[lower_bound(s+l+1, s+r+1, s[l]+k)-s-1]];
	};
	for(int i=0; i<m; i++){
		int ans;
		int 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