Submission #1103844

# Submission time Handle Problem Language Result Execution time Memory
1103844 2024-10-22T02:19:14 Z zNatsumi Nile (IOI24_nile) C++17
100 / 100
135 ms 18632 KB
#include <bits/stdc++.h>

using namespace std;


typedef pair<int, int> ii;
#define ll long long

const int N = 1e5 + 5;
const ll INF = 1e16;

int n, q, pa[N], sz[N], odd_min[N], even_min[N], L[N], R[N];
ll answer[N], res = 0LL;
ii que[N];

struct info{
  int w, a, b;
  info() {}
  info(int w, int a, int b): w(w), a(a), b(b) {}

  friend istream& operator >> (istream& is, info &rhs){
    return is >> rhs.w >> rhs.a >> rhs.b;
  }
  friend ostream& operator << (ostream& os, info rhs){
    return os << "(" << rhs.w << ", " << rhs.a << ", " << rhs.b << ")";
  }
} item[N];

struct Edge{
  int u, v, w;
  Edge() {}
  Edge(int u, int v, ll w): u(u), v(v), w(w) {}

  friend ostream& operator << (ostream &os, Edge &rhs){
    return os << "(" << rhs.u << ", " << rhs.v << ", " << rhs.w << ")";
  }
} edge[2 * N];

struct SegTree{
  ll st[N << 2];

  void build(int tl, int tr, int i){
    st[i] = INF;
    if(tl == tr) return;
    int tm = tl + tr >> 1;
    build(tl, tm, i<<1);
    build(tm + 1, tr, i<<1|1);
  }
  void upd(int pos, int val, int tl, int tr, int i){
    if(tl == tr){
      st[i] = val;
      return;
    }
    int tm = tl + tr >> 1;
    if(pos <= tm) upd(pos, val, tl, tm, i<<1);
    else upd(pos, val, tm + 1, tr, i<<1|1);
    st[i] = min(st[i<<1], st[i<<1|1]);
  }
  int get(int l, int r, int tl, int tr, int i){
    if(r < tl || tr < l || l > r) return INF;
    if(l <= tl && tr <= r) return st[i];
    int tm = tl + tr >> 1;
    return min(get(l, r, tl, tm, i<<1), get(l, r, tm + 1, tr, i<<1|1));
  }
} it;

int fset(int i){ return i == pa[i] ? i : pa[i] = fset(pa[i]); }
void uset(int u, int v){
  int pu = fset(u),
      pv = fset(v);
  if(pu == pv){
    if(u == v - 1) return;
    u += 1;
    if((R[pu] - L[pu] + 1) % 2) res -= min(odd_min[pu], it.get(L[pu], R[pu], 1, n, 1));
    it.upd(u, item[u].a - item[u].b, 1, n, 1);
    if((R[pu] - L[pu] + 1) % 2) res += min(odd_min[pu], it.get(L[pu], R[pu], 1, n, 1));
    return;
  }
  if(sz[pu] % 2) res -= min(odd_min[pu], it.get(L[pu], R[pu], 1, n, 1));
  if(sz[pv] % 2) res -= min(odd_min[pv], it.get(L[pv], R[pv], 1, n, 1));

  if(sz[pu] % 2){
    odd_min[pu] = min(odd_min[pu], even_min[pv]);
    even_min[pu] = min(even_min[pu], odd_min[pv]);
  }else{
    odd_min[pu] = min(odd_min[pu], odd_min[pv]);
    even_min[pu] = min(even_min[pu], even_min[pv]);
  }
  R[pu] = R[pv];
  sz[pu] += sz[pv];

  if(sz[pu] % 2) res += min(odd_min[pu], it.get(L[pu], R[pu], 1, n, 1));
  pa[pv] = pu;
}

vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E){
  n = W.size();
  for(int i = 1; i <= n; i++) item[i] = info(W[i - 1], A[i - 1], B[i - 1]);
  q = E.size();
  for(int i = 1; i <= q; i++) que[i] = make_pair(E[i - 1], i);

  sort(que + 1, que + q + 1);
  for(int i = 1; i <= n; i++) pa[i] = i, sz[i] = 1;

  sort(item + 1, item + n + 1, [](info x, info y){
        return x.w < y.w;
       });

  int ti = 0;
  for(int i = 1; i < n; i++){
    edge[++ti] = Edge(i, i + 1, item[i + 1].w - item[i].w);
    if(i + 2 <= n) edge[++ti] = Edge(i, i + 2, item[i + 2].w - item[i].w);
  }
  for(int i = 1; i <= n; i++){
    odd_min[i] = item[i].a - item[i].b;
    even_min[i] = INF;
    L[i] = i; R[i] = i;
    res += item[i].a;
  }

  sort(edge + 1, edge + ti + 1, [](Edge x, Edge y){
        return make_pair(x.w, abs(x.u - x.v)) < make_pair(y.w, abs(y.u - y.v));
       });

  it.build(1, n, 1);

  for(int i = 1, j = 0; i <= q; i++){
    while(j < ti && edge[j + 1].w <= que[i].first){
      ++j;
      uset(edge[j].u, edge[j].v);
    }
    answer[que[i].second] = res;
  }
  vector<long long> R;
  for(int i = 1; i <= q; i++) R.push_back(answer[i]);
  return R;
}

Compilation message

nile.cpp: In member function 'void SegTree::build(int, int, int)':
nile.cpp:45:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |     int tm = tl + tr >> 1;
      |              ~~~^~~~
nile.cpp: In member function 'void SegTree::upd(int, int, int, int, int)':
nile.cpp:54:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   54 |     int tm = tl + tr >> 1;
      |              ~~~^~~~
nile.cpp: In member function 'int SegTree::get(int, int, int, int, int)':
nile.cpp:60:42: warning: overflow in conversion from 'long long int' to 'int' changes value from '10000000000000000' to '1874919424' [-Woverflow]
   60 |     if(r < tl || tr < l || l > r) return INF;
      |                                          ^~~
nile.cpp:62:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   62 |     int tm = tl + tr >> 1;
      |              ~~~^~~~
nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:116:19: warning: overflow in conversion from 'long long int' to 'int' changes value from '10000000000000000' to '1874919424' [-Woverflow]
  116 |     even_min[i] = INF;
      |                   ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8696 KB Output is correct
