// time_limit/yiping-qnlog2n.cpp
// Complexity: O(qnlog^2 n)
// One can change the 'map' in 'DS' into other DS to obtain a complexity of O(qnlog n)
#include<bits/stdc++.h>
#include"tree.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 3e5 + 5;
struct DS
{
map<int,ll> q;
void clear(void){ q.clear();}
void insert(int x,ll y){ q[x] += y;}
ll cutl(ll k)
{
ll res = 0;
while(q.size() && q.begin() -> second <= k)
{
auto t = *q.begin();
k -= t.second;
res += (ll)t.first * t.second;
q.erase(q.begin());
}
if(q.size() && k)
{
q.begin() -> second -= k;
res += (ll)q.begin() -> first * k;
}
return res;
}
void cutr(ll k)
{
while(q.size() && q.rbegin() -> second <= k)
{
k -= q.rbegin() -> second;
q.erase(prev(q.end()));
}
if(q.size() && k)
{
q.rbegin() -> second -= k;
}
}
void merge(DS &oth)
{
if(q.size() < oth.q.size())
q.swap(oth.q);
for(auto t: oth.q)
q[t.first] += t.second;
oth.q.clear();
}
void replace(int k)
{
ll len = 0;
while(q.size() && q.rbegin() -> first > k)
{
len += q.rbegin() -> second;
q.erase(prev(q.end()));
}
q[k] += len;
}
};
struct Hull
{
ll c, val, l, r;
DS hl, hr;
void init(void)
{
c = l = r = 0; val = 0;
hl.clear(); hr.clear();
}
void merge(Hull &oth)
{
c += oth.c; val += oth.val;
l += oth.l; r += oth.r;
hl.merge(oth.hl);
hr.merge(oth.hr);
}
void trim(int ql,int qr,int w)
{
if(r < qr) hr.insert(w, qr - r), r = qr;
if(l > ql) hl.insert(w, l - ql), l = ql;
hl.replace(w);
hr.replace(w);
if(c < ql)
{
hl.clear(); val += hr.cutl(ql - c); c = l = ql;
}
if(c > qr)
{
hr.clear(); val += hl.cutl(c - qr); c = r = qr;
}
if(r > qr) hr.cutr(r - qr), r = qr;
if(l < ql) hl.cutr(ql - l), l = ql;
}
void print(void)
{
printf("c = %lld, val = %lld, l = %lld, r = %lld\n",c,val,l,r);
printf("hl: ");
for(auto t: hl.q)
printf("(%d, %lld), ",t.first,t.second);
printf("\n");
printf("hr: ");
for(auto t: hr.q)
printf("(%d, %lld), ",t.first,t.second);
printf("\n");
}
};
int n;
vector<int> g[MAXN];
int anc[MAXN], wei[MAXN];
Hull f[MAXN];
ll query(int ql,int qr)
{
// printf("query: ql = %d, qr = %d\n",ql,qr);
for(int u=n; u>=1; --u)
{
f[u].init();
for(int v: g[u])
f[u].merge(f[v]);
f[u].trim(ql, qr, wei[u]);
// printf("g[%d]: ",u);
// for(auto v: g[u])
// printf("%d ",v);
// printf("\n");
//
// printf("f[%d]:\n",u);
// f[u].print();
// printf("\n");
}
return f[1].val;
}
void init(vector<int> _anc, vector<int> _wei)
{
n = (int)_anc.size();
for(int i=1; i<=n; ++i)
g[i].clear();
for(int i=1; i<=n; ++i)
{
anc[i] = _anc[i-1] + 1;
wei[i] = _wei[i-1];
if(i > 1)
g[anc[i]].emplace_back(i);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
44956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
54396 KB |
Output is correct |
2 |
Correct |
127 ms |
54408 KB |
Output is correct |
3 |
Correct |
228 ms |
51792 KB |
Output is correct |
4 |
Correct |
308 ms |
53584 KB |
Output is correct |
5 |
Correct |
249 ms |
53540 KB |
Output is correct |
6 |
Correct |
181 ms |
53528 KB |
Output is correct |
7 |
Correct |
272 ms |
53548 KB |
Output is correct |
8 |
Correct |
299 ms |
55100 KB |
Output is correct |
9 |
Correct |
258 ms |
52820 KB |
Output is correct |
10 |
Correct |
1117 ms |
61680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
45220 KB |
Output is correct |
2 |
Correct |
24 ms |
45096 KB |
Output is correct |
3 |
Correct |
21 ms |
44932 KB |
Output is correct |
4 |
Correct |
22 ms |
45148 KB |
Output is correct |
5 |
Correct |
24 ms |
45148 KB |
Output is correct |
6 |
Correct |
21 ms |
45148 KB |
Output is correct |
7 |
Correct |
30 ms |
44888 KB |
Output is correct |
8 |
Correct |
36 ms |
45136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
45220 KB |
Output is correct |
2 |
Correct |
24 ms |
45096 KB |
Output is correct |
3 |
Correct |
21 ms |
44932 KB |
Output is correct |
4 |
Correct |
22 ms |
45148 KB |
Output is correct |
5 |
Correct |
24 ms |
45148 KB |
Output is correct |
6 |
Correct |
21 ms |
45148 KB |
Output is correct |
7 |
Correct |
30 ms |
44888 KB |
Output is correct |
8 |
Correct |
36 ms |
45136 KB |
Output is correct |
9 |
Correct |
156 ms |
50808 KB |
Output is correct |
10 |
Correct |
159 ms |
52860 KB |
Output is correct |
11 |
Correct |
144 ms |
50300 KB |
Output is correct |
12 |
Correct |
113 ms |
47748 KB |
Output is correct |
13 |
Correct |
95 ms |
47196 KB |
Output is correct |
14 |
Correct |
88 ms |
47196 KB |
Output is correct |
15 |
Correct |
107 ms |
47440 KB |
Output is correct |
16 |
Correct |
138 ms |
47584 KB |
Output is correct |
17 |
Correct |
108 ms |
47448 KB |
Output is correct |
18 |
Correct |
96 ms |
47440 KB |
Output is correct |
19 |
Correct |
119 ms |
47488 KB |
Output is correct |
20 |
Correct |
90 ms |
47392 KB |
Output is correct |
21 |
Correct |
95 ms |
47224 KB |
Output is correct |
22 |
Correct |
214 ms |
49908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2027 ms |
55980 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2027 ms |
55980 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2013 ms |
63668 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
44956 KB |
Output is correct |
2 |
Correct |
273 ms |
54396 KB |
Output is correct |
3 |
Correct |
127 ms |
54408 KB |
Output is correct |
4 |
Correct |
228 ms |
51792 KB |
Output is correct |
5 |
Correct |
308 ms |
53584 KB |
Output is correct |
6 |
Correct |
249 ms |
53540 KB |
Output is correct |
7 |
Correct |
181 ms |
53528 KB |
Output is correct |
8 |
Correct |
272 ms |
53548 KB |
Output is correct |
9 |
Correct |
299 ms |
55100 KB |
Output is correct |
10 |
Correct |
258 ms |
52820 KB |
Output is correct |
11 |
Correct |
1117 ms |
61680 KB |
Output is correct |
12 |
Correct |
31 ms |
45220 KB |
Output is correct |
13 |
Correct |
24 ms |
45096 KB |
Output is correct |
14 |
Correct |
21 ms |
44932 KB |
Output is correct |
15 |
Correct |
22 ms |
45148 KB |
Output is correct |
16 |
Correct |
24 ms |
45148 KB |
Output is correct |
17 |
Correct |
21 ms |
45148 KB |
Output is correct |
18 |
Correct |
30 ms |
44888 KB |
Output is correct |
19 |
Correct |
36 ms |
45136 KB |
Output is correct |
20 |
Correct |
156 ms |
50808 KB |
Output is correct |
21 |
Correct |
159 ms |
52860 KB |
Output is correct |
22 |
Correct |
144 ms |
50300 KB |
Output is correct |
23 |
Correct |
113 ms |
47748 KB |
Output is correct |
24 |
Correct |
95 ms |
47196 KB |
Output is correct |
25 |
Correct |
88 ms |
47196 KB |
Output is correct |
26 |
Correct |
107 ms |
47440 KB |
Output is correct |
27 |
Correct |
138 ms |
47584 KB |
Output is correct |
28 |
Correct |
108 ms |
47448 KB |
Output is correct |
29 |
Correct |
96 ms |
47440 KB |
Output is correct |
30 |
Correct |
119 ms |
47488 KB |
Output is correct |
31 |
Correct |
90 ms |
47392 KB |
Output is correct |
32 |
Correct |
95 ms |
47224 KB |
Output is correct |
33 |
Correct |
214 ms |
49908 KB |
Output is correct |
34 |
Execution timed out |
2027 ms |
55980 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |