Submission #515997

# Submission time Handle Problem Language Result Execution time Memory
515997 2022-01-20T09:23:17 Z NaimSS Construction of Highway (JOI18_construction) C++14
100 / 100
372 ms 85368 KB
#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
  b%=m;
  ll ans = 1;
  for (; e; b = b * b % m, e /= 2)
      if (e & 1) ans = ans * b % m;
  return ans;
}

// debug:
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ 
  cerr << ' ' << H; 
  dbg_out(T...); 
}
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...) 
#endif
//

const int N = 100100;
int A[N],B[N],C[N];
int pai[N];
int H[N];
ll bit[N];
stack<pii> st[N];



class HLD{
public:
	HLD(int n): Sz(n+1) , Hd(n+1) , p(n+1, -1) , g(n+1) , out(n+1) , in(n+1) , n(n){}
	void add_edge(int x , int y){
		g[x].push_back(y);
		g[y].push_back(x);
		edges ++ ;
		assert(edges < n);
	}
	void dfs_sz(int x , int par = -1){
		Sz[x] = 1;
		trav(w, g[x]){
			if(w == par) continue;
			H[w] = H[x] + 1;
      dfs_sz(w,x);

			Sz[x] += Sz[w];
			if(Sz[w] > Sz[g[x][0]] || g[x][0] == par) swap(w, g[x][0]);
		}
	}
	void dfs_dec(int x , int par = -1){ 
		in[x] = tin++;
    p[x] = par;
		allin(w, g[x]){
			if(w == par) continue;
			Hd[w] = ((w == g[x][0]) ? Hd[x] : w);
			dfs_dec(w,x); 
		}
		out[x] = tin;
	}
	void decompose(int root){
    H[root] = 1;
		Hd[root] = root;
		dfs_sz(root);
		dfs_dec(root);
	}


  ll sum(ll x){
    ll res=0;
    while(x>0){
      res+=bit[x];
      x-=(x&-x);
    }
    return res;
  }
void upd(int x,int v){
  while(x<N){
    bit[x]+=v;
    x+=(x&-x);
  }
}

ll cnt(vector<pii> v){
  ll res=0;
  for(auto it : v){
    res+=it.ss * (sum(N-1) - sum(it.ff));
    upd(it.ff,+it.ss);
  }
  for(auto it : v){
    upd(it.ff,-it.ss);
  }
  return res;
}

  ll query(int v,int c){
    ll res=0;
    vector<pii> vec;

    while(v!=-1){
      int d = H[v];
      v = Hd[v];
      int t = H[v]-1;

      stack<pii>&s = st[v];
      vector<pii> cur;
      while(!s.empty() && s.top().ss <= d){
        auto tp = s.top();s.pop();
        cur.pb(pii(tp.ff,tp.ss-t));
        t=tp.ss;
      }
      if(!s.empty() && s.top().ss > d){
        cur.pb(pii(s.top().ff,d-t));
      }
      s.push(pii(c,d));
      v = p[v];
      reverse(all(cur));
      for(auto it : cur)vec.pb(it);
    }
    reverse(all(vec));
    res += cnt(vec);
    return res;
  }

private:
	vector < vi > g; 
	vi Sz , Hd , in , out ,p ; 
	int n , edges =0  , tin = 0;
};




