Submission #482011

# Submission time Handle Problem Language Result Execution time Memory
482011 2021-10-22T15:18:59 Z leaked Construction of Highway (JOI18_construction) C++14
16 / 100
1899 ms 37572 KB
#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});
            if(v!=1)
                assert(seg.get(tin[v],tout[v])!=seg.get(tin[up[v][0]],tout[up[v][0]]));
            v=up[v][0];
        }
        sm+=sz(arr);
        assert(sm<=10*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
*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6092 KB Output is correct
2 Correct 3 ms 6092 KB Output is correct
3 Correct 3 ms 6092 KB Output is correct
4 Correct 4 ms 6220 KB Output is correct
5 Correct 6 ms 6176 KB Output is correct
6 Correct 6 ms 6220 KB Output is correct
7 Correct 5 ms 6220 KB Output is correct
8 Correct 4 ms 6220 KB Output is correct
9 Correct 5 ms 6220 KB Output is correct
10 Correct 4 ms 6220 KB Output is correct
11 Correct 5 ms 6220 KB Output is correct
12 Correct 5 ms 6220 KB Output is correct
13 Correct 5 ms 6220 KB Output is correct
14 Correct 5 ms 6220 KB Output is correct
15 Correct 8 ms 6220 KB Output is correct
16 Correct 8 ms 6220 KB Output is correct
17 Correct 7 ms 6220 KB Output is correct
18 Correct 8 ms 6348 KB Output is correct
19 Correct 7 ms 6220 KB Output is correct
20 Correct 6 ms 6220 KB Output is correct
21 Correct 5 ms 6220 KB Output is correct
22 Correct 6 ms 6220 KB Output is correct
23 Correct 5 ms 6220 KB Output is correct
24 Correct 5 ms 6220 KB Output is correct
25 Correct 5 ms 6220 KB Output is correct
26 Correct 4 ms 6220 KB Output is correct
27 Correct 5 ms 6276 KB Output is correct
28 Correct 5 ms 6220 KB Output is correct
29 Correct 5 ms 6220 KB Output is correct
30 Correct 7 ms 6220 KB Output is correct
31 Correct 7 ms 6220 KB Output is correct
32 Correct 5 ms 6220 KB Output is correct
33 Correct 5 ms 6220 KB Output is correct
34 Correct 4 ms 6220 KB Output is correct
35 Correct 4 ms 6220 KB Output is correct
36 Correct 5 ms 6220 KB Output is correct
37 Correct 4 ms 6220 KB Output is correct
38 Correct 4 ms 6220 KB Output is correct
39 Correct 4 ms 6220 KB Output is correct
40 Correct 5 ms 6220 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 5 ms 6220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6092 KB Output is correct
2 Correct 3 ms 6092 KB Output is correct
3 Correct 3 ms 6092 KB Output is correct
4 Correct 4 ms 6220 KB Output is correct
5 Correct 6 ms 6176 KB Output is correct
6 Correct 6 ms 6220 KB Output is correct
7 Correct 5 ms 6220 KB Output is correct
8 Correct 4 ms 6220 KB Output is correct
9 Correct 5 ms 6220 KB Output is correct
10 Correct 4 ms 6220 KB Output is correct
11 Correct 5 ms 6220 KB Output is correct
12 Correct 5 ms 6220 KB Output is correct
13 Correct 5 ms 6220 KB Output is correct
14 Correct 5 ms 6220 KB Output is correct
15 Correct 8 ms 6220 KB Output is correct
16 Correct 8 ms 6220 KB Output is correct
17 Correct 7 ms 6220 KB Output is correct
18 Correct 8 ms 6348 KB Output is correct
19 Correct 7 ms 6220 KB Output is correct
20 Correct 6 ms 6220 KB Output is correct
21 Correct 5 ms 6220 KB Output is correct
22 Correct 6 ms 6220 KB Output is correct
23 Correct 5 ms 6220 KB Output is correct
24 Correct 5 ms 6220 KB Output is correct
25 Correct 5 ms 6220 KB Output is correct
26 Correct 4 ms 6220 KB Output is correct
27 Correct 5 ms 6276 KB Output is correct
28 Correct 5 ms 6220 KB Output is correct
29 Correct 5 ms 6220 KB Output is correct
30 Correct 7 ms 6220 KB Output is correct
31 Correct 7 ms 6220 KB Output is correct
32 Correct 5 ms 6220 KB Output is correct
33 Correct 5 ms 6220 KB Output is correct
34 Correct 4 ms 6220 KB Output is correct
35 Correct 4 ms 6220 KB Output is correct
36 Correct 5 ms 6220 KB Output is correct
37 Correct 4 ms 6220 KB Output is correct
38 Correct 4 ms 6220 KB Output is correct
39 Correct 4 ms 6220 KB Output is correct
40 Correct 5 ms 6220 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 5 ms 6220 KB Output is correct
45 Correct 9 ms 6220 KB Output is correct
46 Correct 28 ms 6684 KB Output is correct
47 Correct 31 ms 6604 KB Output is correct
48 Correct 27 ms 6604 KB Output is correct
49 Correct 15 ms 6860 KB Output is correct
50 Correct 14 ms 6920 KB Output is correct
51 Correct 15 ms 6924 KB Output is correct
52 Correct 23 ms 6732 KB Output is correct
53 Correct 23 ms 6732 KB Output is correct
54 Correct 23 ms 6804 KB Output is correct
55 Correct 23 ms 6780 KB Output is correct
56 Correct 25 ms 6784 KB Output is correct
57 Correct 62 ms 6676 KB Output is correct
58 Correct 62 ms 6680 KB Output is correct
59 Correct 65 ms 6792 KB Output is correct
60 Correct 64 ms 6668 KB Output is correct
61 Correct 28 ms 6788 KB Output is correct
62 Correct 30 ms 6788 KB Output is correct
63 Correct 26 ms 6788 KB Output is correct
64 Correct 31 ms 6672 KB Output is correct
65 Correct 29 ms 6596 KB Output is correct
66 Correct 31 ms 6680 KB Output is correct
67 Correct 28 ms 6604 KB Output is correct
68 Correct 15 ms 6860 KB Output is correct
69 Correct 28 ms 6780 KB Output is correct
70 Correct 23 ms 6808 KB Output is correct
71 Correct 24 ms 6788 KB Output is correct
72 Correct 64 ms 6660 KB Output is correct
73 Correct 62 ms 6676 KB Output is correct
74 Correct 27 ms 6732 KB Output is correct
75 Correct 23 ms 6732 KB Output is correct
76 Correct 21 ms 6724 KB Output is correct
77 Correct 20 ms 6604 KB Output is correct
78 Correct 21 ms 6740 KB Output is correct
79 Correct 21 ms 6716 KB Output is correct
80 Correct 20 ms 6664 KB Output is correct
81 Correct 25 ms 6752 KB Output is correct
82 Correct 24 ms 6732 KB Output is correct
83 Correct 24 ms 6728 KB Output is correct
84 Correct 24 ms 6736 KB Output is correct
85 Correct 25 ms 6724 KB Output is correct
86 Correct 23 ms 6716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6092 KB Output is correct
2 Correct 3 ms 6092 KB Output is correct
3 Correct 3 ms 6092 KB Output is correct
4 Correct 4 ms 6220 KB Output is correct
5 Correct 6 ms 6176 KB Output is correct
6 Correct 6 ms 6220 KB Output is correct
7 Correct 5 ms 6220 KB Output is correct
8 Correct 4 ms 6220 KB Output is correct
9 Correct 5 ms 6220 KB Output is correct
10 Correct 4 ms 6220 KB Output is correct
11 Correct 5 ms 6220 KB Output is correct
12 Correct 5 ms 6220 KB Output is correct
13 Correct 5 ms 6220 KB Output is correct
14 Correct 5 ms 6220 KB Output is correct
15 Correct 8 ms 6220 KB Output is correct
16 Correct 8 ms 6220 KB Output is correct
17 Correct 7 ms 6220 KB Output is correct
18 Correct 8 ms 6348 KB Output is correct
19 Correct 7 ms 6220 KB Output is correct
20 Correct 6 ms 6220 KB Output is correct
21 Correct 5 ms 6220 KB Output is correct
22 Correct 6 ms 6220 KB Output is correct
23 Correct 5 ms 6220 KB Output is correct
24 Correct 5 ms 6220 KB Output is correct
25 Correct 5 ms 6220 KB Output is correct
26 Correct 4 ms 6220 KB Output is correct
27 Correct 5 ms 6276 KB Output is correct
28 Correct 5 ms 6220 KB Output is correct
29 Correct 5 ms 6220 KB Output is correct
30 Correct 7 ms 6220 KB Output is correct
31 Correct 7 ms 6220 KB Output is correct
32 Correct 5 ms 6220 KB Output is correct
33 Correct 5 ms 6220 KB Output is correct
34 Correct 4 ms 6220 KB Output is correct
35 Correct 4 ms 6220 KB Output is correct
36 Correct 5 ms 6220 KB Output is correct
37 Correct 4 ms 6220 KB Output is correct
38 Correct 4 ms 6220 KB Output is correct
39 Correct 4 ms 6220 KB Output is correct
40 Correct 5 ms 6220 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 5 ms 6220 KB Output is correct
45 Correct 9 ms 6220 KB Output is correct
46 Correct 28 ms 6684 KB Output is correct
47 Correct 31 ms 6604 KB Output is correct
48 Correct 27 ms 6604 KB Output is correct
49 Correct 15 ms 6860 KB Output is correct
50 Correct 14 ms 6920 KB Output is correct
51 Correct 15 ms 6924 KB Output is correct
52 Correct 23 ms 6732 KB Output is correct
53 Correct 23 ms 6732 KB Output is correct
54 Correct 23 ms 6804 KB Output is correct
55 Correct 23 ms 6780 KB Output is correct
56 Correct 25 ms 6784 KB Output is correct
57 Correct 62 ms 6676 KB Output is correct
58 Correct 62 ms 6680 KB Output is correct
59 Correct 65 ms 6792 KB Output is correct
60 Correct 64 ms 6668 KB Output is correct
61 Correct 28 ms 6788 KB Output is correct
62 Correct 30 ms 6788 KB Output is correct
63 Correct 26 ms 6788 KB Output is correct
64 Correct 31 ms 6672 KB Output is correct
65 Correct 29 ms 6596 KB Output is correct
66 Correct 31 ms 6680 KB Output is correct
67 Correct 28 ms 6604 KB Output is correct
68 Correct 15 ms 6860 KB Output is correct
69 Correct 28 ms 6780 KB Output is correct
70 Correct 23 ms 6808 KB Output is correct
71 Correct 24 ms 6788 KB Output is correct
72 Correct 64 ms 6660 KB Output is correct
73 Correct 62 ms 6676 KB Output is correct
74 Correct 27 ms 6732 KB Output is correct
75 Correct 23 ms 6732 KB Output is correct
76 Correct 21 ms 6724 KB Output is correct
77 Correct 20 ms 6604 KB Output is correct
78 Correct 21 ms 6740 KB Output is correct
79 Correct 21 ms 6716 KB Output is correct
80 Correct 20 ms 6664 KB Output is correct
81 Correct 25 ms 6752 KB Output is correct
82 Correct 24 ms 6732 KB Output is correct
83 Correct 24 ms 6728 KB Output is correct
84 Correct 24 ms 6736 KB Output is correct
85 Correct 25 ms 6724 KB Output is correct
86 Correct 23 ms 6716 KB Output is correct
87 Correct 83 ms 7372 KB Output is correct
88 Correct 277 ms 9804 KB Output is correct
89 Correct 1287 ms 18564 KB Output is correct
90 Correct 1286 ms 18292 KB Output is correct
91 Correct 1316 ms 18232 KB Output is correct
92 Correct 500 ms 24440 KB Output is correct
93 Correct 488 ms 24492 KB Output is correct
94 Correct 503 ms 24308 KB Output is correct
95 Correct 815 ms 21956 KB Output is correct
96 Correct 797 ms 21704 KB Output is correct
97 Correct 817 ms 21800 KB Output is correct
98 Correct 800 ms 21804 KB Output is correct
99 Correct 833 ms 20772 KB Output is correct
100 Runtime error 1899 ms 37572 KB Execution killed with signal 6
101 Halted 0 ms 0 KB -