Submission #867204

# Submission time Handle Problem Language Result Execution time Memory
867204 2023-10-27T21:05:28 Z azberjibiou Construction of Highway (JOI18_construction) C++17
100 / 100
239 ms 84564 KB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=100030;
const int mxM=500004;
const int MOD=998244353;
const ll INF=8e18;
typedef struct fenwick{
    int fen[mxN], n;
    void set_n(int m){n=m;}
    void init(){for(int i=0;i<=n+1;i++) fen[i]=0;}
    void upd(int idx, int val){
        while(idx<=n)   fen[idx]+=val, idx+=(idx&(-idx));
    }
    int solv(int idx){
        int res=0;
        while(idx)  res+=fen[idx], idx-=(idx&(-idx));
        return res;
    }
}fenwick;
int N;
int C[mxN];
vector <pii> E;
vector <int> v[mxN];
int dep[mxN], sub[mxN], g[mxN], par[mxN];
stack <pii> stk[mxN];
fenwick T1;
void input(){
    cin >> N;
    for(int i=1;i<=N;i++)   cin >> C[i];
    E.resize(N-1);
    for(auto &[x, y] : E)
    {
        cin >> x >> y;
        v[x].push_back(y);
        par[y]=x;
    }
}
void dfs1(int now){
    sub[now]=1;
    for(int nxt : v[now])
    {
        dep[nxt]=dep[now]+1;
        dfs1(nxt);
        sub[now]+=sub[nxt];
    }
}
void dfs2(int now){
    if(v[now].empty())  return;
    sort(all(v[now]), [](int a, int b){return sub[a]>sub[b];});
    for(int nxt : v[now])   g[nxt]=nxt;
    g[v[now][0]]=g[now];
    for(int nxt : v[now])   dfs2(nxt);
}
void heavy(int now, vector <pii> &ct, int col)
{
    vector <pii> nv;
    int idx=g[now], nl=dep[now]-dep[idx]+1;
    while(!stk[idx].empty())
    {
        auto [nc, len]=stk[idx].top();
        stk[idx].pop();
        if(nl>len)  nv.emplace_back(nc, len), nl-=len;
        else
        {
            nv.emplace_back(nc, nl);
            if(len!=nl) stk[idx].emplace(nc, len-nl);
            break;
        }
    }
    stk[idx].emplace(col, dep[now]-dep[idx]+1);
    reverse(all(nv));
    for(pii ele : nv)   ct.push_back(ele);
}
ll inv_count(vector <pii> &ct)
{
    ll res=0;
    vector <int> cr;
    cr.resize(ct.size());
    for(int i=0;i<ct.size();i++)    cr[i]=ct[i].fi;
    sort(all(cr));
    cr.erase(unique(all(cr)), cr.end());
    for(auto &[x, y] : ct)    x=lower_bound(all(cr), x)-cr.begin()+1;
    T1.set_n(cr.size()+1);
    T1.init();
    for(auto [col, cnt] : ct)
    {
        res+=(ll)T1.solv(col-1)*cnt;
        T1.upd(col, cnt);
    }
    return res;
}
ll qry(int now){
    vector <pii> ct;
    int np=now;
    if(g[now]==now) stk[now].emplace(C[now], 1), np=par[now];
    while(np){
        heavy(np, ct, C[now]);
        np=par[g[np]];
    }
    return inv_count(ct);
}
int main()
{
    gibon
    input();
    dfs1(1);
    g[1]=1;
    dfs2(1);
    stk[1].emplace(C[1], 1);
    for(auto [x, y] : E)    cout << qry(y) << '\n';
}

Compilation message

construction.cpp: In function 'll inv_count(std::vector<std::pair<int, int> >&)':
construction.cpp:88:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for(int i=0;i<ct.size();i++)    cr[i]=ct[i].fi;
      |                 ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 32 ms 72016 KB Output is correct
