답안 #962734

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
962734 2024-04-14T07:39:33 Z Cookie Construction of Highway (JOI18_construction) C++14
0 / 100
2000 ms 262144 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 2e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n;
int siz[mxn + 1], pa[mxn + 1], dep[mxn + 1], head[mxn + 1], c[mxn + 1];
deque<pair<int, int>>dq[mxn + 1];
vt<int>adj[mxn + 1];
int bit[mxn + 1];
vt<pii>comp;
void upd(int p, int v, bool add = 1){
    if(add)comp.pb(mpp(p, v));
    while(p <= n){
        bit[p] += v; p += p & (-p);
    }
}
ll get(int p){
    ll ans = 0;
    while(p){
        ans += bit[p]; p -= p & (-p);
    }
    return(ans);
}
void reset(){
    for(auto [node, val]: comp){
        upd(node, -val, 0);
    }
    comp.clear();
}
void dfs(int s, int pre){
    siz[s] = 1; pa[s] = pre;
    for(auto i: adj[s]){
        if(i != pre){
            dep[i] = dep[s] + 1; 
            dfs(i, s); siz[s] += siz[i]; 
        }
    }
}
void hld(int s, int pre, int group){
    dq[group].pb(mpp(c[s], 1)); head[s] = group;
    int mx = 0, son = 0;
    for(auto i: adj[s]){
        if(i != pre){
            if(siz[i] > mx){
                mx = siz[i]; son = i;
            }
        }
    }
    if(!mx)return;
    hld(son, s, group);
    for(auto i: adj[s]){
        if(i != pre && i != son){
            hld(i, s, i);
        }
    }
}
void solve(){
   cin >> n;
   vt<int>cc;
   for(int i = 1; i <= n; i++){
       cin >> c[i]; cc.pb(c[i]);
   }
   sort(ALL(cc));
   for(int i = 1; i <= n; i++){
       c[i] = lower_bound(ALL(cc), c[i]) - cc.begin() + 1;
   }
   vt<pii>edge;
   for(int i = 1; i < n; i++){
       int u, v; cin >> u >> v;
       adj[u].pb(v); adj[v].pb(u);
       edge.pb(mpp(u, v));
   }
   dfs(1, -1);
   hld(1, -1, 1);
   
   
   for(int i = 0; i < n - 1; i++){
      
       int a = edge[i].fi, b = edge[i].se;
       int dif = dep[a] - dep[head[a]] + 1;
       
       vt<pii>query;
       
       while(dif){
            if(dif >= dq[head[a]].front().se){
                dif -= dq[head[a]].front().se;
                query.pb(dq[head[a]].front()); dq[head[a]].pop_front();
               
            }else{
                dq[head[a]].front().se -= dif; 
                query.pb(mpp(dq[head[a]].front().fi, dif));
                dif = 0;
            }
            
       }
   
       dq[head[a]].push_front(mpp(c[b], dep[a] - dep[head[a]] + 1));
     
       reverse(ALL(query));
       a = pa[head[a]];
       if(a != -1){
           int cnt = 10;
           while(head[a] != 1 && cnt--){
               for(int j = sz(dq[head[a]]) - 1; j >= 0; j--){
                   query.pb(dq[head[a]][j]);
               }
               dq[head[a]].clear();
               dq[head[a]].pb(mpp(c[b], dep[a] - dep[head[a]] + 1));
               a = pa[head[a]];
           }
           
           int dif = dep[a] + 1;
           //for(auto j: dq[head[1]])cout << j.fi << " " << j.se << "\n";
           cnt = 10;
           vt<pii>back;
           while(dif && cnt--){
                if(dif >= dq[head[a]].front().se){
                    dif -= dq[head[a]].front().se;
                    back.pb(dq[head[a]].front()); dq[head[a]].pop_front();
                }else{
                    dq[head[a]].front().se -= dif;
                    back.pb(mpp(dq[head[a]].front().fi, dif)); 
                    dif = 0;
                }
           }
           dq[head[a]].push_front(mpp(c[b], dep[a] + 1));
           
           reverse(ALL(back));
           for(auto j: back)query.pb(j);
           
           
       }
       
       ll res = 0;
       for(auto [val, cnt]: query){
           //cout << val << " " << cnt << "\n";
           res += get(val - 1) * cnt; upd(val, cnt); 
       }
       reset();
       cout << res << "\n";
       
       
   }
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("DATA.inp", "r", stdin);
    //freopen("DATA.out", "w", stdout);
    int tt; tt = 1;
    while(tt--){
        solve();
 
    }
    return(0);
}

Compilation message

construction.cpp: In function 'void reset()':
construction.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   49 |     for(auto [node, val]: comp){
      |              ^
construction.cpp: In function 'void solve()':
construction.cpp:159:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  159 |        for(auto [val, cnt]: query){
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 143956 KB Output is correct
2 Correct 80 ms 143960 KB Output is correct
3 Execution timed out 2354 ms 262144 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 143956 KB Output is correct
2 Correct 80 ms 143960 KB Output is correct
3 Execution timed out 2354 ms 262144 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 143956 KB Output is correct
2 Correct 80 ms 143960 KB Output is correct
3 Execution timed out 2354 ms 262144 KB Time limit exceeded
4 Halted 0 ms 0 KB -