Submission #1003957

#TimeUsernameProblemLanguageResultExecution timeMemory
1003957AdamGSConstruction of Highway (JOI18_construction)C++17
100 / 100
822 ms23500 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7, INF=1e9+7;
vector<int>V[LIM];
pair<int,int>kraw[LIM];
int ile[LIM], oc[LIM], sciezka[LIM], nr[LIM], T[LIM], akt;
ll tr[4*LIM], N;
set<pair<pair<int,int>,int>>S;
void DFS(int x) {
  ile[x]=1;
  for(auto i : V[x]) {
    DFS(i);
    ile[x]+=ile[i];
    oc[i]=x;
  }
  if(!V[x].size()) return;
  rep(i, V[x].size()) if(ile[V[x][i]]>ile[V[x][0]]) swap(V[x][i], V[x][0]);
}
void DFS2(int x) {
  nr[x]=akt++;
  if(!V[x].size()) return;
  sciezka[V[x][0]]=sciezka[x];
  for(auto i : V[x]) DFS2(i);
}
void upd(int v, ll x) {
  v+=N;
  while(v) {
    tr[v]+=x;
    v/=2;
  }
}
ll cnt(int l, int r) {
  if(l>r) return 0;
  l+=N; r+=N;
  ll ans=tr[l];
  if(l!=r) ans+=tr[r];
  while(l/2!=r/2) {
    if(l%2==0) ans+=tr[l+1];
    if(r%2==1) ans+=tr[r-1];
    l/=2; r/=2;
  }
  return ans;
}
ll licz(vector<pair<ll,ll>>A) {
  N=1;
  while(N<A.size()) N*=2;
  rep(i, 2*N) tr[i]=0;
  vector<pair<ll,ll>>B;
  rep(i, A.size()) B.pb({A[i].st, -i});
  sort(all(B));
  ll ans=0;
  for(auto i : B) {
    ans+=cnt(0, -i.nd)*A[-i.nd].nd;
    upd(-i.nd, A[-i.nd].nd);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) cin >> T[i];
  rep(i, n-1) {
    cin >> kraw[i].st >> kraw[i].nd;
    --kraw[i].st; --kraw[i].nd;
    V[kraw[i].st].pb(kraw[i].nd);
  }
  DFS(0);
  rep(i, n) sciezka[i]=i;
  DFS2(0);
  rep(i, n) S.insert({{nr[i], nr[i]}, INF});
  rep(i, n-1) {
    int x=kraw[i].nd;
    vector<pair<pair<int,int>,int>>A;
    while(true) {
      auto it=S.lower_bound({{nr[x]+1, -LIM}, -LIM});
      if(it==S.begin()) break;
      --it;
      auto a=*it;
      if(a.st.st<nr[sciezka[x]]) {
        x=oc[sciezka[x]];
        continue;
      }
      S.erase(a);
      if(a.st.nd>nr[x]) {
        S.insert({{nr[x]+1, a.st.nd}, a.nd});
        a.st.nd=nr[x];
      }
      A.pb(a);
    }
    x=kraw[i].nd;
    while(true) {
      S.insert({{nr[sciezka[x]], nr[x]}, T[kraw[i].nd]});
      if(!sciezka[x]) break;
      x=oc[sciezka[x]];
    }
    vector<pair<ll,ll>>B;
    for(auto j : A) B.pb({j.nd, j.st.nd-j.st.st+1});
    cout << licz(B) << '\n';
  }
}

Compilation message (stderr)

construction.cpp: In function 'void DFS(int)':
construction.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
      |                                    ^
construction.cpp:24:3: note: in expansion of macro 'rep'
   24 |   rep(i, V[x].size()) if(ile[V[x][i]]>ile[V[x][0]]) swap(V[x][i], V[x][0]);
      |   ^~~
construction.cpp: In function 'll licz(std::vector<std::pair<long long int, long long int> >)':
construction.cpp:53:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |   while(N<A.size()) N*=2;
      |         ~^~~~~~~~~
construction.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
      |                                    ^
construction.cpp:56:3: note: in expansion of macro 'rep'
   56 |   rep(i, A.size()) B.pb({A[i].st, -i});
      |   ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...