Fork me on GitHub

Codeforces 894D(DFS+二分)

Codeforces 894D
题意:给出一颗带边权完全二叉树,有$m$个询问,每个询问有$A_i,H_i$,求从$A_i$出发分别到其他点$x$的$H_i-dis(A_i,x)$和

待更,这题不好讲

Codeforces Submission

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
using namespace std;
LL n, m, L[1000000 + 5];
vector<LL> dis[1000000 + 5], qzh[1000000 + 5];
LL query(LL a, LL h) {
if (h < 0) return 0;
LL p = upper_bound(dis[a].begin(), dis[a].end(), h) - dis[a].begin();
return p * h - qzh[a][p - 1];
}
void clean() {}
void solve() {
clean();
for (LL i = 2; i <= n; i++) scanf("%I64d", &L[i]);
for (LL x = n; x >= 1; x--) {
dis[x].push_back(0);
LL lc = (x << 1), rc = (x << 1 | 1);
if (lc <= n) for (LL i = 0; i < (LL)dis[lc].size(); i++) dis[x].push_back(dis[lc][i] + L[lc]);
if (rc <= n) for (LL i = 0; i < (LL)dis[rc].size(); i++) dis[x].push_back(dis[rc][i] + L[rc]);
sort(dis[x].begin(), dis[x].end());
qzh[x].resize(dis[x].size());
qzh[x][0] = 0;
for (LL i = 1; i < qzh[x].size(); i++) qzh[x][i] = qzh[x][i - 1] + dis[x][i];
}
while (m--) {
LL a, h; scanf("%I64d%I64d", &a, &h);
LL ans = 0, lst = 0;
while (a) {
if (h < 0) break;
ans += h;
LL lc = (a << 1), rc = (a << 1 | 1);
if (lc <= n && lst != lc) ans += query(lc, h - L[lc]);
if (rc <= n && lst != rc) ans += query(rc, h - L[rc]);
lst = a, h -= L[a], a >>= 1;
}
printf("%I64d\n", ans);
}
}
int main() {
scanf("%I64d%I64d", &n, &m), solve();
return 0;
}

------ 本文结束 ------