Submission #901937

# Submission time Handle Problem Language Result Execution time Memory
901937 2024-01-10T04:33:46 Z nguyentunglam Dancing Elephants (IOI11_elephants) C++17
97 / 100
9000 ms 50840 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 = 1e5 + 10, T = 100, M = 1e5 + 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);
//  cout << block << endl;
  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]);
  }
//  cout << block1 << " " << block2 << endl;
//  if (x == 17) return;
//  assert(!arr[block1].empty());
//  assert(!arr[block2].empty());
//  return;
  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]);

//  cout << get() << endl;
}


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;
  }
}
#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:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |   for(int i = 0; i < arr[block].size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~
elephants.cpp:83:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     if (i < arr[block].size() / 2) arr[block1].push_back(arr[block][i]);
      |         ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 178 ms 15652 KB Output is correct
8 Correct 259 ms 16172 KB Output is correct
9 Correct 805 ms 18612 KB Output is correct
10 Correct 648 ms 20172 KB Output is correct
11 Correct 674 ms 20204 KB Output is correct
12 Correct 848 ms 19284 KB Output is correct
13 Correct 625 ms 20072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 178 ms 15652 KB Output is correct
8 Correct 259 ms 16172 KB Output is correct
9 Correct 805 ms 18612 KB Output is correct
10 Correct 648 ms 20172 KB Output is correct
11 Correct 674 ms 20204 KB Output is correct
12 Correct 848 ms 19284 KB Output is correct
13 Correct 625 ms 20072 KB Output is correct
14 Correct 303 ms 21780 KB Output is correct
15 Correct 484 ms 19740 KB Output is correct
16 Correct 990 ms 21764 KB Output is correct
17 Correct 1533 ms 23636 KB Output is correct
18 Correct 1553 ms 22768 KB Output is correct
19 Correct 1324 ms 24748 KB Output is correct
20 Correct 1597 ms 23608 KB Output is correct
21 Correct 1679 ms 22880 KB Output is correct
22 Correct 1241 ms 24168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 2 ms 12892 KB Output is correct
7 Correct 178 ms 15652 KB Output is correct
8 Correct 259 ms 16172 KB Output is correct
9 Correct 805 ms 18612 KB Output is correct
10 Correct 648 ms 20172 KB Output is correct
11 Correct 674 ms 20204 KB Output is correct
12 Correct 848 ms 19284 KB Output is correct
13 Correct 625 ms 20072 KB Output is correct
14 Correct 303 ms 21780 KB Output is correct
15 Correct 484 ms 19740 KB Output is correct
16 Correct 990 ms 21764 KB Output is correct
17 Correct 1533 ms 23636 KB Output is correct
18 Correct 1553 ms 22768 KB Output is correct
19 Correct 1324 ms 24748 KB Output is correct
20 Correct 1597 ms 23608 KB Output is correct
21 Correct 1679 ms 22880 KB Output is correct
22 Correct 1241 ms 24168 KB Output is correct
23 Execution timed out 9010 ms 50840 KB Time limit exceeded
24 Halted 0 ms 0 KB -