Submission #68307577


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i ++)
#define per(i, s, t) for(int i = (s); i >= (t); i --)
template<typename T, typename T2>
inline void chmin(T &x, T2 &&y) { x = min(x, y); }
template<typename T, typename T2>
inline void chmax(T &x, T2 &&y) { x = max(x, y); }
typedef long long ll;

const int N = 5e5 + 5;

mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
struct node
{
	int ls, rs, sz;
	unsigned int w;
	ll tg, v, mn;
	void init(ll _v) {w = rnd(); sz = 1; v = mn = _v;}
} a[N];
int idx = 0;

void settag(int x, ll v)
{
	a[x].tg += v;
	a[x].v += v;
	a[x].mn += v;
}

void pd(int x)
{
	if(a[x].tg) settag(a[x].ls, a[x].tg), settag(a[x].rs, a[x].tg), a[x].tg = 0;
}

void pu(int x)
{
	a[x].sz = a[a[x].ls].sz + a[a[x].rs].sz + 1;
	a[x].mn = a[x].ls ? a[a[x].ls].mn : a[x].v;
}

// (-inf,v][v+1,inf]
void splitv(int u, ll v, int &x, int &y)
{
	if(!u) return x = y = 0, void();
	pd(u);
	if(a[u].v <= v) x = u, splitv(a[u].rs, v, a[u].rs, y);
	else y = u, splitv(a[u].ls, v, x, a[u].ls);
	pu(u);
}

// [1,pos][pos+1,n]
void splits(int u, int rk, int &x, int &y)
{
    if(!u) return x = y = 0, void();
	pd(u);
    if(a[a[u].ls].sz < rk) x = u, splits(a[u].rs, rk - a[a[u].ls].sz - 1, a[u].rs, y);
    else y = u, splits(a[u].ls, rk, x, a[u].ls);
    pu(u);
}

int merge(int x, int y)
{
	if(!x || !y) return x | y;
	pd(x), pd(y);
	if(a[x].w < a[y].w) return a[x].rs = merge(a[x].rs, y), pu(x), x;
	else return a[y].ls = merge(x, a[y].ls), pu(y), y;
}

int join(int u, int v)
{
    if (!u || !v)
        return u | v;
    ll x = a[u].mn, y = a[v].mn;
	int t;
    if (x <= y)
        splitv(u, y, t, u);
    else
        splitv(v, x, t, v);
    return merge(t, join(u, v));
}


int ans[N];
void getans(int x, int id)
{
	if(a[x].ls) getans(a[x].ls, id);
	ans[x] = id;
	if(a[x].rs) getans(a[x].rs, id);
}

int n, ls[N], rs[N], rt[N];
ll len[N], qq[N];

signed main()
{
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n;
	len[1] = len[2] = 1;
	rep(i, 3, n + 2)
	{
		cin >> ls[i] >> rs[i] >> qq[i];
		ls[i] ++, rs[i] ++;
		len[i] = min((ll)2e18, len[ls[i]] + len[rs[i]]);
		rt[i] = i;
		a[i].init(qq[i]);
	}
	per(i, n + 2, 3)
	{
		// cerr << i << " " << ls[i] << " " << rs[i] << " " << a[rt[i]].sz << endl;
		int l, r;
		splitv(rt[i], len[ls[i]], l, r);
		if(r) settag(r, -len[ls[i]]);
		// cerr << a[l].sz << " " << a[r].sz << endl;
		rt[ls[i]] = join(rt[ls[i]], l);
		rt[rs[i]] = join(rt[rs[i]], r);
	}
	getans(rt[1], 0);
	getans(rt[2], 1);
	// cerr << a[rt[1]].sz << " " << a[rt[2]].sz << endl;
	rep(i, 3, n + 2) cout << ans[i] << "\n";

	return 0;
}

Submission Info

Submission Time
Task G - Binary Cat
User adam01
Language C++ 20 (gcc 12.2)
Score 625
Code Size 2747 Byte
Status AC
Exec Time 4892 ms
Memory 50376 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 625 / 625
Status
AC × 1
AC × 56
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random2_00.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random2_07.txt, random2_08.txt, random2_09.txt, random2_10.txt, random2_11.txt, random2_12.txt, random2_13.txt, random2_14.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3496 KiB
hand_00.txt AC 217 ms 38496 KiB
hand_01.txt AC 211 ms 38764 KiB
hand_02.txt AC 190 ms 38616 KiB
hand_03.txt AC 198 ms 38688 KiB
hand_04.txt AC 2421 ms 39984 KiB
hand_05.txt AC 4507 ms 44584 KiB
hand_06.txt AC 191 ms 38624 KiB
hand_07.txt AC 4636 ms 50376 KiB
hand_08.txt AC 167 ms 38632 KiB
hand_09.txt AC 1 ms 3628 KiB
random2_00.txt AC 168 ms 36320 KiB
random2_01.txt AC 875 ms 35824 KiB
random2_02.txt AC 936 ms 38452 KiB
random2_03.txt AC 184 ms 37032 KiB
random2_04.txt AC 755 ms 36904 KiB
random2_05.txt AC 757 ms 36540 KiB
random2_06.txt AC 184 ms 36616 KiB
random2_07.txt AC 1004 ms 38344 KiB
random2_08.txt AC 932 ms 36244 KiB
random2_09.txt AC 186 ms 36264 KiB
random2_10.txt AC 1169 ms 37288 KiB
random2_11.txt AC 1093 ms 36592 KiB
random2_12.txt AC 199 ms 36724 KiB
random2_13.txt AC 1396 ms 36576 KiB
random2_14.txt AC 1454 ms 37256 KiB
random_00.txt AC 337 ms 38548 KiB
random_01.txt AC 268 ms 38752 KiB
random_02.txt AC 899 ms 38700 KiB
random_03.txt AC 875 ms 38732 KiB
random_04.txt AC 4892 ms 48948 KiB
random_05.txt AC 351 ms 38628 KiB
random_06.txt AC 290 ms 38616 KiB
random_07.txt AC 4676 ms 49424 KiB
random_08.txt AC 946 ms 38820 KiB
random_09.txt AC 908 ms 38784 KiB
random_10.txt AC 289 ms 38504 KiB
random_11.txt AC 281 ms 38628 KiB
random_12.txt AC 839 ms 38760 KiB
random_13.txt AC 4550 ms 48368 KiB
random_14.txt AC 879 ms 38816 KiB
random_15.txt AC 315 ms 38692 KiB
random_16.txt AC 276 ms 38688 KiB
random_17.txt AC 850 ms 38736 KiB
random_18.txt AC 863 ms 38576 KiB
random_19.txt AC 4580 ms 48636 KiB
random_20.txt AC 312 ms 38656 KiB
random_21.txt AC 274 ms 38628 KiB
random_22.txt AC 4473 ms 47944 KiB
random_23.txt AC 836 ms 38700 KiB
random_24.txt AC 862 ms 38768 KiB
random_25.txt AC 354 ms 38664 KiB
random_26.txt AC 297 ms 38492 KiB
random_27.txt AC 825 ms 38776 KiB
random_28.txt AC 4776 ms 49672 KiB
random_29.txt AC 836 ms 38708 KiB