Submission #1109768

#TimeUsernameProblemLanguageResultExecution timeMemory
1109768ljtunasConstruction of Highway (JOI18_construction)C++17
100 / 100
330 ms22664 KiB
// author : anhtun_hi , nqg #include <bits/stdc++.h> #define ll long long #define ii pair<ll, ll> #define fi first #define se second #define all(x) x.begin(), x.end() #define reset(h, v) memset(h, v, sizeof h) #define Forv(i, a) for(auto& i : a) #define For(i, a, b) for(int i = a; i <= b; ++i) #define Ford(i, a, b) for(int i = a; i >= b; --i) #define c_bit(i) __builtin_popcountll(i) #define Bit(mask, i) ((mask >> i) & 1LL) #define onbit(mask, i) ((mask) bitor (1LL << i)) #define offbit(mask, i) ((mask) &~ (1LL << i)) using namespace std; namespace std { template <typename T, int D> struct _vector : public vector <_vector <T, D - 1>> { static_assert(D >= 1, "Dimension must be positive!"); template <typename... Args> _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {} }; template <typename T> struct _vector <T, 1> : public vector <T> { _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {} }; template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;} template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;} } const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5; const int MAXN = 1e5 + 5; const ll mod = 1e9 + 7; const ll oo = 1e18; //#define int long long int n, sz[MAXN], in[MAXN], en[MAXN], p[MAXN], head[MAXN]; ll c[MAXN]; ii E[MAXN]; vector<int> g[MAXN]; void dfsInit(int u, int par){ sz[u] = 1; p[u] = par; Forv(v, g[u]) if(v ^ par){ dfsInit(v, u), sz[u] += sz[v]; } } vector<int> ListHLD; void hld (int u, int par){ static int timedfs = 0; in[u] = ++timedfs; ListHLD.push_back(u); if(g[u].size()==1){ Forv(x, ListHLD) head[x] = ListHLD[0]; ListHLD.clear(); } Forv(v, g[u]) if(v ^ par){hld(v, u);} en[u] = timedfs; } namespace subtrau{ void Solve(){ For(i, 1, n - 1){ auto [u, v] = E[i]; vector<int> List; int a = u; while(a){ List.push_back(a); a = p[a]; } int ans = 0; int cc = c[v]; For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){ if (c[List[i]] < c[List[j]])++ans; } set<int> ss; Forv(x, List)ss.insert(c[x]); Forv(x, List) c[x] = cc; cout << ans << '\n'; } } } namespace subfull{ int st[MAXN << 2], lazy[MAXN << 2]; ll tmp = 0; int bit[MAXN]; void updBIT(int u, int val){ while(u <= 1e5) bit[u] += val, u += u &- u; } int getBIT(int u){ ll ans = 0; while(u) ans += bit[u], u -= u &- u; return ans; } void down(int id, int l, int r){ if(!lazy[id]) return; st[id << 1] = lazy[id], lazy[id << 1] = lazy[id]; st[id << 1 | 1] = lazy[id], lazy[id << 1 | 1] = lazy[id]; lazy[id] = 0; } vector<ii> ListUPD; void update(int id, int l, int r, int u, int v, int val){ if(v < l || r < u) return; if (l == r){ if(st[id]) tmp += getBIT(st[id] - 1), updBIT(st[id], 1), ListUPD.push_back({st[id], 1}); st[id] = val, lazy[id] = val; return; } else if(u <= l && r <= v && st[id]){ tmp += getBIT(st[id] - 1) * (r - l + 1),updBIT(st[id], r-l+1), ListUPD.push_back({st[id], r-l+1}); st[id] = val, lazy[id] = val; return; } down(id, l, r); int mid = l + r >> 1; update(id << 1 | 1, mid + 1, r, u, v, val); update(id << 1, l, mid, u, v, val); st[id] = (st[id << 1] == st[id << 1 | 1]) * st[id << 1]; } void Solve(){ update(1, 1, n, 1, 1, c[1]); For(i, 1, n - 1){ auto [u, v] = E[i]; tmp = 0; int a = v; while(head[a]!=head[1]){ update(1, 1, n, in[head[a]], in[a], c[v]); a = p[head[a]]; } update(1, 1, n, 1, in[a], c[v]); // if (i==4) cerr << 1 << ' ' << in[a] << '\n'; for(auto [val, cnt] : ListUPD) updBIT(val, -cnt); ListUPD.clear(); cout << tmp << '\n'; } } } void Solve() { cin >> n; vector<int> compress; For(i, 1, n) cin >> c[i], compress.push_back(c[i]); sort(all(compress)), compress.resize(unique(all(compress)) - compress.begin()); For(i, 1, n) c[i] = lower_bound(all(compress), c[i]) - compress.begin() + 1; For(i, 1, n - 1){ auto&[u, v] = E[i]; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } For(i, 1, n) sort(all(g[i]), [&](int x, int y){ return sz[x] > sz[y]; }); dfsInit(1, 0); hld(1, 0); // subtrau::Solve(); subfull::Solve(); } int32_t main() { cin.tie(0) -> sync_with_stdio(0); if(fopen("JOI18_construction.inp", "r")) { freopen("JOI18_construction.inp", "r", stdin); freopen("JOI18_construction.out", "w", stdout); } int t = 1; // cin >> t; for (int test = 1; test <= t; test++) { Solve(); } return 0; }

Compilation message (stderr)

construction.cpp: In function 'void subtrau::Solve()':
construction.cpp:10:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 | #define For(i, a, b)   for(int i = a; i <= b; ++i)
......
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |                 ~~~~~~~~~~~~~~~~~~~~~    
construction.cpp:71:13: note: in expansion of macro 'For'
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |             ^~~
construction.cpp:10:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 | #define For(i, a, b)   for(int i = a; i <= b; ++i)
......
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:71:40: note: in expansion of macro 'For'
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |                                        ^~~
construction.cpp: In function 'void subfull::update(int, int, int, int, int, int)':
construction.cpp:114:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  114 |         int mid = l + r >> 1;
      |                   ~~^~~
construction.cpp: In function 'int32_t main()':
construction.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |         freopen("JOI18_construction.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |         freopen("JOI18_construction.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...