Submission #49506487


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair<int, int> PII;
const int MAXN = 212345;
const int inf = 1123456789;
//const ll inf = 1000000000000000018ll;
const LL mod = 998244353;
#define rep(i, l, r) for (int i=(l); i<=(r); i++)
#define per(i, r, l) for (int i=(r); i>=(l); i--)
#define mem(a, b) memset(a, b, sizeof(a))

int N, in[MAXN], out[MAXN], clk;
vector<int> nxt[MAXN];
LL ansn[MAXN];

int rt[MAXN];
struct segmentTree {
	#define lson ls[x]
	#define rson rs[x]
	int tot, cnt[MAXN<<5], ls[MAXN<<5], rs[MAXN<<5];
	segmentTree() {
        tot = 0, mem(cnt, 0), mem(ls, 0), mem(rs, 0);
	}
    void pushup(int x) { cnt[x] = cnt[lson] + cnt[rson]; }
	void update(int &x, int l, int r, int p) {
		if (!x) x = ++tot;
		if (l==r) cnt[x] = 1;
        else {
			int mid = (l + r) >> 1;
			if (mid>=p) update(lson, l, mid, p);
			if (mid< p) update(rson, mid+1, r, p);
			pushup(x);
		}
	}
    int query(int x, int l, int r, int p) { // <=p
        if (!x) return 0;
        if (r<=p) return cnt[x];
        else {
            int mid = (l + r) >> 1;
            if (mid>=p) return query(lson, l, mid, p);
            else return cnt[lson] + query(rson, mid+1, r, p);
        }
    }
	int merge(int x, int y, int l, int r) {
		if (!x) return y;
		if (!y) return x;
		if (l==r) {
			cnt[x] += cnt[y];
			return x;
		} else {
			int mid = (l + r) >> 1;
			ls[x] = merge(ls[x], ls[y], l, mid);
			rs[x] = merge(rs[x], rs[y], mid+1, r);
			pushup(x);
			return x;
		}
	}
} ST;

void dfs1(int x, int f)
{
    in[x] = ++clk;
    for (auto v: nxt[x]) if (v!=f) dfs1(v, x);
    out[x] = clk;
}
void dfs2(int x, int f)
{
    ST.update(rt[x], 1, N, x);
    int sumk = 0;
    for (auto v: nxt[x]) if (v!=f) {
        dfs2(v, x);
        int k = ST.query(rt[v], 1, N, x);
        ansn[1] += k;
        ansn[in[v]] -= k;
        ansn[out[v]+1] += k;
        sumk += k;
        ST.merge(rt[x], rt[v], 1, N);
    }
    ansn[in[x]] += x-1-sumk;
    ansn[out[x]+1] -= x-1-sumk;
}
int main()
{
    scanf("%d", &N);
    rep(i, 2, N) {
        int a, b; scanf("%d%d", &a, &b);
        nxt[a].push_back(b), nxt[b].push_back(a);
    }
    dfs1(1, 0);
    for (int i=1; i<=N; i++) rt[i] = i; ST.tot = N;
    dfs2(1, 0);
    rep(i, 1, N) ansn[i] += ansn[i-1];
    rep(i, 1, N) printf("%lld ", ansn[in[i]]);
}

Submission Info

Submission Time
Task G - Tree Inversion
User zhyh
Language C++ 20 (gcc 12.2)
Score 600
Code Size 2463 Byte
Status AC
Exec Time 280 ms
Memory 129628 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:93:5: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
   93 |     for (int i=1; i<=N; i++) rt[i] = i; ST.tot = N;
      |     ^~~
Main.cpp:93:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’
   93 |     for (int i=1; i<=N; i++) rt[i] = i; ST.tot = N;
      |                                         ^~
