Submission #1103796

# Submission time Handle Problem Language Result Execution time Memory
1103796 2024-10-21T18:19:52 Z VinhLuu Nile (IOI24_nile) C++17
100 / 100
135 ms 49872 KB
#include <bits/stdc++.h>
#define ll long long
#define all(vin) vin.begin(), vin.end()
using namespace std;
 
typedef tuple<ll,ll,ll> tp;
const int N = 2e5 + 5;
const int oo = 2e9;
 
ll n, q, W[N], a[N], b[N], d[N], c[N], s[N], le[N], ri[N], w[2][N], kq[N], idx[N];
 
int fin_left(int u){
  return le[u] == u ? u : le[u] = fin_left(le[u]);
}
 
int fin_right(int u){
  return ri[u] == u ? u : ri[u] = fin_right(ri[u]);
}
 
vector<int> open[N], bridge[N];
struct ST{
  ll st[N << 1];
 
  void re(int sz){
    for(int i = 1; i <= 2 * sz; i ++) st[i] = oo;
  }
 
  void update(int i,ll x){
    i += n - 1;
    st[i] = x;
    while(i > 1){
      i /= 2;
      st[i] = min(st[i << 1], st[i << 1|1]);
    }
  }
 
  ll get(int l,int r){
    if(l > r) return oo;
    r++;
    ll ret = oo;
    for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
      if(l & 1) ret = min(ret, st[l ++]);
      if(r & 1) ret = min(ret, st[-- r]);
    }
    return ret;
  }
} T[2], tree[2];
 
vector<long long> calculate_costs(
    vector<int> _W, vector<int> A,
    vector<int> B, vector<int> E){
  n = (int)_W.size();
 
  T[0].re(n);
  T[1].re(n);
  tree[0].re(n);
  tree[1].re(n);
 
  for(int i = 1; i <= n; i ++){
    W[i] = _W[i - 1];
    a[i] = A[i - 1];
    b[i] = B[i - 1];
    idx[i] = i;
  }
 
  sort(idx + 1, idx + n + 1, [&](int x,int y){return W[x] > W[y];});
 
 
  ll ans = 0;
 
  for(int id = 1; id <= n; id ++){
    int i = idx[id];
    tree[id % 2].update(id, a[i] - b[i]);
    ans += a[i];
    s[id] = s[id - 1] + b[i];
    w[0][id] = w[1][id] = a[i];
    le[id] = id, ri[id] = id;
  }
  q = (int)E.size();
 
  for(int i = 1; i <= q; i ++){
    d[i] = E[i - 1];
    c[i] = i;
  }
  sort(c + 1, c + q + 1, [&](int x,int y){return d[x] < d[y];});
 
 
  for(int id = 1; id < n; id ++){
    int i = idx[id], nxt = idx[id + 1];
    int l = 1, r = q, mid, pos = q + 1;
    while(l <= r){
      mid = (l + r) / 2;
      if(d[c[mid]] >= W[i] - W[nxt]){
        pos = mid;
        r = mid - 1;
      }else l = mid + 1;
    }
    if(pos != q + 1){
      open[pos].push_back(id);
    }
  }
 
  for(int id = 2; id < n; id ++){
    int _left = idx[id - 1], _right = idx[id + 1];
    int l = 1, r = q, mid, pos = q + 1;
    while(l <= r){
      mid = (l + r) / 2;
      if(d[c[mid]] >= W[_left] - W[_right]){
        pos = mid;
        r = mid - 1;
      }else l = mid + 1;
    }
    if(pos != q + 1){
      bridge[pos].push_back(id);
    }
  }
 
 
  for(int k = 1; k <= q; k ++){
    for(auto id : bridge[k]){
      int j = idx[id];
      T[id % 2].update(id, a[j] - b[j]);
      int _left = fin_left(id);
      int _right = fin_right(id);
      ans = ans - w[0][_left];
      ll val = 0;
      if((_right - _left + 1) % 2){
        int tc = _left % 2;
        val = s[_right] - s[_left - 1] + min(tree[tc].get(_left, _right), T[tc ^ 1].get(_left, _right));
      }else val = s[_right] - s[_left - 1];
      ans += val;
      w[0][_left] = w[1][_right] = val;
    }
    for(auto j : open[k]){
      int _right = fin_right(j + 1);
      int _left = fin_left(j);
      ans = ans - w[0][j + 1] - w[1][j];
      ll val = 0;
      int tc = _left % 2;
      if((_right - _left + 1) % 2){
          val = s[_right] - s[_left - 1] + min(tree[tc].get(_left, _right), T[tc ^ 1].get(_left, _right));;
      }else val = s[_right] - s[_left - 1];
      ans += val;
      w[1][_right] = w[0][_left] = val;
      ri[j] = j + 1;
      le[j + 1] = j;
    }
    kq[c[k]] = ans;
  }
 
  vector<ll> _ans;
  for(int i = 1; i <= q; i ++) _ans.push_back(kq[i]);
  return _ans;
}
 
