constint N = 1e6 + 10; int n, m; int fa[N][23]; int val[N]; int head[N], nxt[N], to[N], e_tot; int col[N];
inlineintread() { int res = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + c - 48; c = getchar(); } return res * f; }
voidchange(int x, int v){ for (int i = x; i <= n; i += lowbit(i)) { val[i] += v; } }
intquery(int x, int y){ int res = 0; for (int i = y; i; i -= lowbit(i)) { res += val[i]; } for (int i = x - 1; i; i -= lowbit(i)) { res -= val[i]; } return res; }
int dfn[N], tim, id[N], siz[N];
voiddfs(int u){ dfn[u] = ++tim; id[tim] = u; siz[u] = 1; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; dfs(v); siz[u] += siz[v]; } }
intmain(){ n = read(), m = read(); for (int i = 2; i <= n; ++i) { fa[i][0] = read(); add(fa[i][0], i); } dfs(1); for (int k = 1; k <= 21; ++k) { for (int i = 1; i <= n; ++i) { fa[i][k] = fa[fa[i][k - 1]][k - 1]; } } while (m--) { int x; x = read(); if (x > 0) { if (!col[x]) { change(dfn[x], 1); } elsechange(dfn[x], -1); col[x] ^= 1; } else { x = -x; int u = x; if (!query(1, n)) { puts("0"); continue; } elseif (query(dfn[u], dfn[u] + siz[u] - 1)){ printf("%d\n", u); continue; } else { for (int k = 21; ~k; --k) { if (fa[u][k] && !query(dfn[fa[u][k]], dfn[fa[u][k]] + siz[fa[u][k]] - 1)) { u = fa[u][k]; } } printf("%d\n", fa[u][0]); } } } return0; }
#include<iostream> #include<cstdio> #include<algorithm> #define ll long long
usingnamespace std;
constint N = 50; int n; ll k; int tot; ll ans; int h[N], g[N]; ll tmp[2100000]; int ttot; int val[2100000]; int hei; ll coin;
structPair{ ll x; int y; bool op; booloperator < (const Pair A) const { if (x == A.x) { return op > A.op; } return x < A.x; } }p[2100000];
boolcheck(int st, int _n){ hei = 0x3f3f3f3f, coin = 0; int last = 0; for (int i = 0; i < _n; ++i) { if (st >> i & 1) { if (h[i + 1] >= h[last]) { last = i + 1; hei = h[i + 1]; coin += g[i + 1]; } elsereturn0; } } return1; }
boolcheck2(int st, int _n){ hei = 0x3f3f3f3f, coin = 0; int last = 0; for (int i = 0; i < _n; ++i) { if (st >> i & 1) { if (h[i + 1 + n / 2] >= h[last]) { if (!last) hei = h[i + 1 + n / 2]; coin += g[i + 1 + n / 2]; last = i + 1 + n / 2; } elsereturn0; } } return1; }
intlowbit(int x){ return x & (-x); }
voidchange(int x, int v){ for (int i = x; i <= tot; i += lowbit(i)) { val[i] += v; } }
ll query(int x, int y){ ll res = 0; for (int i = y; i; i -= lowbit(i)) { res += val[i]; } for (int i = x - 1; i; i -= lowbit(i)) { res -= val[i]; } return res; }
signedmain(){ scanf("%d%lld", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d%d", &h[i], &g[i]); } p[++tot].x = k; p[tot].y = 0; p[tot].op = 1; for (int st = 1; st < (1 << n / 2); ++st) { if (check(st, n / 2)) { ++tot; p[tot].x = k - coin; p[tot].y = hei; p[tot].op = 1; } } p[++tot].x = 0; p[tot].y = 0x3f3f3f3f; p[tot].op = 0; for (int st = 1; st < (1 << (n - n / 2)); ++st) { if (check2(st, n - n / 2)) { ++tot; p[tot].x = coin; p[tot].y = hei; p[tot].op = 0; } } for (int i = 1; i <= tot; ++i) { tmp[i] = p[i].x; } sort(tmp + 1, tmp + tot + 1); for (int i = 1; i <= tot; ++i) { p[i].x = lower_bound(tmp + 1, tmp + tot + 1, p[i].x) - tmp; } for (int i = 1; i <= tot; ++i) { tmp[i] = p[i].y; } sort(tmp + 1, tmp + tot + 1); for (int i = 1; i <= tot; ++i) { p[i].y = lower_bound(tmp + 1, tmp + tot + 1, p[i].y) - tmp; } sort(p + 1, p + tot + 1); for (int i = 1; i <= tot; ++i) { if (p[i].op == 1) { change(p[i].y, 1); } else { ans += query(1, p[i].y); } } cout << ans << endl; return0; }
constint N = 1e6 + 10; int n, m; int fa[N][23]; int val[N]; int head[N], nxt[N], to[N], e_tot; int col[N];
inlineintread() { int res = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + c - 48; c = getchar(); } return res * f; }
voidchange(int x, int v){ for (int i = x; i <= n; i += lowbit(i)) { val[i] += v; } }
intquery(int x, int y){ int res = 0; for (int i = y; i; i -= lowbit(i)) { res += val[i]; } for (int i = x - 1; i; i -= lowbit(i)) { res -= val[i]; } return res; }
int dfn[N], tim, id[N], siz[N];
voiddfs(int u){ dfn[u] = ++tim; id[tim] = u; siz[u] = 1; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; dfs(v); siz[u] += siz[v]; } }
intmain(){ n = read(), m = read(); for (int i = 2; i <= n; ++i) { fa[i][0] = read(); add(fa[i][0], i); } dfs(1); for (int k = 1; k <= 21; ++k) { for (int i = 1; i <= n; ++i) { fa[i][k] = fa[fa[i][k - 1]][k - 1]; } } while (m--) { int x; x = read(); if (x > 0) { if (!col[x]) { change(dfn[x], 1); } elsechange(dfn[x], -1); col[x] ^= 1; } else { x = -x; int u = x; if (!query(1, n)) { puts("0"); continue; } elseif (query(dfn[u], dfn[u] + siz[u] - 1)){ printf("%d\n", u); continue; } else { for (int k = 21; ~k; --k) { if (fa[u][k] && !query(dfn[fa[u][k]], dfn[fa[u][k]] + siz[fa[u][k]] - 1)) { u = fa[u][k]; } } printf("%d\n", fa[u][0]); } } } return0; }