int32_t main(){
  fastio;
  int n;
  cin >> n;
  vi vec;
  rep(i,1,n+1){

    cin >> C[i];
    vec.pb(C[i]);
    pai[i] = i;
  }
  Unique(vec);
  rep(i,1,n+1){
    C[i] = (lower_bound(all(vec),C[i]) - vec.begin()) + 1;
  }
  HLD H(n);
  rep(j,1,n){
    cin >> A[j] >> B[j];
    H.add_edge(A[j],B[j]);
  }
  H.decompose(1);
  rep(j,1,n){
    cout << H.query(B[j],C[B[j]]) << endl;
  }

  // math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}

Compilation message

construction.cpp: In constructor 'HLD::HLD(int)':
construction.cpp:159:25: warning: 'HLD::p' will be initialized after [-Wreorder]
  159 |  vi Sz , Hd , in , out ,p ;
      |                         ^
construction.cpp:158:16: warning:   'std::vector<std::vector<int> > HLD::g' [-Wreorder]
  158 |  vector < vi > g;
      |                ^
construction.cpp:65:2: warning:   when initialized here [-Wreorder]
   65 |  HLD(int n): Sz(n+1) , Hd(n+1) , p(n+1, -1) , g(n+1) , out(n+1) , in(n+1) , n(n){}
      |  ^~~
construction.cpp:159:20: warning: 'HLD::out' will be initialized after [-Wreorder]
  159 |  vi Sz , Hd , in , out ,p ;
      |                    ^~~
construction.cpp:159:15: warning:   'vi HLD::in' [-Wreorder]
  159 |  vi Sz , Hd , in , out ,p ;
      |               ^~
construction.cpp:65:2: warning:   when initialized here [-Wreorder]
   65 |  HLD(int n): Sz(n+1) , Hd(n+1) , p(n+1, -1) , g(n+1) , out(n+1) , in(n+1) , n(n){}
      |  ^~~
# Verdict Execution time Memory Grader output
1 Correct 41 ms 67664 KB Output is correct
2 Correct 38 ms 67668 KB Output is correct
3 Correct 39 ms 67824 KB Output is correct
4 Correct 40 ms 67716 KB Output is correct
5 Correct 39 ms 67780 KB Output is correct
6 Correct 41 ms 67736 KB Output is correct
7 Correct 38 ms 67712 KB Output is correct
8 Correct 40 ms 67804 KB Output is correct
9 Correct 39 ms 67760 KB Output is correct
10 Correct 39 ms 67756 KB Output is correct
11 Correct 37 ms 67788 KB Output is correct
12 Correct 38 ms 67780 KB Output is correct
13 Correct 43 ms 67732 KB Output is correct
14 Correct 41 ms 67820 KB Output is correct
15 Correct 40 ms 67720 KB Output is correct
16 Correct 39 ms 67748 KB Output is correct
17 Correct 37 ms 67788 KB Output is correct
18 Correct 39 ms 67828 KB Output is correct
19 Correct 39 ms 67720 KB Output is correct
20 Correct 38 ms 67812 KB Output is correct
21 Correct 40 ms 67828 KB Output is correct
22 Correct 39 ms 67720 KB Output is correct
23 Correct 42 ms 67768 KB Output is correct
24 Correct 38 ms 67712 KB Output is correct
25 Correct 39 ms 67780 KB Output is correct
26 Correct 39 ms 67732 KB Output is correct
27 Correct 38 ms 67700 KB Output is correct
28 Correct 38 ms 67836 KB Output is correct
29 Correct 38 ms 67840 KB Output is correct
30 Correct 38 ms 67704 KB Output is correct
31 Correct 40 ms 67700 KB Output is correct
32 Correct 38 ms 67788 KB Output is correct
33 Correct 38 ms 67812 KB Output is correct
34 Correct 38 ms 67812 KB Output is correct
35 Correct 39 ms 67808 KB Output is correct
36 Correct 39 ms 67828 KB Output is correct
37 Correct 38 ms 67788 KB Output is correct
38 Correct 38 ms 67700 KB Output is correct
39 Correct 39 ms 67712 KB Output is correct
40 Correct 39 ms 67808 KB Output is correct
41 Correct 40 ms 67720 KB Output is correct
42 Correct 39 ms 67768 KB Output is correct
43 Correct 49 ms 67780 KB Output is correct
44 Correct 39 ms 67764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 67664 KB Output is correct
2 Correct 38 ms 67668 KB Output is correct
3 Correct 39 ms 67824 KB Output is correct
4 Correct 40 ms 67716 KB Output is correct
5 Correct 39 ms 67780 KB Output is correct
6 Correct 41 ms 67736 KB Output is correct
7 Correct 38 ms 67712 KB Output is correct
8 Correct 40 ms 67804 KB Output is correct
9 Correct 39 ms 67760 KB Output is correct
10 Correct 39 ms 67756 KB Output is correct
11 Correct 37 ms 67788 KB Output is correct
12 Correct 38 ms 67780 KB Output is correct
13 Correct 43 ms 67732 KB Output is correct
14 Correct 41 ms 67820 KB Output is correct
15 Correct 40 ms 67720 KB Output is correct
16 Correct 39 ms 67748 KB Output is correct
17 Correct 37 ms 67788 KB Output is correct
18 Correct 39 ms 67828 KB Output is correct
19 Correct 39 ms 67720 KB Output is correct
20 Correct 38 ms 67812 KB Output is correct
21 Correct 40 ms 67828 KB Output is correct
22 Correct 39 ms 67720 KB Output is correct
23 Correct 42 ms 67768 KB Output is correct
24 Correct 38 ms 67712 KB Output is correct
25 Correct 39 ms 67780 KB Output is correct
26 Correct 39 ms 67732 KB Output is correct
27 Correct 38 ms 67700 KB Output is correct
28 Correct 38 ms 67836 KB Output is correct
29 Correct 38 ms 67840 KB Output is correct
30 Correct 38 ms 67704 KB Output is correct
31 Correct 40 ms 67700 KB Output is correct
32 Correct 38 ms 67788 KB Output is correct
33 Correct 38 ms 67812 KB Output is correct
34 Correct 38 ms 67812 KB Output is correct
35 Correct 39 ms 67808 KB Output is correct
36 Correct 39 ms 67828 KB Output is correct
37 Correct 38 ms 67788 KB Output is correct
38 Correct 38 ms 67700 KB Output is correct
39 Correct 39 ms 67712 KB Output is correct
40 Correct 39 ms 67808 KB Output is correct
41 Correct 40 ms 67720 KB Output is correct
42 Correct 39 ms 67768 KB Output is correct
43 Correct 49 ms 67780 KB Output is correct
44 Correct 39 ms 67764 KB Output is correct
45 Correct 40 ms 67796 KB Output is correct
46 Correct 44 ms 68180 KB Output is correct
47 Correct 44 ms 68348 KB Output is correct
48 Correct 43 ms 68196 KB Output is correct
49 Correct 42 ms 68316 KB Output is correct
50 Correct 43 ms 68344 KB Output is correct
51 Correct 45 ms 68436 KB Output is correct
52 Correct 41 ms 68296 KB Output is correct
53 Correct 42 ms 68292 KB Output is correct
54 Correct 42 ms 68348 KB Output is correct
55 Correct 46 ms 68292 KB Output is correct
56 Correct 41 ms 68292 KB Output is correct
57 Correct 45 ms 68124 KB Output is correct
58 Correct 47 ms 68332 KB Output is correct
59 Correct 47 ms 68292 KB Output is correct
60 Correct 47 ms 68172 KB Output is correct
61 Correct 42 ms 68296 KB Output is correct
62 Correct 47 ms 68288 KB Output is correct
63 Correct 45 ms 68296 KB Output is correct
64 Correct 45 ms 68232 KB Output is correct
65 Correct 43 ms 68212 KB Output is correct
66 Correct 43 ms 68172 KB Output is correct
67 Correct 44 ms 68228 KB Output is correct
68 Correct 40 ms 68316 KB Output is correct
69 Correct 41 ms 68376 KB Output is correct
70 Correct 47 ms 68344 KB Output is correct
71 Correct 41 ms 68308 KB Output is correct
72 Correct 43 ms 68232 KB Output is correct
73 Correct 50 ms 68204 KB Output is correct
74 Correct 41 ms 68216 KB Output is correct
75 Correct 46 ms 68236 KB Output is correct
76 Correct 41 ms 68216 KB Output is correct
77 Correct 42 ms 68164 KB Output is correct
78 Correct 42 ms 68256 KB Output is correct
79 Correct 41 ms 68240 KB Output is correct
80 Correct 44 ms 68224 KB Output is correct
81 Correct 42 ms 68232 KB Output is correct
82 Correct 46 ms 68240 KB Output is correct
83 Correct 45 ms 68188 KB Output is correct
84 Correct 42 ms 68164 KB Output is correct
85 Correct 43 ms 68236 KB Output is correct
86 Correct 43 ms 68232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 67664 KB Output is correct
2 Correct 38 ms 67668 KB Output is correct
3 Correct 39 ms 67824 KB Output is correct
4 Correct 40 ms 67716 KB Output is correct
5 Correct 39 ms 67780 KB Output is correct
6 Correct 41 ms 67736 KB Output is correct
7 Correct 38 ms 67712 KB Output is correct
8 Correct 40 ms 67804 KB Output is correct
9 Correct 39 ms 67760 KB Output is correct
10 Correct 39 ms 67756 KB Output is correct
11 Correct 37 ms 67788 KB Output is correct
12 Correct 38 ms 67780 KB Output is correct
13 Correct 43 ms 67732 KB Output is correct
14 Correct 41 ms 67820 KB Output is correct
15 Correct 40 ms 67720 KB Output is correct
16 Correct 39 ms 67748 KB Output is correct
17 Correct 37 ms 67788 KB Output is correct
18 Correct 39 ms 67828 KB Output is correct
19 Correct 39 ms 67720 KB Output is correct
20 Correct 38 ms 67812 KB Output is correct
21 Correct 40 ms 67828 KB Output is correct
22 Correct 39 ms 67720 KB Output is correct
23 Correct 42 ms 67768 KB Output is correct
24 Correct 38 ms 67712 KB Output is correct
25 Correct 39 ms 67780 KB Output is correct
26 Correct 39 ms 67732 KB Output is correct
27 Correct 38 ms 67700 KB Output is correct
28 Correct 38 ms 67836 KB Output is correct
29 Correct 38 ms 67840 KB Output is correct
30 Correct 38 ms 67704 KB Output is correct
31 Correct 40 ms 67700 KB Output is correct
32 Correct 38 ms 67788 KB Output is correct
33 Correct 38 ms 67812 KB Output is correct
34 Correct 38 ms 67812 KB Output is correct
35 Correct 39 ms 67808 KB Output is correct
36 Correct 39 ms 67828 KB Output is correct
37 Correct 38 ms 67788 KB Output is correct
38 Correct 38 ms 67700 KB Output is correct
39 Correct 39 ms 67712 KB Output is correct
40 Correct 39 ms 67808 KB Output is correct
41 Correct 40 ms 67720 KB Output is correct
42 Correct 39 ms 67768 KB Output is correct
43 Correct 49 ms 67780 KB Output is correct
44 Correct 39 ms 67764 KB Output is correct
45 Correct 40 ms 67796 KB Output is correct
46 Correct 44 ms 68180 KB Output is correct
47 Correct 44 ms 68348 KB Output is correct
48 Correct 43 ms 68196 KB Output is correct
49 Correct 42 ms 68316 KB Output is correct
50 Correct 43 ms 68344 KB Output is correct
51 Correct 45 ms 68436 KB Output is correct
52 Correct 41 ms 68296 KB Output is correct
53 Correct 42 ms 68292 KB Output is correct
54 Correct 42 ms 68348 KB Output is correct
55 Correct 46 ms 68292 KB Output is correct
56 Correct 41 ms 68292 KB Output is correct
57 Correct 45 ms 68124 KB Output is correct
58 Correct 47 ms 68332 KB Output is correct
59 Correct 47 ms 68292 KB Output is correct
60 Correct 47 ms 68172 KB Output is correct
61 Correct 42 ms 68296 KB Output is correct
62 Correct 47 ms 68288 KB Output is correct
63 Correct 45 ms 68296 KB Output is correct
64 Correct 45 ms 68232 KB Output is correct
65 Correct 43 ms 68212 KB Output is correct
66 Correct 43 ms 68172 KB Output is correct
67 Correct 44 ms 68228 KB Output is correct
68 Correct 40 ms 68316 KB Output is correct
69 Correct 41 ms 68376 KB Output is correct
70 Correct 47 ms 68344 KB Output is correct
71 Correct 41 ms 68308 KB Output is correct
72 Correct 43 ms 68232 KB Output is correct
73 Correct 50 ms 68204 KB Output is correct
74 Correct 41 ms 68216 KB Output is correct
75 Correct 46 ms 68236 KB Output is correct
76 Correct 41 ms 68216 KB Output is correct
77 Correct 42 ms 68164 KB Output is correct
78 Correct 42 ms 68256 KB Output is correct
79 Correct 41 ms 68240 KB Output is correct
80 Correct 44 ms 68224 KB Output is correct
81 Correct 42 ms 68232 KB Output is correct
82 Correct 46 ms 68240 KB Output is correct
83 Correct 45 ms 68188 KB Output is correct
84 Correct 42 ms 68164 KB Output is correct
85 Correct 43 ms 68236 KB Output is correct
86 Correct 43 ms 68232 KB Output is correct
87 Correct 56 ms 69028 KB Output is correct
88 Correct 98 ms 71732 KB Output is correct
89 Correct 270 ms 80760 KB Output is correct
90 Correct 263 ms 80832 KB Output is correct
91 Correct 277 ms 80664 KB Output is correct
92 Correct 141 ms 85352 KB Output is correct
93 Correct 126 ms 85348 KB Output is correct
94 Correct 133 ms 85368 KB Output is correct
95 Correct 158 ms 84112 KB Output is correct
96 Correct 142 ms 84444 KB Output is correct
97 Correct 156 ms 84488 KB Output is correct
98 Correct 147 ms 84472 KB Output is correct
99 Correct 140 ms 83136 KB Output is correct
100 Correct 311 ms 80348 KB Output is correct
101 Correct 324 ms 80580 KB Output is correct
102 Correct 372 ms 80656 KB Output is correct
103 Correct 332 ms 80772 KB Output is correct
104 Correct 142 ms 83132 KB Output is correct
105 Correct 146 ms 83124 KB Output is correct
106 Correct 159 ms 83160 KB Output is correct
107 Correct 246 ms 79184 KB Output is correct
108 Correct 239 ms 79348 KB Output is correct
109 Correct 264 ms 79924 KB Output is correct
110 Correct 115 ms 84092 KB Output is correct
111 Correct 144 ms 84144 KB Output is correct
112 Correct 145 ms 83268 KB Output is correct
113 Correct 138 ms 81732 KB Output is correct
114 Correct 332 ms 80252 KB Output is correct
115 Correct 333 ms 79352 KB Output is correct
116 Correct 134 ms 81780 KB Output is correct
117 Correct 169 ms 81660 KB Output is correct
118 Correct 148 ms 81152 KB Output is correct
119 Correct 157 ms 80912 KB Output is correct
120 Correct 137 ms 80404 KB Output is correct
121 Correct 141 ms 80244 KB Output is correct
122 Correct 143 ms 79920 KB Output is correct
123 Correct 155 ms 81604 KB Output is correct
124 Correct 160 ms 81248 KB Output is correct
125 Correct 174 ms 80928 KB Output is correct
126 Correct 145 ms 80576 KB Output is correct
127 Correct 171 ms 80140 KB Output is correct
128 Correct 150 ms 79972 KB Output is correct