2 Correct 3 ms 8528 KB Output is correct
3 Correct 3 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 13136 KB Output is correct
2 Correct 60 ms 15440 KB Output is correct
3 Correct 58 ms 15696 KB Output is correct
4 Correct 61 ms 15176 KB Output is correct
5 Correct 72 ms 15816 KB Output is correct
6 Correct 59 ms 15688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 13896 KB Output is correct
2 Correct 60 ms 13188 KB Output is correct
3 Correct 86 ms 14664 KB Output is correct
4 Correct 93 ms 14664 KB Output is correct
5 Correct 79 ms 13128 KB Output is correct
6 Correct 82 ms 14408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8696 KB Output is correct
2 Correct 3 ms 8528 KB Output is correct
3 Correct 3 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8528 KB Output is correct
7 Correct 3 ms 8528 KB Output is correct
8 Correct 2 ms 8528 KB Output is correct
9 Correct 4 ms 8784 KB Output is correct
10 Correct 4 ms 8784 KB Output is correct
11 Correct 3 ms 8528 KB Output is correct
12 Correct 3 ms 8668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8696 KB Output is correct
2 Correct 3 ms 8528 KB Output is correct
3 Correct 3 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8528 KB Output is correct
7 Correct 56 ms 13136 KB Output is correct
8 Correct 60 ms 15440 KB Output is correct
9 Correct 58 ms 15696 KB Output is correct
10 Correct 61 ms 15176 KB Output is correct
11 Correct 72 ms 15816 KB Output is correct
12 Correct 59 ms 15688 KB Output is correct
13 Correct 60 ms 13896 KB Output is correct
14 Correct 60 ms 13188 KB Output is correct
15 Correct 86 ms 14664 KB Output is correct
16 Correct 93 ms 14664 KB Output is correct
17 Correct 79 ms 13128 KB Output is correct
18 Correct 82 ms 14408 KB Output is correct
19 Correct 3 ms 8528 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
21 Correct 4 ms 8784 KB Output is correct
22 Correct 4 ms 8784 KB Output is correct
23 Correct 3 ms 8528 KB Output is correct
24 Correct 3 ms 8668 KB Output is correct
25 Correct 68 ms 15544 KB Output is correct
26 Correct 77 ms 16064 KB Output is correct
27 Correct 91 ms 16200 KB Output is correct
28 Correct 94 ms 13384 KB Output is correct
29 Correct 73 ms 15688 KB Output is correct
30 Correct 88 ms 15944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 13896 KB Output is correct
2 Correct 60 ms 13188 KB Output is correct
3 Correct 86 ms 14664 KB Output is correct
4 Correct 93 ms 14664 KB Output is correct
5 Correct 79 ms 13128 KB Output is correct
6 Correct 82 ms 14408 KB Output is correct
7 Correct 81 ms 15296 KB Output is correct
8 Correct 86 ms 17156 KB Output is correct
9 Correct 109 ms 15296 KB Output is correct
10 Correct 112 ms 17088 KB Output is correct
11 Correct 113 ms 17088 KB Output is correct
12 Correct 108 ms 15296 KB Output is correct
13 Correct 118 ms 15296 KB Output is correct
14 Correct 110 ms 16824 KB Output is correct
15 Correct 104 ms 16960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 3 ms 8696 KB Output is correct
3 Correct 3 ms 8528 KB Output is correct
4 Correct 3 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8528 KB Output is correct
7 Correct 3 ms 8528 KB Output is correct
8 Correct 56 ms 13136 KB Output is correct
9 Correct 60 ms 15440 KB Output is correct
10 Correct 58 ms 15696 KB Output is correct
11 Correct 61 ms 15176 KB Output is correct
12 Correct 72 ms 15816 KB Output is correct
13 Correct 59 ms 15688 KB Output is correct
14 Correct 60 ms 13896 KB Output is correct
15 Correct 60 ms 13188 KB Output is correct
16 Correct 86 ms 14664 KB Output is correct
17 Correct 93 ms 14664 KB Output is correct
18 Correct 79 ms 13128 KB Output is correct
19 Correct 82 ms 14408 KB Output is correct
20 Correct 3 ms 8528 KB Output is correct
21 Correct 2 ms 8528 KB Output is correct
22 Correct 4 ms 8784 KB Output is correct
23 Correct 4 ms 8784 KB Output is correct
24 Correct 3 ms 8528 KB Output is correct
25 Correct 3 ms 8668 KB Output is correct
26 Correct 68 ms 15544 KB Output is correct
27 Correct 77 ms 16064 KB Output is correct
28 Correct 91 ms 16200 KB Output is correct
29 Correct 94 ms 13384 KB Output is correct
30 Correct 73 ms 15688 KB Output is correct
31 Correct 88 ms 15944 KB Output is correct
32 Correct 81 ms 15296 KB Output is correct
33 Correct 86 ms 17156 KB Output is correct
34 Correct 109 ms 15296 KB Output is correct
35 Correct 112 ms 17088 KB Output is correct
36 Correct 113 ms 17088 KB Output is correct
37 Correct 108 ms 15296 KB Output is correct
38 Correct 118 ms 15296 KB Output is correct
39 Correct 110 ms 16824 KB Output is correct
40 Correct 104 ms 16960 KB Output is correct
41 Correct 85 ms 15296 KB Output is correct
42 Correct 91 ms 15580 KB Output is correct
43 Correct 116 ms 15296 KB Output is correct
44 Correct 119 ms 18624 KB Output is correct
45 Correct 132 ms 18624 KB Output is correct
46 Correct 135 ms 18632 KB Output is correct
47 Correct 122 ms 18624 KB Output is correct
48 Correct 98 ms 18452 KB Output is correct
49 Correct 120 ms 17344 KB Output is correct