//
//signed main(){
//  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//  #define task "v"
//  if(fopen(task ".inp","r")){
//    freopen(task ".inp","r",stdin);
//    freopen(task ".ans","w",stdout);
//  }
//
//  vector<int> _W, _A, _B;
//
//  cin >> n;
//
//  for(int i = 1; i <= n; i ++){
//    int x, y, z; cin >> x >> y >> z;
//    _W.push_back(x);
//    _A.push_back(y);
//    _B.push_back(z);
//  }
//  vector<int> _E;
//  int q;  cin >> q;
//  for(int i = 1; i <= q; i ++){
//    int x; cin >> x;
//    _E.push_back(x);
//  }
//  vector<long long> _ans = calculate_costs(_W, _A, _B, _E);
//  for(auto j : _ans) cout << j << "\n";
//
//}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31480 KB Output is correct
2 Correct 5 ms 31312 KB Output is correct
3 Correct 6 ms 31312 KB Output is correct
4 Correct 5 ms 31312 KB Output is correct
5 Correct 5 ms 31484 KB Output is correct
6 Correct 5 ms 31312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 43328 KB Output is correct
2 Correct 55 ms 45660 KB Output is correct
3 Correct 56 ms 45896 KB Output is correct
4 Correct 58 ms 45704 KB Output is correct
5 Correct 49 ms 45600 KB Output is correct
6 Correct 49 ms 45632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 43908 KB Output is correct
2 Correct 55 ms 42948 KB Output is correct
3 Correct 54 ms 43716 KB Output is correct
4 Correct 52 ms 43604 KB Output is correct
5 Correct 57 ms 43328 KB Output is correct
6 Correct 52 ms 43524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31480 KB Output is correct
2 Correct 5 ms 31312 KB Output is correct
3 Correct 6 ms 31312 KB Output is correct
4 Correct 5 ms 31312 KB Output is correct
5 Correct 5 ms 31484 KB Output is correct
6 Correct 5 ms 31312 KB Output is correct
7 Correct 6 ms 31312 KB Output is correct
8 Correct 6 ms 31328 KB Output is correct
9 Correct 6 ms 31312 KB Output is correct
10 Correct 6 ms 31324 KB Output is correct
11 Correct 5 ms 31312 KB Output is correct
12 Correct 6 ms 31216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31480 KB Output is correct
2 Correct 5 ms 31312 KB Output is correct
3 Correct 6 ms 31312 KB Output is correct
4 Correct 5 ms 31312 KB Output is correct
5 Correct 5 ms 31484 KB Output is correct
6 Correct 5 ms 31312 KB Output is correct
7 Correct 52 ms 43328 KB Output is correct
8 Correct 55 ms 45660 KB Output is correct
9 Correct 56 ms 45896 KB Output is correct
10 Correct 58 ms 45704 KB Output is correct
11 Correct 49 ms 45600 KB Output is correct
12 Correct 49 ms 45632 KB Output is correct
13 Correct 55 ms 43908 KB Output is correct
14 Correct 55 ms 42948 KB Output is correct
15 Correct 54 ms 43716 KB Output is correct
16 Correct 52 ms 43604 KB Output is correct
17 Correct 57 ms 43328 KB Output is correct
18 Correct 52 ms 43524 KB Output is correct
19 Correct 6 ms 31312 KB Output is correct
20 Correct 6 ms 31328 KB Output is correct
21 Correct 6 ms 31312 KB Output is correct
22 Correct 6 ms 31324 KB Output is correct
23 Correct 5 ms 31312 KB Output is correct
24 Correct 6 ms 31216 KB Output is correct
25 Correct 66 ms 45588 KB Output is correct
26 Correct 69 ms 45664 KB Output is correct
27 Correct 56 ms 45332 KB Output is correct
28 Correct 59 ms 42536 KB Output is correct
29 Correct 59 ms 44992 KB Output is correct
30 Correct 58 ms 45072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 43908 KB Output is correct
2 Correct 55 ms 42948 KB Output is correct
3 Correct 54 ms 43716 KB Output is correct
4 Correct 52 ms 43604 KB Output is correct
5 Correct 57 ms 43328 KB Output is correct
6 Correct 52 ms 43524 KB Output is correct
7 Correct 94 ms 47060 KB Output is correct
8 Correct 112 ms 48964 KB Output is correct
9 Correct 112 ms 46412 KB Output is correct
10 Correct 111 ms 48352 KB Output is correct
11 Correct 102 ms 48200 KB Output is correct
12 Correct 103 ms 46816 KB Output is correct
13 Correct 112 ms 46336 KB Output is correct
14 Correct 66 ms 47040 KB Output is correct
15 Correct 100 ms 46536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31312 KB Output is correct
2 Correct 5 ms 31480 KB Output is correct
3 Correct 5 ms 31312 KB Output is correct
4 Correct 6 ms 31312 KB Output is correct
5 Correct 5 ms 31312 KB Output is correct
6 Correct 5 ms 31484 KB Output is correct
7 Correct 5 ms 31312 KB Output is correct
8 Correct 52 ms 43328 KB Output is correct
9 Correct 55 ms 45660 KB Output is correct
10 Correct 56 ms 45896 KB Output is correct
11 Correct 58 ms 45704 KB Output is correct
12 Correct 49 ms 45600 KB Output is correct
13 Correct 49 ms 45632 KB Output is correct
14 Correct 55 ms 43908 KB Output is correct
15 Correct 55 ms 42948 KB Output is correct
16 Correct 54 ms 43716 KB Output is correct
17 Correct 52 ms 43604 KB Output is correct
18 Correct 57 ms 43328 KB Output is correct
19 Correct 52 ms 43524 KB Output is correct
20 Correct 6 ms 31312 KB Output is correct
21 Correct 6 ms 31328 KB Output is correct
22 Correct 6 ms 31312 KB Output is correct
23 Correct 6 ms 31324 KB Output is correct
24 Correct 5 ms 31312 KB Output is correct
25 Correct 6 ms 31216 KB Output is correct
26 Correct 66 ms 45588 KB Output is correct
27 Correct 69 ms 45664 KB Output is correct
28 Correct 56 ms 45332 KB Output is correct
29 Correct 59 ms 42536 KB Output is correct
30 Correct 59 ms 44992 KB Output is correct
31 Correct 58 ms 45072 KB Output is correct
32 Correct 94 ms 47060 KB Output is correct
33 Correct 112 ms 48964 KB Output is correct
34 Correct 112 ms 46412 KB Output is correct
35 Correct 111 ms 48352 KB Output is correct
36 Correct 102 ms 48200 KB Output is correct
37 Correct 103 ms 46816 KB Output is correct
38 Correct 112 ms 46336 KB Output is correct
39 Correct 66 ms 47040 KB Output is correct
40 Correct 100 ms 46536 KB Output is correct
41 Correct 95 ms 47080 KB Output is correct
42 Correct 96 ms 47428 KB Output is correct
43 Correct 112 ms 46408 KB Output is correct
44 Correct 116 ms 49740 KB Output is correct
45 Correct 135 ms 49732 KB Output is correct
46 Correct 117 ms 49580 KB Output is correct
47 Correct 120 ms 49872 KB Output is correct
48 Correct 77 ms 48076 KB Output is correct
49 Correct 116 ms 47832 KB Output is correct