#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define m_p make_pair
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)x.size()
#define fast_ioi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<long long,long long> pll;
template <class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template <class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
auto rng=bind(uniform_int_distribution<int>(1,1e9),mt19937(time(0)));
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
template <class T> using oset=tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N=1e5+3;
struct segtree{
pii t[4*N];
segtree(){
fill(t,t+4*N,m_p(-2e9,1));
}
void upd(int id,pii x,int v=1,int tl=0,int tr=N-1){
if(tl==tr){
umax(t[v],x);
// debug()<<imie(t[v])imie(x);
return;
}
int tm=(tl+tr)>>1;
if(tm>=id)
upd(id,x,2*v,tl,tm);
else
upd(id,x,2*v+1,tm+1,tr);
t[v]=max(t[2*v],t[2*v+1]);
}
pii get(int l,int r,int v=1,int tl=0,int tr=N-1){
if(tl>=l&&tr<=r) return t[v];
if(tl>r||tr<l) return {-2e9,1};
int tm=(tl+tr)>>1;
return max(get(l,r,2*v,tl,tm),get(l,r,2*v+1,tm+1,tr));
}
}seg;
struct fenwick{
int fnw[N];
fenwick(){
fill(fnw,fnw+N,0);
}
vec<pii>up;
void upd(int i,int x,bool t=1){
if(t) up.pb({i,-x});
while(i<N){
fnw[i]+=x;
i+=i&-i;
}
}
int get(int i){
int ans=0;
while(i>0){
ans+=fnw[i];
i-=i&-i;
}
return ans;
}
int get(int l,int r){
return get(r)-get(l-1);
}
void clr(){
// debug()<<imie(up);
for(auto &z : up)
upd(z.f,z.s,0);
up.clear();
}
}fnw;
vec<int> g[N];
int up[N][20],tin[N],tout[N],tt=1;
void dfs(int v,int p){
up[v][0]=p;
tin[v]=tt++;
// debug()<<imie()
for(int i=1;i<20;i++) up[v][i]=up[up[v][i-1]][i-1];
for(auto &z : g[v]){
if(z==p) continue;
dfs(z,v);
}
tout[v]=tt-1;
// debug()<<imie(v)imie(tin[v])imie(tout[v]);
}
signed main(){
fast_ioi;
// return 0;
int n;
cin>>n;
vec<int>a(n);
for(auto &z : a) cin>>z;
vec<int> kek=a;
sort(all(kek));kek.erase(unique(all(kek)),kek.end());
for(auto &z : a) z=lower_bound(all(kek),z)-kek.begin()+1;
vec<pii>e;
for(int i=1;i<n;i++){
int v, u;
cin >> v >> u;//--v; --u;
e.pb({v,u});
g[v].pb(u);
}
g[0].pb(1);
dfs(0,0);
seg.upd(tin[1],{-1,a[0]});
// return 0;
int sm=0;
for(int i=0;i<n-1;i++){
ll ans=0;
int v=e[i].f;
vec<pii>arr;
while(v){
/// find max u
int cnt=0;
for(int j=19;j>=0;j--){
if(up[v][j]!=0
&&
seg.get(tin[up[v][j]],tout[up[v][j]])==seg.get(tin[v],tout[v]))
v=up[v][j],cnt+=(1<<j);
}
arr.pb({seg.get(tin[v],tout[v]).s,cnt+1});
v=up[v][0];
}
sm+=sz(arr);
assert(sm<=20*n);
// debug()<<imie(arr);
for(auto &z : arr){
ans+=1ll*z.s*fnw.get(z.f-1);
fnw.upd(z.f,z.s);
}
fnw.clr();
cout<<ans<<'\n';
seg.upd(tin[e[i].s],{i,a[e[i].s-1]});
// if(i==1) return 0;
// for(int i=0;i<=n;i++)
// debug()<<imie(i)imie(seg.get(tin[i],tout[i]));
// debug()<<imie(*max_element(fnw.fnw,fnw.fnw+N));
// debug()<<imie()
}
return 0;
}
/*
5
4
0
0
0
1
2
3
0
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
3 ms |
6092 KB |
Output is correct |
3 |
Correct |
8 ms |
6164 KB |
Output is correct |
4 |
Correct |
4 ms |
6252 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
5 ms |
6220 KB |
Output is correct |
7 |
Correct |
4 ms |
6204 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
15 ms |
6276 KB |
Output is correct |
12 |
Correct |
13 ms |
6240 KB |
Output is correct |
13 |
Correct |
6 ms |
6220 KB |
Output is correct |
14 |
Correct |
5 ms |
6220 KB |
Output is correct |
15 |
Correct |
6 ms |
6220 KB |
Output is correct |
16 |
Correct |
6 ms |
6220 KB |
Output is correct |
17 |
Correct |
9 ms |
6220 KB |
Output is correct |
18 |
Correct |
7 ms |
6220 KB |
Output is correct |
19 |
Correct |
6 ms |
6220 KB |
Output is correct |
20 |
Correct |
6 ms |
6232 KB |
Output is correct |
21 |
Correct |
7 ms |
6276 KB |
Output is correct |
22 |
Correct |
5 ms |
6204 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6264 KB |
Output is correct |
25 |
Correct |
5 ms |
6272 KB |
Output is correct |
26 |
Correct |
4 ms |
6220 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
5 ms |
6168 KB |
Output is correct |
29 |
Correct |
5 ms |
6220 KB |
Output is correct |
30 |
Correct |
6 ms |
6256 KB |
Output is correct |
31 |
Correct |
6 ms |
6220 KB |
Output is correct |
32 |
Correct |
6 ms |
6220 KB |
Output is correct |
33 |
Correct |
11 ms |
6220 KB |
Output is correct |
34 |
Correct |
5 ms |
6220 KB |
Output is correct |
35 |
Correct |
5 ms |
6220 KB |
Output is correct |
36 |
Correct |
6 ms |
6220 KB |
Output is correct |
37 |
Correct |
6 ms |
6220 KB |
Output is correct |
38 |
Correct |
12 ms |
6264 KB |
Output is correct |
39 |
Correct |
10 ms |
6220 KB |
Output is correct |
40 |
Correct |
11 ms |
6272 KB |
Output is correct |
41 |
Correct |
5 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
6220 KB |
Output is correct |
43 |
Correct |
5 ms |
6220 KB |
Output is correct |
44 |
Correct |
6 ms |
6268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
3 ms |
6092 KB |
Output is correct |
3 |
Correct |
8 ms |
6164 KB |
Output is correct |
4 |
Correct |
4 ms |
6252 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
5 ms |
6220 KB |
Output is correct |
7 |
Correct |
4 ms |
6204 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
15 ms |
6276 KB |
Output is correct |
12 |
Correct |
13 ms |
6240 KB |
Output is correct |
13 |
Correct |
6 ms |
6220 KB |
Output is correct |
14 |
Correct |
5 ms |
6220 KB |
Output is correct |
15 |
Correct |
6 ms |
6220 KB |
Output is correct |
16 |
Correct |
6 ms |
6220 KB |
Output is correct |
17 |
Correct |
9 ms |
6220 KB |
Output is correct |
18 |
Correct |
7 ms |
6220 KB |
Output is correct |
19 |
Correct |
6 ms |
6220 KB |
Output is correct |
20 |
Correct |
6 ms |
6232 KB |
Output is correct |
21 |
Correct |
7 ms |
6276 KB |
Output is correct |
22 |
Correct |
5 ms |
6204 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6264 KB |
Output is correct |
25 |
Correct |
5 ms |
6272 KB |
Output is correct |
26 |
Correct |
4 ms |
6220 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
5 ms |
6168 KB |
Output is correct |
29 |
Correct |
5 ms |
6220 KB |
Output is correct |
30 |
Correct |
6 ms |
6256 KB |
Output is correct |
31 |
Correct |
6 ms |
6220 KB |
Output is correct |
32 |
Correct |
6 ms |
6220 KB |
Output is correct |
33 |
Correct |
11 ms |
6220 KB |
Output is correct |
34 |
Correct |
5 ms |
6220 KB |
Output is correct |
35 |
Correct |
5 ms |
6220 KB |
Output is correct |
36 |
Correct |
6 ms |
6220 KB |
Output is correct |
37 |
Correct |
6 ms |
6220 KB |
Output is correct |
38 |
Correct |
12 ms |
6264 KB |
Output is correct |
39 |
Correct |
10 ms |
6220 KB |
Output is correct |
40 |
Correct |
11 ms |
6272 KB |
Output is correct |
41 |
Correct |
5 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
6220 KB |
Output is correct |
43 |
Correct |
5 ms |
6220 KB |
Output is correct |
44 |
Correct |
6 ms |
6268 KB |
Output is correct |
45 |
Correct |
6 ms |
6220 KB |
Output is correct |
46 |
Correct |
26 ms |
6688 KB |
Output is correct |
47 |
Correct |
28 ms |
6700 KB |
Output is correct |
48 |
Correct |
23 ms |
6604 KB |
Output is correct |
49 |
Correct |
18 ms |
6924 KB |
Output is correct |
50 |
Correct |
69 ms |
6908 KB |
Output is correct |
51 |
Correct |
40 ms |
6860 KB |
Output is correct |
52 |
Correct |
48 ms |
6728 KB |
Output is correct |
53 |
Correct |
44 ms |
6792 KB |
Output is correct |
54 |
Correct |
22 ms |
6732 KB |
Output is correct |
55 |
Correct |
22 ms |
6732 KB |
Output is correct |
56 |
Correct |
34 ms |
6788 KB |
Output is correct |
57 |
Correct |
66 ms |
6668 KB |
Output is correct |
58 |
Correct |
54 ms |
6664 KB |
Output is correct |
59 |
Correct |
50 ms |
6600 KB |
Output is correct |
60 |
Correct |
49 ms |
6672 KB |
Output is correct |
61 |
Correct |
25 ms |
6732 KB |
Output is correct |
62 |
Correct |
25 ms |
6796 KB |
Output is correct |
63 |
Correct |
26 ms |
6732 KB |
Output is correct |
64 |
Correct |
26 ms |
6604 KB |
Output is correct |
65 |
Correct |
29 ms |
6684 KB |
Output is correct |
66 |
Correct |
29 ms |
6684 KB |
Output is correct |
67 |
Correct |
24 ms |
6684 KB |
Output is correct |
68 |
Correct |
22 ms |
6920 KB |
Output is correct |
69 |
Correct |
22 ms |
6784 KB |
Output is correct |
70 |
Correct |
22 ms |
6784 KB |
Output is correct |
71 |
Correct |
23 ms |
6780 KB |
Output is correct |
72 |
Correct |
51 ms |
6680 KB |
Output is correct |
73 |
Correct |
50 ms |
6656 KB |
Output is correct |
74 |
Correct |
26 ms |
6732 KB |
Output is correct |
75 |
Correct |
25 ms |
6736 KB |
Output is correct |
76 |
Correct |
41 ms |
6596 KB |
Output is correct |
77 |
Correct |
22 ms |
6604 KB |
Output is correct |
78 |
Correct |
22 ms |
6732 KB |
Output is correct |
79 |
Correct |
19 ms |
6732 KB |
Output is correct |
80 |
Correct |
21 ms |
6716 KB |
Output is correct |
81 |
Correct |
24 ms |
6740 KB |
Output is correct |
82 |
Correct |
24 ms |
6720 KB |
Output is correct |
83 |
Correct |
23 ms |
6604 KB |
Output is correct |
84 |
Correct |
22 ms |
6668 KB |
Output is correct |
85 |
Correct |
22 ms |
6732 KB |
Output is correct |
86 |
Correct |
22 ms |
6712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
Output is correct |
2 |
Correct |
3 ms |
6092 KB |
Output is correct |
3 |
Correct |
8 ms |
6164 KB |
Output is correct |
4 |
Correct |
4 ms |
6252 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
5 ms |
6220 KB |
Output is correct |
7 |
Correct |
4 ms |
6204 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
15 ms |
6276 KB |
Output is correct |
12 |
Correct |
13 ms |
6240 KB |
Output is correct |
13 |
Correct |
6 ms |
6220 KB |
Output is correct |
14 |
Correct |
5 ms |
6220 KB |
Output is correct |
15 |
Correct |
6 ms |
6220 KB |
Output is correct |
16 |
Correct |
6 ms |
6220 KB |
Output is correct |
17 |
Correct |
9 ms |
6220 KB |
Output is correct |
18 |
Correct |
7 ms |
6220 KB |
Output is correct |
19 |
Correct |
6 ms |
6220 KB |
Output is correct |
20 |
Correct |
6 ms |
6232 KB |
Output is correct |
21 |
Correct |
7 ms |
6276 KB |
Output is correct |
22 |
Correct |
5 ms |
6204 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6264 KB |
Output is correct |
25 |
Correct |
5 ms |
6272 KB |
Output is correct |
26 |
Correct |
4 ms |
6220 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
5 ms |
6168 KB |
Output is correct |
29 |
Correct |
5 ms |
6220 KB |
Output is correct |
30 |
Correct |
6 ms |
6256 KB |
Output is correct |
31 |
Correct |
6 ms |
6220 KB |
Output is correct |
32 |
Correct |
6 ms |
6220 KB |
Output is correct |
33 |
Correct |
11 ms |
6220 KB |
Output is correct |
34 |
Correct |
5 ms |
6220 KB |
Output is correct |
35 |
Correct |
5 ms |
6220 KB |
Output is correct |
36 |
Correct |
6 ms |
6220 KB |
Output is correct |
37 |
Correct |
6 ms |
6220 KB |
Output is correct |
38 |
Correct |
12 ms |
6264 KB |
Output is correct |
39 |
Correct |
10 ms |
6220 KB |
Output is correct |
40 |
Correct |
11 ms |
6272 KB |
Output is correct |
41 |
Correct |
5 ms |
6220 KB |
Output is correct |
42 |
Correct |
5 ms |
6220 KB |
Output is correct |
43 |
Correct |
5 ms |
6220 KB |
Output is correct |
44 |
Correct |
6 ms |
6268 KB |
Output is correct |
45 |
Correct |
6 ms |
6220 KB |
Output is correct |
46 |
Correct |
26 ms |
6688 KB |
Output is correct |
47 |
Correct |
28 ms |
6700 KB |
Output is correct |
48 |
Correct |
23 ms |
6604 KB |
Output is correct |
49 |
Correct |
18 ms |
6924 KB |
Output is correct |
50 |
Correct |
69 ms |
6908 KB |
Output is correct |
51 |
Correct |
40 ms |
6860 KB |
Output is correct |
52 |
Correct |
48 ms |
6728 KB |
Output is correct |
53 |
Correct |
44 ms |
6792 KB |
Output is correct |
54 |
Correct |
22 ms |
6732 KB |
Output is correct |
55 |
Correct |
22 ms |
6732 KB |
Output is correct |
56 |
Correct |
34 ms |
6788 KB |
Output is correct |
57 |
Correct |
66 ms |
6668 KB |
Output is correct |
58 |
Correct |
54 ms |
6664 KB |
Output is correct |
59 |
Correct |
50 ms |
6600 KB |
Output is correct |
60 |
Correct |
49 ms |
6672 KB |
Output is correct |
61 |
Correct |
25 ms |
6732 KB |
Output is correct |
62 |
Correct |
25 ms |
6796 KB |
Output is correct |
63 |
Correct |
26 ms |
6732 KB |
Output is correct |
64 |
Correct |
26 ms |
6604 KB |
Output is correct |
65 |
Correct |
29 ms |
6684 KB |
Output is correct |
66 |
Correct |
29 ms |
6684 KB |
Output is correct |
67 |
Correct |
24 ms |
6684 KB |
Output is correct |
68 |
Correct |
22 ms |
6920 KB |
Output is correct |
69 |
Correct |
22 ms |
6784 KB |
Output is correct |
70 |
Correct |
22 ms |
6784 KB |
Output is correct |
71 |
Correct |
23 ms |
6780 KB |
Output is correct |
72 |
Correct |
51 ms |
6680 KB |
Output is correct |
73 |
Correct |
50 ms |
6656 KB |
Output is correct |
74 |
Correct |
26 ms |
6732 KB |
Output is correct |
75 |
Correct |
25 ms |
6736 KB |
Output is correct |
76 |
Correct |
41 ms |
6596 KB |
Output is correct |
77 |
Correct |
22 ms |
6604 KB |
Output is correct |
78 |
Correct |
22 ms |
6732 KB |
Output is correct |
79 |
Correct |
19 ms |
6732 KB |
Output is correct |
80 |
Correct |
21 ms |
6716 KB |
Output is correct |
81 |
Correct |
24 ms |
6740 KB |
Output is correct |
82 |
Correct |
24 ms |
6720 KB |
Output is correct |
83 |
Correct |
23 ms |
6604 KB |
Output is correct |
84 |
Correct |
22 ms |
6668 KB |
Output is correct |
85 |
Correct |
22 ms |
6732 KB |
Output is correct |
86 |
Correct |
22 ms |
6712 KB |
Output is correct |
87 |
Correct |
72 ms |
7436 KB |
Output is correct |
88 |
Correct |
240 ms |
9772 KB |
Output is correct |
89 |
Correct |
1184 ms |
18384 KB |
Output is correct |
90 |
Correct |
1092 ms |
18260 KB |
Output is correct |
91 |
Correct |
1191 ms |
18256 KB |
Output is correct |
92 |
Correct |
488 ms |
24308 KB |
Output is correct |
93 |
Correct |
629 ms |
24472 KB |
Output is correct |
94 |
Correct |
564 ms |
24388 KB |
Output is correct |
95 |
Correct |
837 ms |
21796 KB |
Output is correct |
96 |
Correct |
810 ms |
21716 KB |
Output is correct |
97 |
Correct |
807 ms |
21664 KB |
Output is correct |
98 |
Correct |
822 ms |
21688 KB |
Output is correct |
99 |
Correct |
869 ms |
20544 KB |
Output is correct |
100 |
Execution timed out |
2079 ms |
18724 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |