Submission #659741

# Submission time Handle Problem Language Result Execution time Memory
659741 2022-11-19T08:16:18 Z MohammadAghil Construction of Highway (JOI18_construction) C++17
100 / 100
363 ms 30160 KB
      #include <bits/stdc++.h>
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
 using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
     #define per(i,r,l) for(int i = (r); i >= (l); i--)
       #define rep(i,l,r) for(int i = (l); i < (r); i++)
          #define all(x) begin(x), end(x)
             #define sz(x) (int)(x).size()
                 #define pb push_back
                     #define ss second
                          #define ff first
                                  void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
     cin.tie(0) -> sync_with_stdio(0);
     // #ifndef ONLINE_JUDGE
     //      #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
     //      freopen("inp.txt", "r", stdin);
     //      freopen("out.txt", "w", stdout);
     // #else
     //      #define er(args ...) 0
     // #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 1e5 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll,md);return k*k%md*(b&1ll?a:1)%md;}

int n, c[maxn], h[maxn], par[maxn], cnt[maxn], srt[maxn], top[maxn];
vector<int> adj[maxn];
priority_queue<pp, vector<pp>, greater<pp>> pq[maxn];

void dfs(int r, int p){
     cnt[r] = 1;
     par[r] = p;
     for(int c: adj[r]) if(c - p){
          h[c] = h[r] + 1;
          dfs(c, r);
          cnt[r] += cnt[c];
     }
}

void dfsh(int r, int p, int tp){
     top[r] = tp;
     // er(r, tp, srt[r]);
     pp bst = {-1, -1};
     pq[tp].push({srt[r], c[r]});
     for(int c: adj[r]) if(c - p) bst = max(bst, {cnt[c], c});
     if(bst.ss + 1){
          srt[bst.ss] = srt[r] + 1;
          dfsh(bst.ss, r, tp);
          for(int c: adj[r]) if(c - p && c - bst.ss) dfsh(c, r, c);
     }
}

ll fen[maxn];
vector<pp> his;
void upd(int i, int k, bool f = true){
     if(f) his.pb({i, k});
     for(; i < maxn; i += i&-i) fen[i] += k;
}
ll get(int i){
     ll res = 0;
     for(; i; i -= i&-i) res += fen[i];
     return res;
}
void clear(){
     for(auto[i, k]: his) upd(i, -k, false);
     his.clear();
} 

ll query(int u, int nw){
     ll res = 0;
     while(u + 1){
          vector<pp> tmp;
          int lst = -1;
          while(true){
               auto[tp, vl] = pq[top[u]].top();
               if(tp > srt[u]){
                    tmp.pb({srt[u] - lst, vl});
                    break;
               }else{
                    pq[top[u]].pop();
                    tmp.pb({tp - lst, vl});
                    lst = tp;
               }
          }
          pq[top[u]].push({srt[u], nw});
          reverse(all(tmp));
          for(auto c: tmp) res += 1ll*get(c.ss-1)*c.ff, upd(c.ss, c.ff);
          u = par[top[u]];
     }
     clear();
     return res;
}
 
int main(){ IOS();
     cin >> n;
     map<int, int> mp; int id = 1;
     rep(i,0,n) cin >> c[i], mp[c[i]] = 0;
     for(auto&c: mp) c.ss = id++;
     rep(i,0,n) c[i] = mp[c[i]];
     vector<pp> edge;
     rep(i,1,n){
          int u, v; cin >> u >> v; u--, v--;
          adj[u].pb(v), adj[v].pb(u);
          edge.pb({u, v});
     }    
     dfs(0, -1), dfsh(0, -1, 0);
     for(auto[u, v]: edge){
          if(h[u] > h[v]) swap(u, v);
          cout << query(u, c[v]) << '\n';          
     }
     return 0-0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5820 KB Output is correct
4 Correct 4 ms 5844 KB Output is correct
5 Correct 4 ms 5844 KB Output is correct
6 Correct 4 ms 5844 KB Output is correct
7 Correct 4 ms 5964 KB Output is correct
8 Correct 5 ms 6048 KB Output is correct
9 Correct 4 ms 5952 KB Output is correct
10 Correct 3 ms 5972 KB Output is correct
11 Correct 4 ms 5960 KB Output is correct
12 Correct 4 ms 5956 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 4 ms 5972 KB Output is correct
15 Correct 4 ms 5844 KB Output is correct
16 Correct 4 ms 5952 KB Output is correct
17 Correct 4 ms 5964 KB Output is correct
18 Correct 5 ms 5952 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5960 KB Output is correct
22 Correct 3 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 4 ms 5840 KB Output is correct
25 Correct 4 ms 5844 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 4 ms 5852 KB Output is correct
29 Correct 5 ms 5844 KB Output is correct
30 Correct 4 ms 5948 KB Output is correct
31 Correct 5 ms 5828 KB Output is correct
32 Correct 4 ms 5844 KB Output is correct
33 Correct 4 ms 5952 KB Output is correct
34 Correct 4 ms 5844 KB Output is correct
35 Correct 4 ms 5844 KB Output is correct
36 Correct 3 ms 5844 KB Output is correct
37 Correct 4 ms 5828 KB Output is correct
38 Correct 3 ms 5828 KB Output is correct
39 Correct 4 ms 5844 KB Output is correct
40 Correct 4 ms 5844 KB Output is correct
41 Correct 3 ms 5844 KB Output is correct
42 Correct 4 ms 5824 KB Output is correct
43 Correct 4 ms 5844 KB Output is correct
44 Correct 4 ms 5844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5820 KB Output is correct
4 Correct 4 ms 5844 KB Output is correct
5 Correct 4 ms 5844 KB Output is correct
6 Correct 4 ms 5844 KB Output is correct
7 Correct 4 ms 5964 KB Output is correct
8 Correct 5 ms 6048 KB Output is correct
9 Correct 4 ms 5952 KB Output is correct
10 Correct 3 ms 5972 KB Output is correct
11 Correct 4 ms 5960 KB Output is correct
12 Correct 4 ms 5956 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 4 ms 5972 KB Output is correct
15 Correct 4 ms 5844 KB Output is correct
16 Correct 4 ms 5952 KB Output is correct
17 Correct 4 ms 5964 KB Output is correct
18 Correct 5 ms 5952 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5960 KB Output is correct
22 Correct 3 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 4 ms 5840 KB Output is correct
25 Correct 4 ms 5844 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 4 ms 5852 KB Output is correct
29 Correct 5 ms 5844 KB Output is correct
30 Correct 4 ms 5948 KB Output is correct
31 Correct 5 ms 5828 KB Output is correct
32 Correct 4 ms 5844 KB Output is correct
33 Correct 4 ms 5952 KB Output is correct
34 Correct 4 ms 5844 KB Output is correct
35 Correct 4 ms 5844 KB Output is correct
36 Correct 3 ms 5844 KB Output is correct
37 Correct 4 ms 5828 KB Output is correct
38 Correct 3 ms 5828 KB Output is correct
39 Correct 4 ms 5844 KB Output is correct
40 Correct 4 ms 5844 KB Output is correct
41 Correct 3 ms 5844 KB Output is correct
42 Correct 4 ms 5824 KB Output is correct
43 Correct 4 ms 5844 KB Output is correct
44 Correct 4 ms 5844 KB Output is correct
45 Correct 5 ms 5972 KB Output is correct
46 Correct 10 ms 6472 KB Output is correct
47 Correct 9 ms 6484 KB Output is correct
48 Correct 9 ms 6484 KB Output is correct
49 Correct 9 ms 6732 KB Output is correct
50 Correct 8 ms 6856 KB Output is correct
51 Correct 8 ms 6844 KB Output is correct
52 Correct 7 ms 6644 KB Output is correct
53 Correct 7 ms 6612 KB Output is correct
54 Correct 8 ms 6720 KB Output is correct
55 Correct 10 ms 6612 KB Output is correct
56 Correct 9 ms 6612 KB Output is correct
57 Correct 10 ms 6356 KB Output is correct
58 Correct 11 ms 6484 KB Output is correct
59 Correct 11 ms 6468 KB Output is correct
60 Correct 11 ms 6484 KB Output is correct
61 Correct 7 ms 6612 KB Output is correct
62 Correct 8 ms 6592 KB Output is correct
63 Correct 7 ms 6612 KB Output is correct
64 Correct 8 ms 6228 KB Output is correct
65 Correct 8 ms 6220 KB Output is correct
66 Correct 9 ms 6228 KB Output is correct
67 Correct 9 ms 6368 KB Output is correct
68 Correct 7 ms 6528 KB Output is correct
69 Correct 8 ms 6604 KB Output is correct
70 Correct 7 ms 6484 KB Output is correct
71 Correct 6 ms 6356 KB Output is correct
72 Correct 10 ms 6356 KB Output is correct
73 Correct 10 ms 6272 KB Output is correct
74 Correct 7 ms 6344 KB Output is correct
75 Correct 10 ms 6484 KB Output is correct
76 Correct 8 ms 6484 KB Output is correct
77 Correct 9 ms 6484 KB Output is correct
78 Correct 10 ms 6336 KB Output is correct
79 Correct 7 ms 6272 KB Output is correct
80 Correct 8 ms 6272 KB Output is correct
81 Correct 8 ms 6552 KB Output is correct
82 Correct 8 ms 6484 KB Output is correct
83 Correct 8 ms 6468 KB Output is correct
84 Correct 7 ms 6344 KB Output is correct
85 Correct 7 ms 6228 KB Output is correct
86 Correct 7 ms 6212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5820 KB Output is correct
4 Correct 4 ms 5844 KB Output is correct
5 Correct 4 ms 5844 KB Output is correct
6 Correct 4 ms 5844 KB Output is correct
7 Correct 4 ms 5964 KB Output is correct
8 Correct 5 ms 6048 KB Output is correct
9 Correct 4 ms 5952 KB Output is correct
10 Correct 3 ms 5972 KB Output is correct
11 Correct 4 ms 5960 KB Output is correct
12 Correct 4 ms 5956 KB Output is correct
13 Correct 4 ms 5972 KB Output is correct
14 Correct 4 ms 5972 KB Output is correct
15 Correct 4 ms 5844 KB Output is correct
16 Correct 4 ms 5952 KB Output is correct
17 Correct 4 ms 5964 KB Output is correct
18 Correct 5 ms 5952 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 5 ms 5972 KB Output is correct
21 Correct 4 ms 5960 KB Output is correct
22 Correct 3 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 4 ms 5840 KB Output is correct
25 Correct 4 ms 5844 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 4 ms 5972 KB Output is correct
28 Correct 4 ms 5852 KB Output is correct
29 Correct 5 ms 5844 KB Output is correct
30 Correct 4 ms 5948 KB Output is correct
31 Correct 5 ms 5828 KB Output is correct
32 Correct 4 ms 5844 KB Output is correct
33 Correct 4 ms 5952 KB Output is correct
34 Correct 4 ms 5844 KB Output is correct
35 Correct 4 ms 5844 KB Output is correct
36 Correct 3 ms 5844 KB Output is correct
37 Correct 4 ms 5828 KB Output is correct
38 Correct 3 ms 5828 KB Output is correct
39 Correct 4 ms 5844 KB Output is correct
40 Correct 4 ms 5844 KB Output is correct
41 Correct 3 ms 5844 KB Output is correct
42 Correct 4 ms 5824 KB Output is correct
43 Correct 4 ms 5844 KB Output is correct
44 Correct 4 ms 5844 KB Output is correct
45 Correct 5 ms 5972 KB Output is correct
46 Correct 10 ms 6472 KB Output is correct
47 Correct 9 ms 6484 KB Output is correct
48 Correct 9 ms 6484 KB Output is correct
49 Correct 9 ms 6732 KB Output is correct
50 Correct 8 ms 6856 KB Output is correct
51 Correct 8 ms 6844 KB Output is correct
52 Correct 7 ms 6644 KB Output is correct
53 Correct 7 ms 6612 KB Output is correct
54 Correct 8 ms 6720 KB Output is correct
55 Correct 10 ms 6612 KB Output is correct
56 Correct 9 ms 6612 KB Output is correct
57 Correct 10 ms 6356 KB Output is correct
58 Correct 11 ms 6484 KB Output is correct
59 Correct 11 ms 6468 KB Output is correct
60 Correct 11 ms 6484 KB Output is correct
61 Correct 7 ms 6612 KB Output is correct
62 Correct 8 ms 6592 KB Output is correct
63 Correct 7 ms 6612 KB Output is correct
64 Correct 8 ms 6228 KB Output is correct
65 Correct 8 ms 6220 KB Output is correct
66 Correct 9 ms 6228 KB Output is correct
67 Correct 9 ms 6368 KB Output is correct
68 Correct 7 ms 6528 KB Output is correct
69 Correct 8 ms 6604 KB Output is correct
70 Correct 7 ms 6484 KB Output is correct
71 Correct 6 ms 6356 KB Output is correct
72 Correct 10 ms 6356 KB Output is correct
73 Correct 10 ms 6272 KB Output is correct
74 Correct 7 ms 6344 KB Output is correct
75 Correct 10 ms 6484 KB Output is correct
76 Correct 8 ms 6484 KB Output is correct
77 Correct 9 ms 6484 KB Output is correct
78 Correct 10 ms 6336 KB Output is correct
79 Correct 7 ms 6272 KB Output is correct
80 Correct 8 ms 6272 KB Output is correct
81 Correct 8 ms 6552 KB Output is correct
82 Correct 8 ms 6484 KB Output is correct
83 Correct 8 ms 6468 KB Output is correct
84 Correct 7 ms 6344 KB Output is correct
85 Correct 7 ms 6228 KB Output is correct
86 Correct 7 ms 6212 KB Output is correct
87 Correct 20 ms 7404 KB Output is correct
88 Correct 61 ms 10608 KB Output is correct
89 Correct 254 ms 21880 KB Output is correct
90 Correct 241 ms 21900 KB Output is correct
91 Correct 253 ms 21984 KB Output is correct
92 Correct 159 ms 30056 KB Output is correct
93 Correct 155 ms 30160 KB Output is correct
94 Correct 178 ms 30068 KB Output is correct
95 Correct 157 ms 27380 KB Output is correct
96 Correct 157 ms 27772 KB Output is correct
97 Correct 170 ms 27696 KB Output is correct
98 Correct 157 ms 27812 KB Output is correct
99 Correct 151 ms 26660 KB Output is correct
100 Correct 310 ms 21104 KB Output is correct
101 Correct 323 ms 21428 KB Output is correct
102 Correct 324 ms 21540 KB Output is correct
103 Correct 363 ms 21364 KB Output is correct
104 Correct 163 ms 26540 KB Output is correct
105 Correct 171 ms 26624 KB Output is correct
106 Correct 176 ms 26608 KB Output is correct
107 Correct 189 ms 15704 KB Output is correct
108 Correct 221 ms 15784 KB Output is correct
109 Correct 217 ms 17600 KB Output is correct
110 Correct 111 ms 24104 KB Output is correct
111 Correct 148 ms 27376 KB Output is correct
112 Correct 117 ms 21672 KB Output is correct
113 Correct 103 ms 20564 KB Output is correct
114 Correct 289 ms 21108 KB Output is correct
115 Correct 275 ms 15404 KB Output is correct
116 Correct 115 ms 20672 KB Output is correct
117 Correct 158 ms 23676 KB Output is correct
118 Correct 161 ms 22940 KB Output is correct
119 Correct 152 ms 22464 KB Output is correct
120 Correct 121 ms 18104 KB Output is correct
121 Correct 114 ms 17444 KB Output is correct
122 Correct 115 ms 17080 KB Output is correct
123 Correct 179 ms 23804 KB Output is correct
124 Correct 172 ms 22980 KB Output is correct
125 Correct 172 ms 22596 KB Output is correct
126 Correct 120 ms 18288 KB Output is correct
127 Correct 132 ms 17452 KB Output is correct
128 Correct 128 ms 17084 KB Output is correct