2 Correct 36 ms 72020 KB Output is correct
3 Correct 34 ms 72284 KB Output is correct
4 Correct 32 ms 72284 KB Output is correct
5 Correct 32 ms 72280 KB Output is correct
6 Correct 32 ms 72268 KB Output is correct
7 Correct 40 ms 72272 KB Output is correct
8 Correct 36 ms 72272 KB Output is correct
9 Correct 37 ms 72284 KB Output is correct
10 Correct 34 ms 72280 KB Output is correct
11 Correct 36 ms 72284 KB Output is correct
12 Correct 33 ms 72092 KB Output is correct
13 Correct 32 ms 72284 KB Output is correct
14 Correct 32 ms 72284 KB Output is correct
15 Correct 32 ms 72284 KB Output is correct
16 Correct 32 ms 72280 KB Output is correct
17 Correct 33 ms 72472 KB Output is correct
18 Correct 33 ms 72108 KB Output is correct
19 Correct 32 ms 72272 KB Output is correct
20 Correct 32 ms 72072 KB Output is correct
21 Correct 34 ms 72296 KB Output is correct
22 Correct 42 ms 72148 KB Output is correct
23 Correct 33 ms 72276 KB Output is correct
24 Correct 33 ms 72284 KB Output is correct
25 Correct 32 ms 72308 KB Output is correct
26 Correct 37 ms 72536 KB Output is correct
27 Correct 33 ms 72300 KB Output is correct
28 Correct 33 ms 72324 KB Output is correct
29 Correct 35 ms 72284 KB Output is correct
30 Correct 33 ms 72284 KB Output is correct
31 Correct 33 ms 72076 KB Output is correct
32 Correct 33 ms 72272 KB Output is correct
33 Correct 32 ms 72284 KB Output is correct
34 Correct 32 ms 72280 KB Output is correct
35 Correct 32 ms 72284 KB Output is correct
36 Correct 33 ms 72184 KB Output is correct
37 Correct 33 ms 72284 KB Output is correct
38 Correct 32 ms 72284 KB Output is correct
39 Correct 32 ms 72276 KB Output is correct
40 Correct 35 ms 72124 KB Output is correct
41 Correct 38 ms 72284 KB Output is correct
42 Correct 32 ms 72276 KB Output is correct
43 Correct 32 ms 72276 KB Output is correct
44 Correct 33 ms 72284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 72016 KB Output is correct
2 Correct 36 ms 72020 KB Output is correct
3 Correct 34 ms 72284 KB Output is correct
4 Correct 32 ms 72284 KB Output is correct
5 Correct 32 ms 72280 KB Output is correct
6 Correct 32 ms 72268 KB Output is correct
7 Correct 40 ms 72272 KB Output is correct
8 Correct 36 ms 72272 KB Output is correct
9 Correct 37 ms 72284 KB Output is correct
10 Correct 34 ms 72280 KB Output is correct
11 Correct 36 ms 72284 KB Output is correct
12 Correct 33 ms 72092 KB Output is correct
13 Correct 32 ms 72284 KB Output is correct
14 Correct 32 ms 72284 KB Output is correct
15 Correct 32 ms 72284 KB Output is correct
16 Correct 32 ms 72280 KB Output is correct
17 Correct 33 ms 72472 KB Output is correct
18 Correct 33 ms 72108 KB Output is correct
19 Correct 32 ms 72272 KB Output is correct
20 Correct 32 ms 72072 KB Output is correct
21 Correct 34 ms 72296 KB Output is correct
22 Correct 42 ms 72148 KB Output is correct
23 Correct 33 ms 72276 KB Output is correct
24 Correct 33 ms 72284 KB Output is correct
25 Correct 32 ms 72308 KB Output is correct
26 Correct 37 ms 72536 KB Output is correct
27 Correct 33 ms 72300 KB Output is correct
28 Correct 33 ms 72324 KB Output is correct
29 Correct 35 ms 72284 KB Output is correct
30 Correct 33 ms 72284 KB Output is correct
31 Correct 33 ms 72076 KB Output is correct
32 Correct 33 ms 72272 KB Output is correct
33 Correct 32 ms 72284 KB Output is correct
34 Correct 32 ms 72280 KB Output is correct
35 Correct 32 ms 72284 KB Output is correct
36 Correct 33 ms 72184 KB Output is correct
37 Correct 33 ms 72284 KB Output is correct
38 Correct 32 ms 72284 KB Output is correct
39 Correct 32 ms 72276 KB Output is correct
40 Correct 35 ms 72124 KB Output is correct
41 Correct 38 ms 72284 KB Output is correct
42 Correct 32 ms 72276 KB Output is correct
43 Correct 32 ms 72276 KB Output is correct
44 Correct 33 ms 72284 KB Output is correct
45 Correct 37 ms 72308 KB Output is correct
46 Correct 36 ms 72284 KB Output is correct
47 Correct 42 ms 72276 KB Output is correct
48 Correct 43 ms 72552 KB Output is correct
49 Correct 34 ms 72528 KB Output is correct
50 Correct 34 ms 72540 KB Output is correct
51 Correct 35 ms 72556 KB Output is correct
52 Correct 35 ms 72524 KB Output is correct
53 Correct 35 ms 72544 KB Output is correct
54 Correct 34 ms 72644 KB Output is correct
55 Correct 34 ms 72428 KB Output is correct
56 Correct 39 ms 72692 KB Output is correct
57 Correct 38 ms 72284 KB Output is correct
58 Correct 46 ms 72280 KB Output is correct
59 Correct 40 ms 72284 KB Output is correct
60 Correct 37 ms 72324 KB Output is correct
61 Correct 34 ms 72528 KB Output is correct
62 Correct 35 ms 72540 KB Output is correct
63 Correct 36 ms 72564 KB Output is correct
64 Correct 35 ms 72284 KB Output is correct
65 Correct 42 ms 72288 KB Output is correct
66 Correct 36 ms 72288 KB Output is correct
67 Correct 37 ms 72280 KB Output is correct
68 Correct 34 ms 72536 KB Output is correct
69 Correct 34 ms 72528 KB Output is correct
70 Correct 36 ms 72624 KB Output is correct
71 Correct 34 ms 72536 KB Output is correct
72 Correct 45 ms 72428 KB Output is correct
73 Correct 37 ms 72424 KB Output is correct
74 Correct 36 ms 72540 KB Output is correct
75 Correct 36 ms 72276 KB Output is correct
76 Correct 37 ms 72472 KB Output is correct
77 Correct 42 ms 72344 KB Output is correct
78 Correct 34 ms 72280 KB Output is correct
79 Correct 34 ms 72284 KB Output is correct
80 Correct 35 ms 72284 KB Output is correct
81 Correct 34 ms 72272 KB Output is correct
82 Correct 35 ms 72272 KB Output is correct
83 Correct 35 ms 72284 KB Output is correct
84 Correct 34 ms 72340 KB Output is correct
85 Correct 38 ms 72292 KB Output is correct
86 Correct 35 ms 72208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 72016 KB Output is correct
2 Correct 36 ms 72020 KB Output is correct
3 Correct 34 ms 72284 KB Output is correct
4 Correct 32 ms 72284 KB Output is correct
5 Correct 32 ms 72280 KB Output is correct
6 Correct 32 ms 72268 KB Output is correct
7 Correct 40 ms 72272 KB Output is correct
8 Correct 36 ms 72272 KB Output is correct
9 Correct 37 ms 72284 KB Output is correct
10 Correct 34 ms 72280 KB Output is correct
11 Correct 36 ms 72284 KB Output is correct
12 Correct 33 ms 72092 KB Output is correct
13 Correct 32 ms 72284 KB Output is correct
14 Correct 32 ms 72284 KB Output is correct
15 Correct 32 ms 72284 KB Output is correct
16 Correct 32 ms 72280 KB Output is correct
17 Correct 33 ms 72472 KB Output is correct
18 Correct 33 ms 72108 KB Output is correct
19 Correct 32 ms 72272 KB Output is correct
20 Correct 32 ms 72072 KB Output is correct
21 Correct 34 ms 72296 KB Output is correct
22 Correct 42 ms 72148 KB Output is correct
23 Correct 33 ms 72276 KB Output is correct
24 Correct 33 ms 72284 KB Output is correct
25 Correct 32 ms 72308 KB Output is correct
26 Correct 37 ms 72536 KB Output is correct
27 Correct 33 ms 72300 KB Output is correct
28 Correct 33 ms 72324 KB Output is correct
29 Correct 35 ms 72284 KB Output is correct
30 Correct 33 ms 72284 KB Output is correct
31 Correct 33 ms 72076 KB Output is correct
32 Correct 33 ms 72272 KB Output is correct
33 Correct 32 ms 72284 KB Output is correct
34 Correct 32 ms 72280 KB Output is correct
35 Correct 32 ms 72284 KB Output is correct
36 Correct 33 ms 72184 KB Output is correct
37 Correct 33 ms 72284 KB Output is correct
38 Correct 32 ms 72284 KB Output is correct
39 Correct 32 ms 72276 KB Output is correct
40 Correct 35 ms 72124 KB Output is correct
41 Correct 38 ms 72284 KB Output is correct
42 Correct 32 ms 72276 KB Output is correct
43 Correct 32 ms 72276 KB Output is correct
44 Correct 33 ms 72284 KB Output is correct
45 Correct 37 ms 72308 KB Output is correct
46 Correct 36 ms 72284 KB Output is correct
47 Correct 42 ms 72276 KB Output is correct
48 Correct 43 ms 72552 KB Output is correct
49 Correct 34 ms 72528 KB Output is correct
50 Correct 34 ms 72540 KB Output is correct
51 Correct 35 ms 72556 KB Output is correct
52 Correct 35 ms 72524 KB Output is correct
53 Correct 35 ms 72544 KB Output is correct
54 Correct 34 ms 72644 KB Output is correct
55 Correct 34 ms 72428 KB Output is correct
56 Correct 39 ms 72692 KB Output is correct
57 Correct 38 ms 72284 KB Output is correct
58 Correct 46 ms 72280 KB Output is correct
59 Correct 40 ms 72284 KB Output is correct
60 Correct 37 ms 72324 KB Output is correct
61 Correct 34 ms 72528 KB Output is correct
62 Correct 35 ms 72540 KB Output is correct
63 Correct 36 ms 72564 KB Output is correct
64 Correct 35 ms 72284 KB Output is correct
65 Correct 42 ms 72288 KB Output is correct
66 Correct 36 ms 72288 KB Output is correct
67 Correct 37 ms 72280 KB Output is correct
68 Correct 34 ms 72536 KB Output is correct
69 Correct 34 ms 72528 KB Output is correct
70 Correct 36 ms 72624 KB Output is correct
71 Correct 34 ms 72536 KB Output is correct
72 Correct 45 ms 72428 KB Output is correct
73 Correct 37 ms 72424 KB Output is correct
74 Correct 36 ms 72540 KB Output is correct
75 Correct 36 ms 72276 KB Output is correct
76 Correct 37 ms 72472 KB Output is correct
77 Correct 42 ms 72344 KB Output is correct
78 Correct 34 ms 72280 KB Output is correct
79 Correct 34 ms 72284 KB Output is correct
80 Correct 35 ms 72284 KB Output is correct
81 Correct 34 ms 72272 KB Output is correct
82 Correct 35 ms 72272 KB Output is correct
83 Correct 35 ms 72284 KB Output is correct
84 Correct 34 ms 72340 KB Output is correct
85 Correct 38 ms 72292 KB Output is correct
86 Correct 35 ms 72208 KB Output is correct
87 Correct 44 ms 72720 KB Output is correct
88 Correct 67 ms 73552 KB Output is correct
89 Correct 189 ms 77016 KB Output is correct
90 Correct 151 ms 76996 KB Output is correct
91 Correct 150 ms 77112 KB Output is correct
92 Correct 87 ms 84560 KB Output is correct
93 Correct 82 ms 84556 KB Output is correct
94 Correct 83 ms 84564 KB Output is correct
95 Correct 117 ms 81068 KB Output is correct
96 Correct 104 ms 81364 KB Output is correct
97 Correct 102 ms 81440 KB Output is correct
98 Correct 104 ms 81452 KB Output is correct
99 Correct 99 ms 80396 KB Output is correct
100 Correct 175 ms 77064 KB Output is correct
101 Correct 237 ms 77408 KB Output is correct
102 Correct 234 ms 77772 KB Output is correct
103 Correct 239 ms 77396 KB Output is correct
104 Correct 94 ms 80208 KB Output is correct
105 Correct 94 ms 80112 KB Output is correct
106 Correct 102 ms 80212 KB Output is correct
107 Correct 156 ms 76128 KB Output is correct
108 Correct 147 ms 76368 KB Output is correct
109 Correct 155 ms 76624 KB Output is correct
110 Correct 83 ms 84168 KB Output is correct
111 Correct 94 ms 81024 KB Output is correct
112 Correct 94 ms 80944 KB Output is correct
113 Correct 96 ms 79440 KB Output is correct
114 Correct 170 ms 77140 KB Output is correct
115 Correct 227 ms 76880 KB Output is correct
116 Correct 106 ms 79620 KB Output is correct
117 Correct 94 ms 78336 KB Output is correct
118 Correct 109 ms 77652 KB Output is correct
119 Correct 94 ms 77488 KB Output is correct
120 Correct 99 ms 78084 KB Output is correct
121 Correct 92 ms 77476 KB Output is correct
122 Correct 92 ms 77132 KB Output is correct
123 Correct 99 ms 78220 KB Output is correct
124 Correct 96 ms 77784 KB Output is correct
125 Correct 99 ms 77520 KB Output is correct
126 Correct 100 ms 77872 KB Output is correct
127 Correct 95 ms 77560 KB Output is correct
128 Correct 99 ms 77080 KB Output is correct