forked from andreimargeloiu/Competitive-programming-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arb3.cpp
69 lines (59 loc) · 1.49 KB
/
arb3.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# include <fstream>
# include <algorithm>
# include <vector>
# define NR 100005
using namespace std;
vector <int> v[NR];
vector <long long> S[NR];
int i,j,n,X,Q;
int T[NR];
long long suma, val[NR], times;
bool cmp (int x, int y) {
return val[x] > val[y];
}
long long solve (int X, long long times) {
if (X==1) return times;
int ci,cs,mij, aux1, aux2;
long long newTimes, ajung;
ajung=val[X] - times;
ci=0; cs=v[T[X]].size()-1;
// caut suma
while (ci<=cs) {
mij=(ci+cs)/2;
if (val[v[T[X]][mij]]>=ajung) ci=mij+1, aux1=mij;
else cs=mij-1;
}
ci=0; cs=v[T[X]].size()-1;
while (ci<=cs) {
mij=(ci+cs)/2;
if (val[v[T[X]][mij]]>=val[X]) ci=mij+1, aux2=mij;
else cs=mij-1;
}
newTimes=S[T[X]][aux1] - (ajung)*(aux1+1) - (aux1-aux2);
return solve (T[X], newTimes);
}
int main ()
{
freopen ("arb3.in", "r", stdin);
freopen ("arb3.out", "w", stdout);
scanf ("%d%d", &n, &Q);
for (i=2; i<=n; ++i) {
scanf ("%d", &T[i]);
v[T[i]].push_back(i);
}
for (i=1; i<=n; ++i)
scanf ("%d", &val[i]);
for (i=1; i<=n; ++i) {
sort (v[i].begin(), v[i].end(), cmp);
suma=0;
for (j=0; j<v[i].size(); ++j) {
suma+=val[v[i][j]];
S[i].push_back(suma);
}
}
for (i=1; i<=Q; ++i) {
scanf ("%d%d", &X, ×);
printf ("%d\n", solve (X, times));
}
return 0;
}