#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const ll maxn = 1e5 + 10;
struct checkpoll
{
ll p, c;
void read()
{
cin >> p >> c;
}
bool operator < (const checkpoll &t) const
{
return c < t.c;
}
} cp[maxn];
struct citizen
{
ll s, t, x, y;
void read()
{
cin >> s >> t >> x >> y;
}
} ct[maxn];
ll n, m, q;
vector < ll > adj[maxn];
pair < ll, ll > road[maxn];
void input()
{
cin >> n >> m >> q;
for (ll i = 1; i < n; i ++)
{
ll a, b;
cin >> a >> b;
road[i] = {a, b};
adj[a].push_back(b);
adj[b].push_back(a);
}
for (ll i = 1; i <= m; i ++)
cp[i].read();
for (ll i = 1; i <= q; i ++)
ct[i].read();
}
ll tin[maxn], tout[maxn], timer, occ[2 * maxn];
ll par[maxn], depth[maxn];
void euler_tour(ll v = 1, ll p = 0)
{
occ[++ timer] = v;
tin[v] = timer;
for (ll u : adj[v])
{
if (u == p)
continue;
depth[u] = depth[v] + 1;
par[u] = v;
euler_tour(u, v);
occ[++ timer] = v;
}
tout[v] = timer;
}
const ll maxlog = 20;
struct sparse_table
{
ll lg[2 * maxn], dp[maxlog][2 * maxn];
void build_sparse_table()
{
for (ll i = 1; i <= timer; i ++)
{
lg[i] = lg[i / 2] + 1;
dp[0][i] = occ[i];
}
for (ll j = 1; j < lg[timer]; j ++)
{
for (ll i = 1; i <= timer - (1 << j) + 1; i ++)
{
dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
if (depth[dp[j - 1][i]] < depth[dp[j][i]])
dp[j][i] = dp[j - 1][i];
}
}
/**cout << "---------------" << endl;
for (ll j = 0; j < lg[timer]; j ++, cout << endl)
for (ll i = 1; i <= timer; i ++)
cout << dp[j][i] << " ";*/
}
ll lca_query(ll v, ll u)
{
ll l = tin[v], r = tin[u];
if (l > r)
swap(l, r);
ll len = lg[r - l + 1] - 1, lca = dp[len][r - (1 << len) + 1];
///cout << "lca " << v << " " << u << " " << l << " " << r << endl;
if (depth[dp[len][l]] < depth[lca])
lca = dp[len][l];
return lca;
}
};
sparse_table st;
struct fenwick
{
ll fen[2 * maxn];
void add(ll pos, ll val)
{
for (ll i = pos; i <= timer; i += (i & -i))
fen[i] += val;
}
ll sum(ll pos)
{
ll s = 0;
for (ll i = pos; i > 0; i -= (i & -i))
s += fen[i];
return s;
}
void range_update(ll left, ll right, ll val)
{
add(left, val);
add(right + 1, - val);
}
ll query(ll pos)
{
return sum(pos);
}
};
fenwick gold, silver;
pair < ll, ll > range[maxn];
vector < ll > upd[maxn];
ll binary_position(ll val)
{
ll left = 1, right = m;
while(left <= right)
{
ll mid = left + (right - left) / 2;
if (cp[mid].c <= val)
left = mid + 1;
else
right = mid - 1;
}
return right;
}
ll ans[maxn];
pair < ll, ll > avab[maxn];
void parallel_binary_search()
{
for (ll i = 1; i <= q; i ++)
{
range[i] = {0, 1e9 + 10};
}
for (ll i = 1; i < n; i ++)
{
if (depth[road[i].first] < depth[road[i].second])
swap(road[i].first, road[i].second);
}
/**for (ll i = 1; i <= m; i ++)
{
cout << road[cp[i].p].first << endl;
}*/
sort(cp + 1, cp + m + 1);
while(true)
{
bool done = true;
for (ll i = 0; i <= m; i ++)
upd[i].clear();
for (ll i = 1; i <= q; i ++)
{
if (range[i].first <= range[i].second)
{
done = false;
ll mid = (range[i].first + (range[i].second - range[i].first) / 2);
ll pos = binary_position(mid);
upd[pos].push_back(i);
}
}
if (done)
break;
//for (ll i = 1; i <= m; i ++)
// gold.range_update(tin[cp[i].p], tout[cp[i].p], 1);
/**cout << "-------------" << endl;
for (ll i = 1; i <= m; i ++)
cout << range[i].first << " " << range[i].second << endl;*/
for (ll i = 0; i <= m; i ++)
{
if (i != 0)
{
silver.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], cp[i].c);
}
for (ll idx : upd[i])
{
ll lca = st.lca_query(ct[idx].s, ct[idx].t);
ll val = silver.query(tin[ct[idx].s]) + silver.query(tin[ct[idx].t]);
val = val - 2 * silver.query(tin[lca]);
///co
///cout << idx << " : " << val << " -- " << lca << endl;
/**if (idx == 6)
{
cout << ct[idx].s << " " << ct[idx].t << " " << val << " " << lca << " " << i << endl;
}*/
ll mid = (range[idx].first + (range[idx].second - range[idx].first) / 2);
if (val > ct[idx].y)
range[idx].second = mid - 1;
else
range[idx].first = mid + 1;
}
}
for (ll i = 1; i <= m; i ++)
silver.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], -cp[i].c);
}
for (ll i = 0; i <= m; i ++)
upd[i].clear();
for (ll i = 1; i <= q; i ++)
{
ll pos = binary_position(range[i].first);
upd[pos].push_back(i);
}
for (ll i = 1; i <= m; i ++)
{
gold.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], 1);
}
for (ll i = 0; i <= m; i ++)
{
if (i != 0)
{
silver.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], cp[i].c);
gold.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], -1);
}
/**cout << "step" << endl;
cout << i << endl;
for (ll j = 1; j <= n; j ++)
cout << gold.query(tin[j]) << " ";
cout << endl;
cout << "order" << endl;
for (ll j = 1; j <= timer; j ++)
cout << occ[j] << " ";
cout << endl;*/
for (ll idx : upd[i])
{
ll lca = st.lca_query(ct[idx].s, ct[idx].t);
ll val = silver.query(tin[ct[idx].s]) + silver.query(tin[ct[idx].t]);
val = val - 2 * silver.query(tin[lca]);
ll min_gold = gold.query(tin[ct[idx].s]) + gold.query(tin[ct[idx].t]);
///cout << "gold " << min_gold << " " << gold.query(tinendl;
min_gold = min_gold - 2 * gold.query(tin[lca]);
//cout << idx << " : " << min_gold << " " << i << " lca " << lca << endl;
avab[idx].second = min_gold;
}
}
for (ll i = 1; i <= m; i ++)
silver.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], -cp[i].c);
for (ll i = 0; i <= m; i ++)
upd[i].clear();
for (ll i = 1; i <= q; i ++)
{
ll pos = binary_position(range[i].second);
upd[pos].push_back(i);
}
for (ll i = 1; i <= m; i ++)
{
gold.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], 1);
}
for (ll i = 0; i <= m; i ++)
{
if (i != 0)
{
silver.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], cp[i].c);
gold.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], -1);
}
/**cout << "step" << endl;
cout << i << endl;
for (ll j = 1; j <= n; j ++)
cout << gold.query(tin[j]) << " ";
cout << endl;
cout << "order" << endl;
for (ll j = 1; j <= timer; j ++)
cout << occ[j] << " ";
cout << endl;*/
for (ll idx : upd[i])
{
ll lca = st.lca_query(ct[idx].s, ct[idx].t);
ll val = silver.query(tin[ct[idx].s]) + silver.query(tin[ct[idx].t]);
val = val - 2 * silver.query(tin[lca]);
ll min_gold = gold.query(tin[ct[idx].s]) + gold.query(tin[ct[idx].t]);
///cout << "gold " << min_gold << " " << gold.query(tinendl;
min_gold = min_gold - 2 * gold.query(tin[lca]);
//cout << idx << " : " << min_gold << " " << i << " lca " << lca << endl;
ll to_fill = min_gold - avab[idx].second, cnt = range[idx].first;
ll cn = min(to_fill, (ct[idx].y - val) / cnt);
ll left_gold = ct[idx].x - min_gold + cn;
if (left_gold < 0)
left_gold = - 1;
ans[idx] = left_gold;
}
}
for (ll i = 1; i <= m; i ++)
silver.range_update(tin[road[cp[i].p].first], tout[road[cp[i].p].first], -cp[i].c);
for (ll i = 1; i <= q; i ++)
cout << ans[i] << endl;
}
void solve()
{
input();
euler_tour(1, 0);
st.build_sparse_table();
parallel_binary_search();
}
int main()
{
speed();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
11 ms |
5972 KB |
Output is correct |
6 |
Correct |
14 ms |
6256 KB |
Output is correct |
7 |
Correct |
12 ms |
5952 KB |
Output is correct |
8 |
Correct |
16 ms |
6260 KB |
Output is correct |
9 |
Correct |
15 ms |
6264 KB |
Output is correct |
10 |
Correct |
16 ms |
6256 KB |
Output is correct |
11 |
Correct |
16 ms |
6272 KB |
Output is correct |
12 |
Correct |
16 ms |
6268 KB |
Output is correct |
13 |
Correct |
16 ms |
6356 KB |
Output is correct |
14 |
Correct |
16 ms |
6336 KB |
Output is correct |
15 |
Correct |
16 ms |
6352 KB |
Output is correct |
16 |
Correct |
16 ms |
6360 KB |
Output is correct |
17 |
Correct |
19 ms |
6272 KB |
Output is correct |
18 |
Correct |
16 ms |
6364 KB |
Output is correct |
19 |
Correct |
14 ms |
6356 KB |
Output is correct |
20 |
Correct |
16 ms |
6356 KB |
Output is correct |
21 |
Correct |
15 ms |
6348 KB |
Output is correct |
22 |
Correct |
14 ms |
6356 KB |
Output is correct |
23 |
Correct |
14 ms |
6280 KB |
Output is correct |
24 |
Correct |
14 ms |
6356 KB |
Output is correct |
25 |
Correct |
14 ms |
6348 KB |
Output is correct |
26 |
Correct |
12 ms |
6352 KB |
Output is correct |
27 |
Correct |
11 ms |
6336 KB |
Output is correct |
28 |
Correct |
12 ms |
6356 KB |
Output is correct |
29 |
Correct |
11 ms |
6240 KB |
Output is correct |
30 |
Correct |
12 ms |
6052 KB |
Output is correct |
31 |
Correct |
11 ms |
6100 KB |
Output is correct |
32 |
Correct |
11 ms |
6100 KB |
Output is correct |
33 |
Correct |
17 ms |
6416 KB |
Output is correct |
34 |
Correct |
16 ms |
6384 KB |
Output is correct |
35 |
Correct |
16 ms |
6464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
11 ms |
5960 KB |
Output is correct |
3 |
Correct |
12 ms |
6076 KB |
Output is correct |
4 |
Correct |
16 ms |
6132 KB |
Output is correct |
5 |
Correct |
855 ms |
57220 KB |
Output is correct |
6 |
Correct |
817 ms |
46672 KB |
Output is correct |
7 |
Correct |
807 ms |
52380 KB |
Output is correct |
8 |
Correct |
628 ms |
51360 KB |
Output is correct |
9 |
Correct |
629 ms |
50636 KB |
Output is correct |
10 |
Correct |
1014 ms |
61616 KB |
Output is correct |
11 |
Correct |
1008 ms |
61612 KB |
Output is correct |
12 |
Correct |
987 ms |
61628 KB |
Output is correct |
13 |
Correct |
1026 ms |
61692 KB |
Output is correct |
14 |
Correct |
1056 ms |
61628 KB |
Output is correct |
15 |
Correct |
1072 ms |
67564 KB |
Output is correct |
16 |
Correct |
1041 ms |
67964 KB |
Output is correct |
17 |
Correct |
1032 ms |
67212 KB |
Output is correct |
18 |
Correct |
1140 ms |
61496 KB |
Output is correct |
19 |
Correct |
1081 ms |
61496 KB |
Output is correct |
20 |
Correct |
1004 ms |
61588 KB |
Output is correct |
21 |
Correct |
939 ms |
60896 KB |
Output is correct |
22 |
Correct |
984 ms |
61292 KB |
Output is correct |
23 |
Correct |
966 ms |
61272 KB |
Output is correct |
24 |
Correct |
977 ms |
61284 KB |
Output is correct |
25 |
Correct |
741 ms |
62060 KB |
Output is correct |
26 |
Correct |
760 ms |
62012 KB |
Output is correct |
27 |
Correct |
689 ms |
62004 KB |
Output is correct |
28 |
Correct |
695 ms |
61376 KB |
Output is correct |
29 |
Correct |
672 ms |
61420 KB |
Output is correct |
30 |
Correct |
801 ms |
61620 KB |
Output is correct |
31 |
Correct |
747 ms |
61620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
16 ms |
6356 KB |
Output is correct |
3 |
Correct |
17 ms |
6356 KB |
Output is correct |
4 |
Correct |
16 ms |
6356 KB |
Output is correct |
5 |
Correct |
1156 ms |
70796 KB |
Output is correct |
6 |
Correct |
927 ms |
69684 KB |
Output is correct |
7 |
Correct |
1202 ms |
58084 KB |
Output is correct |
8 |
Correct |
1926 ms |
84604 KB |
Output is correct |
9 |
Correct |
1997 ms |
84616 KB |
Output is correct |
10 |
Correct |
2037 ms |
84580 KB |
Output is correct |
11 |
Correct |
1548 ms |
84696 KB |
Output is correct |
12 |
Correct |
1557 ms |
84520 KB |
Output is correct |
13 |
Correct |
1570 ms |
84696 KB |
Output is correct |
14 |
Correct |
1390 ms |
83976 KB |
Output is correct |
15 |
Correct |
1263 ms |
84160 KB |
Output is correct |
16 |
Correct |
1483 ms |
84416 KB |
Output is correct |
17 |
Correct |
1376 ms |
84380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
11 ms |
5972 KB |
Output is correct |
6 |
Correct |
14 ms |
6256 KB |
Output is correct |
7 |
Correct |
12 ms |
5952 KB |
Output is correct |
8 |
Correct |
16 ms |
6260 KB |
Output is correct |
9 |
Correct |
15 ms |
6264 KB |
Output is correct |
10 |
Correct |
16 ms |
6256 KB |
Output is correct |
11 |
Correct |
16 ms |
6272 KB |
Output is correct |
12 |
Correct |
16 ms |
6268 KB |
Output is correct |
13 |
Correct |
16 ms |
6356 KB |
Output is correct |
14 |
Correct |
16 ms |
6336 KB |
Output is correct |
15 |
Correct |
16 ms |
6352 KB |
Output is correct |
16 |
Correct |
16 ms |
6360 KB |
Output is correct |
17 |
Correct |
19 ms |
6272 KB |
Output is correct |
18 |
Correct |
16 ms |
6364 KB |
Output is correct |
19 |
Correct |
14 ms |
6356 KB |
Output is correct |
20 |
Correct |
16 ms |
6356 KB |
Output is correct |
21 |
Correct |
15 ms |
6348 KB |
Output is correct |
22 |
Correct |
14 ms |
6356 KB |
Output is correct |
23 |
Correct |
14 ms |
6280 KB |
Output is correct |
24 |
Correct |
14 ms |
6356 KB |
Output is correct |
25 |
Correct |
14 ms |
6348 KB |
Output is correct |
26 |
Correct |
12 ms |
6352 KB |
Output is correct |
27 |
Correct |
11 ms |
6336 KB |
Output is correct |
28 |
Correct |
12 ms |
6356 KB |
Output is correct |
29 |
Correct |
11 ms |
6240 KB |
Output is correct |
30 |
Correct |
12 ms |
6052 KB |
Output is correct |
31 |
Correct |
11 ms |
6100 KB |
Output is correct |
32 |
Correct |
11 ms |
6100 KB |
Output is correct |
33 |
Correct |
17 ms |
6416 KB |
Output is correct |
34 |
Correct |
16 ms |
6384 KB |
Output is correct |
35 |
Correct |
16 ms |
6464 KB |
Output is correct |
36 |
Correct |
2 ms |
5076 KB |
Output is correct |
37 |
Correct |
11 ms |
5960 KB |
Output is correct |
38 |
Correct |
12 ms |
6076 KB |
Output is correct |
39 |
Correct |
16 ms |
6132 KB |
Output is correct |
40 |
Correct |
855 ms |
57220 KB |
Output is correct |
41 |
Correct |
817 ms |
46672 KB |
Output is correct |
42 |
Correct |
807 ms |
52380 KB |
Output is correct |
43 |
Correct |
628 ms |
51360 KB |
Output is correct |
44 |
Correct |
629 ms |
50636 KB |
Output is correct |
45 |
Correct |
1014 ms |
61616 KB |
Output is correct |
46 |
Correct |
1008 ms |
61612 KB |
Output is correct |
47 |
Correct |
987 ms |
61628 KB |
Output is correct |
48 |
Correct |
1026 ms |
61692 KB |
Output is correct |
49 |
Correct |
1056 ms |
61628 KB |
Output is correct |
50 |
Correct |
1072 ms |
67564 KB |
Output is correct |
51 |
Correct |
1041 ms |
67964 KB |
Output is correct |
52 |
Correct |
1032 ms |
67212 KB |
Output is correct |
53 |
Correct |
1140 ms |
61496 KB |
Output is correct |
54 |
Correct |
1081 ms |
61496 KB |
Output is correct |
55 |
Correct |
1004 ms |
61588 KB |
Output is correct |
56 |
Correct |
939 ms |
60896 KB |
Output is correct |
57 |
Correct |
984 ms |
61292 KB |
Output is correct |
58 |
Correct |
966 ms |
61272 KB |
Output is correct |
59 |
Correct |
977 ms |
61284 KB |
Output is correct |
60 |
Correct |
741 ms |
62060 KB |
Output is correct |
61 |
Correct |
760 ms |
62012 KB |
Output is correct |
62 |
Correct |
689 ms |
62004 KB |
Output is correct |
63 |
Correct |
695 ms |
61376 KB |
Output is correct |
64 |
Correct |
672 ms |
61420 KB |
Output is correct |
65 |
Correct |
801 ms |
61620 KB |
Output is correct |
66 |
Correct |
747 ms |
61620 KB |
Output is correct |
67 |
Correct |
3 ms |
5120 KB |
Output is correct |
68 |
Correct |
16 ms |
6356 KB |
Output is correct |
69 |
Correct |
17 ms |
6356 KB |
Output is correct |
70 |
Correct |
16 ms |
6356 KB |
Output is correct |
71 |
Correct |
1156 ms |
70796 KB |
Output is correct |
72 |
Correct |
927 ms |
69684 KB |
Output is correct |
73 |
Correct |
1202 ms |
58084 KB |
Output is correct |
74 |
Correct |
1926 ms |
84604 KB |
Output is correct |
75 |
Correct |
1997 ms |
84616 KB |
Output is correct |
76 |
Correct |
2037 ms |
84580 KB |
Output is correct |
77 |
Correct |
1548 ms |
84696 KB |
Output is correct |
78 |
Correct |
1557 ms |
84520 KB |
Output is correct |
79 |
Correct |
1570 ms |
84696 KB |
Output is correct |
80 |
Correct |
1390 ms |
83976 KB |
Output is correct |
81 |
Correct |
1263 ms |
84160 KB |
Output is correct |
82 |
Correct |
1483 ms |
84416 KB |
Output is correct |
83 |
Correct |
1376 ms |
84380 KB |
Output is correct |
84 |
Correct |
1153 ms |
60312 KB |
Output is correct |
85 |
Correct |
984 ms |
50296 KB |
Output is correct |
86 |
Correct |
882 ms |
49480 KB |
Output is correct |
87 |
Correct |
1770 ms |
77960 KB |
Output is correct |
88 |
Correct |
1808 ms |
78008 KB |
Output is correct |
89 |
Correct |
1795 ms |
78268 KB |
Output is correct |
90 |
Correct |
1700 ms |
78236 KB |
Output is correct |
91 |
Correct |
1783 ms |
78136 KB |
Output is correct |
92 |
Correct |
2037 ms |
82148 KB |
Output is correct |
93 |
Correct |
1889 ms |
83580 KB |
Output is correct |
94 |
Correct |
1771 ms |
78088 KB |
Output is correct |
95 |
Correct |
1808 ms |
78420 KB |
Output is correct |
96 |
Correct |
1755 ms |
78028 KB |
Output is correct |
97 |
Correct |
1716 ms |
78128 KB |
Output is correct |
98 |
Correct |
1578 ms |
78220 KB |
Output is correct |
99 |
Correct |
1599 ms |
77828 KB |
Output is correct |
100 |
Correct |
1572 ms |
78220 KB |
Output is correct |
101 |
Correct |
1540 ms |
78592 KB |
Output is correct |
102 |
Correct |
1266 ms |
78548 KB |
Output is correct |
103 |
Correct |
1248 ms |
78712 KB |
Output is correct |
104 |
Correct |
1261 ms |
78368 KB |
Output is correct |
105 |
Correct |
856 ms |
74932 KB |
Output is correct |
106 |
Correct |
775 ms |
76632 KB |
Output is correct |
107 |
Correct |
929 ms |
75396 KB |
Output is correct |
108 |
Correct |
907 ms |
74728 KB |
Output is correct |