// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
struct LISAN : vector<int> {
void done() { sort(AI()); erase(unique(AI()), end()); }
int operator () (const int& v) const { return lower_bound(AI(), v) - begin(); }
};
struct BIT {
int n; vector<int> val;
BIT(int _n = 0): n(_n), val(n + 1, 0) {}
void modify(int p, int v) {
for (; p <= n; p += p & -p)
val[p] += v;
}
int query(int p) {
int r = 0;
for (; p > 0; p -= p & -p)
r += val[p];
return r;
}
};
const int kN = 100'001;
int N, C[kN], A[kN], B[kN];
int par[kN];
vector<int> chi[kN];
int dep[kN], siz[kN];
void dfs_siz(int u) {
siz[u] = 1;
for (int& v: chi[u]) {
dep[v] = dep[u] + 1;
dfs_siz(v);
siz[u] += siz[v];
if (siz[v] > siz[chi[u][0]])
swap(v, chi[u][0]);
}
}
vector<pii> chain[kN];
int head[kN];
void dfs_head(int u, int h) {
head[u] = h;
for (const int& v: chi[u])
dfs_head(v, v == chi[u][0] ? h : v);
if (chain[h].empty())
chain[h].pb(-1, dep[u] + 1);
if (chain[h].back().ff != C[u])
chain[h].pb(C[u], dep[u]);
else
chain[h].back().ss = dep[u];
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
LISAN lisan;
for (int i = 1; i <= N; ++i) {
cin >> C[i];
lisan.pb(C[i]);
}
lisan.done();
for (int i = 1; i <= N; ++i)
C[i] = lisan(C[i]) + 1;
for (int i = 1; i < N; ++i) {
cin >> A[i] >> B[i];
par[B[i]] = A[i];
chi[A[i]].pb(B[i]);
}
dfs_siz(1);
dfs_head(1, 1);
BIT bit(lisan.size());
vector<pii> vals, tmp;
for (int i = 1; i < N; ++i) {
int u = A[i];
do {
int h = head[u];
while (8e7 /* is stronger than me */ ) {
pii cur = chain[h].back();
if (cur.ss > dep[u]) break;
chain[h].pop_back();
if (chain[h].back().ss > dep[u]) {
tmp.pb(cur.ff, dep[u] - cur.ss + 1);
if (chain[h].back().ss > dep[u] + 1)
chain[h].pb(cur.ff, dep[u] + 1);
break;
}
tmp.pb(cur.ff, chain[h].back().ss - cur.ss);
}
while (!tmp.empty()) {
vals.push_back(tmp.back());
tmp.pop_back();
}
u = par[h];
} while (u != 0);
ll ans = 0;
for (const auto& [val, num]: vals) {
ans += 1ll * bit.query(val - 1) * num;
bit.modify(val, num);
}
cout << ans << '\n';
for (const auto& [val, num]: vals)
bit.modify(val, -num);
vals.clear();
u = A[i];
do {
int h = head[u];
chain[h].pb(C[B[i]], dep[h]);
u = par[h];
} while (u != 0);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5084 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5088 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
4960 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5036 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
2 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
4988 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5084 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5088 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
4960 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5036 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
2 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
4988 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
6 ms |
5324 KB |
Output is correct |
47 |
Correct |
6 ms |
5388 KB |
Output is correct |
48 |
Correct |
8 ms |
5332 KB |
Output is correct |
49 |
Correct |
5 ms |
5580 KB |
Output is correct |
50 |
Correct |
5 ms |
5580 KB |
Output is correct |
51 |
Correct |
5 ms |
5596 KB |
Output is correct |
52 |
Correct |
5 ms |
5452 KB |
Output is correct |
53 |
Correct |
5 ms |
5452 KB |
Output is correct |
54 |
Correct |
5 ms |
5452 KB |
Output is correct |
55 |
Correct |
5 ms |
5452 KB |
Output is correct |
56 |
Correct |
7 ms |
5488 KB |
Output is correct |
57 |
Correct |
9 ms |
5324 KB |
Output is correct |
58 |
Correct |
8 ms |
5324 KB |
Output is correct |
59 |
Correct |
7 ms |
5324 KB |
Output is correct |
60 |
Correct |
10 ms |
5300 KB |
Output is correct |
61 |
Correct |
5 ms |
5452 KB |
Output is correct |
62 |
Correct |
7 ms |
5452 KB |
Output is correct |
63 |
Correct |
7 ms |
5452 KB |
Output is correct |
64 |
Correct |
5 ms |
5324 KB |
Output is correct |
65 |
Correct |
6 ms |
5324 KB |
Output is correct |
66 |
Correct |
6 ms |
5324 KB |
Output is correct |
67 |
Correct |
6 ms |
5324 KB |
Output is correct |
68 |
Correct |
4 ms |
5580 KB |
Output is correct |
69 |
Correct |
5 ms |
5452 KB |
Output is correct |
70 |
Correct |
5 ms |
5452 KB |
Output is correct |
71 |
Correct |
5 ms |
5452 KB |
Output is correct |
72 |
Correct |
7 ms |
5324 KB |
Output is correct |
73 |
Correct |
7 ms |
5336 KB |
Output is correct |
74 |
Correct |
7 ms |
5448 KB |
Output is correct |
75 |
Correct |
7 ms |
5324 KB |
Output is correct |
76 |
Correct |
8 ms |
5324 KB |
Output is correct |
77 |
Correct |
5 ms |
5324 KB |
Output is correct |
78 |
Correct |
5 ms |
5392 KB |
Output is correct |
79 |
Correct |
5 ms |
5324 KB |
Output is correct |
80 |
Correct |
5 ms |
5324 KB |
Output is correct |
81 |
Correct |
5 ms |
5324 KB |
Output is correct |
82 |
Correct |
5 ms |
5324 KB |
Output is correct |
83 |
Correct |
5 ms |
5324 KB |
Output is correct |
84 |
Correct |
5 ms |
5340 KB |
Output is correct |
85 |
Correct |
4 ms |
5324 KB |
Output is correct |
86 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5084 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5088 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
4960 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5036 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
2 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
4988 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
6 ms |
5324 KB |
Output is correct |
47 |
Correct |
6 ms |
5388 KB |
Output is correct |
48 |
Correct |
8 ms |
5332 KB |
Output is correct |
49 |
Correct |
5 ms |
5580 KB |
Output is correct |
50 |
Correct |
5 ms |
5580 KB |
Output is correct |
51 |
Correct |
5 ms |
5596 KB |
Output is correct |
52 |
Correct |
5 ms |
5452 KB |
Output is correct |
53 |
Correct |
5 ms |
5452 KB |
Output is correct |
54 |
Correct |
5 ms |
5452 KB |
Output is correct |
55 |
Correct |
5 ms |
5452 KB |
Output is correct |
56 |
Correct |
7 ms |
5488 KB |
Output is correct |
57 |
Correct |
9 ms |
5324 KB |
Output is correct |
58 |
Correct |
8 ms |
5324 KB |
Output is correct |
59 |
Correct |
7 ms |
5324 KB |
Output is correct |
60 |
Correct |
10 ms |
5300 KB |
Output is correct |
61 |
Correct |
5 ms |
5452 KB |
Output is correct |
62 |
Correct |
7 ms |
5452 KB |
Output is correct |
63 |
Correct |
7 ms |
5452 KB |
Output is correct |
64 |
Correct |
5 ms |
5324 KB |
Output is correct |
65 |
Correct |
6 ms |
5324 KB |
Output is correct |
66 |
Correct |
6 ms |
5324 KB |
Output is correct |
67 |
Correct |
6 ms |
5324 KB |
Output is correct |
68 |
Correct |
4 ms |
5580 KB |
Output is correct |
69 |
Correct |
5 ms |
5452 KB |
Output is correct |
70 |
Correct |
5 ms |
5452 KB |
Output is correct |
71 |
Correct |
5 ms |
5452 KB |
Output is correct |
72 |
Correct |
7 ms |
5324 KB |
Output is correct |
73 |
Correct |
7 ms |
5336 KB |
Output is correct |
74 |
Correct |
7 ms |
5448 KB |
Output is correct |
75 |
Correct |
7 ms |
5324 KB |
Output is correct |
76 |
Correct |
8 ms |
5324 KB |
Output is correct |
77 |
Correct |
5 ms |
5324 KB |
Output is correct |
78 |
Correct |
5 ms |
5392 KB |
Output is correct |
79 |
Correct |
5 ms |
5324 KB |
Output is correct |
80 |
Correct |
5 ms |
5324 KB |
Output is correct |
81 |
Correct |
5 ms |
5324 KB |
Output is correct |
82 |
Correct |
5 ms |
5324 KB |
Output is correct |
83 |
Correct |
5 ms |
5324 KB |
Output is correct |
84 |
Correct |
5 ms |
5340 KB |
Output is correct |
85 |
Correct |
4 ms |
5324 KB |
Output is correct |
86 |
Correct |
5 ms |
5324 KB |
Output is correct |
87 |
Correct |
13 ms |
5708 KB |
Output is correct |
88 |
Correct |
35 ms |
7232 KB |
Output is correct |
89 |
Correct |
148 ms |
12584 KB |
Output is correct |
90 |
Correct |
141 ms |
12552 KB |
Output is correct |
91 |
Correct |
144 ms |
12664 KB |
Output is correct |
92 |
Correct |
85 ms |
20420 KB |
Output is correct |
93 |
Correct |
98 ms |
20776 KB |
Output is correct |
94 |
Correct |
84 ms |
20804 KB |
Output is correct |
95 |
Correct |
89 ms |
17584 KB |
Output is correct |
96 |
Correct |
105 ms |
17828 KB |
Output is correct |
97 |
Correct |
106 ms |
17708 KB |
Output is correct |
98 |
Correct |
85 ms |
17680 KB |
Output is correct |
99 |
Correct |
92 ms |
16712 KB |
Output is correct |
100 |
Correct |
187 ms |
13216 KB |
Output is correct |
101 |
Correct |
200 ms |
13072 KB |
Output is correct |
102 |
Correct |
212 ms |
13236 KB |
Output is correct |
103 |
Correct |
214 ms |
13120 KB |
Output is correct |
104 |
Correct |
95 ms |
16692 KB |
Output is correct |
105 |
Correct |
93 ms |
16576 KB |
Output is correct |
106 |
Correct |
120 ms |
16684 KB |
Output is correct |
107 |
Correct |
120 ms |
12568 KB |
Output is correct |
108 |
Correct |
114 ms |
12548 KB |
Output is correct |
109 |
Correct |
150 ms |
12808 KB |
Output is correct |
110 |
Correct |
72 ms |
20580 KB |
Output is correct |
111 |
Correct |
82 ms |
17720 KB |
Output is correct |
112 |
Correct |
71 ms |
17320 KB |
Output is correct |
113 |
Correct |
70 ms |
16292 KB |
Output is correct |
114 |
Correct |
203 ms |
13232 KB |
Output is correct |
115 |
Correct |
151 ms |
12728 KB |
Output is correct |
116 |
Correct |
82 ms |
16296 KB |
Output is correct |
117 |
Correct |
91 ms |
14636 KB |
Output is correct |
118 |
Correct |
100 ms |
14124 KB |
Output is correct |
119 |
Correct |
87 ms |
13608 KB |
Output is correct |
120 |
Correct |
72 ms |
14124 KB |
Output is correct |
121 |
Correct |
90 ms |
13724 KB |
Output is correct |
122 |
Correct |
82 ms |
13260 KB |
Output is correct |
123 |
Correct |
97 ms |
14728 KB |
Output is correct |
124 |
Correct |
103 ms |
14152 KB |
Output is correct |
125 |
Correct |
100 ms |
13720 KB |
Output is correct |
126 |
Correct |
92 ms |
14248 KB |
Output is correct |
127 |
Correct |
82 ms |
13868 KB |
Output is correct |
128 |
Correct |
87 ms |
13352 KB |
Output is correct |