Submission #901939

# Submission time Handle Problem Language Result Execution time Memory
901939 2024-01-10T04:36:28 Z nguyentunglam Dancing Elephants (IOI11_elephants) C++17
100 / 100
2137 ms 33904 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;

const int N = 150000 + 10, T = 1000, M = 150000 + 10;

int n, l, idblock, invalid = M + 1;

int a[N], b[N];

int pre[M], nxt[M];

vector<pair<int, int> > f[M];

vector<int> arr[M];

void change (vector<int> &v, int x, bool k) {
  if (k) {
    for(int i = 0; i < v.size(); i++) if (v[i] >= x) {
      v.insert(v.begin() + i, x);
      return;
    }
    v.push_back(x);
  }
  else {
    for(int i = 0; i < v.size(); i++) if (v[i] == x) {
      v.erase(v.begin() + i);
      return;
    }
  }
}

void update_block (int block) {
  f[block].resize(arr[block].size());
  for(int i = arr[block].size() - 1, j = arr[block].size(); i >= 0; i--) {
    while (j > 0 && arr[block][j - 1] - l > arr[block][i]) {
      j--;
    }
    if (j == arr[block].size()) f[block][i] = make_pair(1, arr[block][i]);
    else f[block][i] = make_pair(f[block][j].first + 1, f[block][j].second);
  }
}

int get() {
  int last = -1e9, ans = 0;
  for(int block = nxt[0]; block != invalid; block = nxt[block]) {
    int x = upper_bound(arr[block].begin(), arr[block].end(), last + l) - arr[block].begin();
    if (x < arr[block].size()) {
      ans += f[block][x].first;
      last = f[block][x].second;
    }
  }
  return ans;
}

int group (int x) {
  int cur = nxt[0];
  while (nxt[cur] != invalid && arr[nxt[cur]][0] <= x) cur = nxt[cur];
  return cur;
}

void add(int x) {
  int block = group(x);
  change(arr[block], x, 1);
  if (arr[block].size() <= T) {
    update_block(block);
    return;
  }
  int L = pre[block], R = nxt[block];
  int block1 = ++idblock;
  int block2 = ++idblock;
  nxt[L] = block1;
  pre[block1] = L;
  nxt[block1] = block2;
  pre[block2] = block1;
  nxt[block2] = R;
  pre[R] = block2;
  for(int i = 0; i < arr[block].size(); i++) {
    if (i < arr[block].size() / 2) arr[block1].push_back(arr[block][i]);
    else arr[block2].push_back(arr[block][i]);
  }
  update_block(block1);
  update_block(block2);
}

void del(int x) {
  int block = group(x);
  change(arr[block], x, 0);
  if (arr[block].empty()) {
    int L = pre[block], R = nxt[block];
    nxt[L] = R;
    pre[R] = L;
  }
  else update_block(block);
}

void init(int N, int L, int X[])
{
  n = N; l = L;

  for(int i = 0; i < n; i++) a[i] = X[i];

  nxt[0] = idblock = 1;
  pre[idblock] = 0;
  nxt[idblock] = invalid;

  arr[1].push_back(a[0]);
  update_block(1);


  for(int i = 1; i < n; i++) add(a[i]);
}


int update(int i, int y)
{
  del(a[i]);
  a[i] = y;
  add(a[i]);

  return get();
}

#ifdef ngu
int x[N];
int main() {

  freopen ("task.inp", "r", stdin);
  freopen ("task.out", "w", stdout);

  int n, l, m; cin >> n >> l >> m;

  for(int i = 0; i < n; i++) cin >> x[i];

  init(n, l, x);

  while (m--) {
    int i, v; cin >> i >> v;
    cout << update(i, v) << endl;
  }

  cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
}
#endif // ngu

Compilation message

elephants.cpp: In function 'void change(std::vector<int>&, int, bool)':
elephants.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int i = 0; i < v.size(); i++) if (v[i] >= x) {
      |                    ~~^~~~~~~~~~
elephants.cpp:29:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i = 0; i < v.size(); i++) if (v[i] == x) {
      |                    ~~^~~~~~~~~~
elephants.cpp: In function 'void update_block(int)':
elephants.cpp:42:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     if (j == arr[block].size()) f[block][i] = make_pair(1, arr[block][i]);
      |         ~~^~~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'int get()':
elephants.cpp:51:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     if (x < arr[block].size()) {
      |         ~~^~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'void add(int)':
elephants.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |   for(int i = 0; i < arr[block].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~
elephants.cpp:82:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     if (i < arr[block].size() / 2) arr[block1].push_back(arr[block][i]);
      |         ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16984 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16984 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16984 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 217 ms 17492 KB Output is correct
8 Correct 225 ms 17920 KB Output is correct
9 Correct 320 ms 19024 KB Output is correct
10 Correct 324 ms 21072 KB Output is correct
11 Correct 291 ms 20816 KB Output is correct
12 Correct 472 ms 19904 KB Output is correct
13 Correct 327 ms 20916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16984 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 217 ms 17492 KB Output is correct
8 Correct 225 ms 17920 KB Output is correct
9 Correct 320 ms 19024 KB Output is correct
10 Correct 324 ms 21072 KB Output is correct
11 Correct 291 ms 20816 KB Output is correct
12 Correct 472 ms 19904 KB Output is correct
13 Correct 327 ms 20916 KB Output is correct
14 Correct 252 ms 20304 KB Output is correct
15 Correct 323 ms 18088 KB Output is correct
16 Correct 826 ms 19912 KB Output is correct
17 Correct 782 ms 21400 KB Output is correct
18 Correct 822 ms 20696 KB Output is correct
19 Correct 479 ms 22460 KB Output is correct
20 Correct 789 ms 21440 KB Output is correct
21 Correct 741 ms 21136 KB Output is correct
22 Correct 506 ms 22480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16984 KB Output is correct
3 Correct 3 ms 16984 KB Output is correct
4 Correct 4 ms 16988 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 217 ms 17492 KB Output is correct
8 Correct 225 ms 17920 KB Output is correct
9 Correct 320 ms 19024 KB Output is correct
10 Correct 324 ms 21072 KB Output is correct
11 Correct 291 ms 20816 KB Output is correct
12 Correct 472 ms 19904 KB Output is correct
13 Correct 327 ms 20916 KB Output is correct
14 Correct 252 ms 20304 KB Output is correct
15 Correct 323 ms 18088 KB Output is correct
16 Correct 826 ms 19912 KB Output is correct
17 Correct 782 ms 21400 KB Output is correct
18 Correct 822 ms 20696 KB Output is correct
19 Correct 479 ms 22460 KB Output is correct
20 Correct 789 ms 21440 KB Output is correct
21 Correct 741 ms 21136 KB Output is correct
22 Correct 506 ms 22480 KB Output is correct
23 Correct 1779 ms 24584 KB Output is correct
24 Correct 1848 ms 29172 KB Output is correct
25 Correct 1465 ms 28552 KB Output is correct
26 Correct 1465 ms 33876 KB Output is correct
27 Correct 1438 ms 33904 KB Output is correct
28 Correct 1069 ms 20468 KB Output is correct
29 Correct 896 ms 20364 KB Output is correct
30 Correct 1072 ms 20312 KB Output is correct
31 Correct 855 ms 20364 KB Output is correct
32 Correct 1390 ms 33272 KB Output is correct
33 Correct 1372 ms 32668 KB Output is correct
34 Correct 1492 ms 33584 KB Output is correct
35 Correct 1270 ms 33780 KB Output is correct
36 Correct 1453 ms 27428 KB Output is correct
37 Correct 1662 ms 28872 KB Output is correct
38 Correct 1398 ms 32376 KB Output is correct
39 Correct 1406 ms 33460 KB Output is correct
40 Correct 1497 ms 32536 KB Output is correct
41 Correct 2137 ms 30576 KB Output is correct
42 Correct 2132 ms 30588 KB Output is correct