Main.cpp:87:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   87 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
Main.cpp:89:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   89 |         int a, b; scanf("%d%d", &a, &b);
      |                   ~~~~~^~~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 72
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 02_handmade_64.txt, 02_handmade_65.txt, 02_handmade_66.txt, 02_handmade_67.txt, 02_handmade_68.txt, 02_handmade_69.txt, 02_handmade_70.txt, 02_handmade_71.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 32 ms 83312 KiB
00_sample_01.txt AC 32 ms 83280 KiB
00_sample_02.txt AC 32 ms 83312 KiB
01_random_03.txt AC 210 ms 98700 KiB
01_random_04.txt AC 279 ms 126068 KiB
01_random_05.txt AC 229 ms 98088 KiB
01_random_06.txt AC 223 ms 98536 KiB
01_random_07.txt AC 205 ms 98716 KiB
01_random_08.txt AC 273 ms 116688 KiB
01_random_09.txt AC 230 ms 98160 KiB
01_random_10.txt AC 221 ms 98308 KiB
01_random_11.txt AC 198 ms 98824 KiB
01_random_12.txt AC 257 ms 126212 KiB
01_random_13.txt AC 234 ms 98156 KiB
01_random_14.txt AC 234 ms 98408 KiB
01_random_15.txt AC 200 ms 98720 KiB
01_random_16.txt AC 279 ms 117528 KiB
01_random_17.txt AC 245 ms 98384 KiB
01_random_18.txt AC 246 ms 98604 KiB
01_random_19.txt AC 204 ms 98636 KiB
01_random_20.txt AC 280 ms 125744 KiB
01_random_21.txt AC 243 ms 98320 KiB
01_random_22.txt AC 237 ms 98604 KiB
01_random_23.txt AC 206 ms 98720 KiB
01_random_24.txt AC 273 ms 123056 KiB
01_random_25.txt AC 252 ms 98160 KiB
01_random_26.txt AC 232 ms 98480 KiB
01_random_27.txt AC 219 ms 98796 KiB
01_random_28.txt AC 271 ms 123132 KiB
01_random_29.txt AC 241 ms 98264 KiB
01_random_30.txt AC 239 ms 98536 KiB
01_random_31.txt AC 35 ms 83964 KiB
01_random_32.txt AC 73 ms 93880 KiB
01_random_33.txt AC 136 ms 92192 KiB
01_random_34.txt AC 217 ms 97664 KiB
01_random_35.txt AC 123 ms 92080 KiB
01_random_36.txt AC 208 ms 96716 KiB
01_random_37.txt AC 46 ms 85056 KiB
01_random_38.txt AC 86 ms 89860 KiB
01_random_39.txt AC 51 ms 87240 KiB
01_random_40.txt AC 66 ms 87000 KiB
01_random_41.txt AC 193 ms 96156 KiB
01_random_42.txt AC 79 ms 88772 KiB
01_random_43.txt AC 127 ms 91664 KiB
01_random_44.txt AC 227 ms 97552 KiB
01_random_45.txt AC 53 ms 86112 KiB
01_random_46.txt AC 117 ms 96824 KiB
01_random_47.txt AC 37 ms 83868 KiB
01_random_48.txt AC 209 ms 97448 KiB
01_random_49.txt AC 115 ms 92708 KiB
01_random_50.txt AC 130 ms 91816 KiB
01_random_51.txt AC 98 ms 89792 KiB
01_random_52.txt AC 204 ms 98708 KiB
01_random_53.txt AC 256 ms 123736 KiB
01_random_54.txt AC 234 ms 98156 KiB
01_random_55.txt AC 228 ms 98348 KiB
01_random_56.txt AC 223 ms 98712 KiB
01_random_57.txt AC 260 ms 117608 KiB
01_random_58.txt AC 234 ms 98380 KiB
01_random_59.txt AC 234 ms 98344 KiB
01_random_60.txt AC 214 ms 98760 KiB
01_random_61.txt AC 278 ms 125960 KiB
01_random_62.txt AC 247 ms 98264 KiB
01_random_63.txt AC 242 ms 98408 KiB
02_handmade_64.txt AC 32 ms 83408 KiB
02_handmade_65.txt AC 32 ms 83284 KiB
02_handmade_66.txt AC 181 ms 129560 KiB
02_handmade_67.txt AC 176 ms 129496 KiB
02_handmade_68.txt AC 178 ms 129432 KiB
02_handmade_69.txt AC 177 ms 129628 KiB
02_handmade_70.txt AC 173 ms 129624 KiB
02_handmade_71.txt AC 188 ms 129496 KiB