제출 #901939

#제출 시각아이디문제언어결과실행 시간메모리
901939nguyentunglam코끼리 (Dancing Elephants) (IOI11_elephants)C++17
100 / 100
2137 ms33904 KiB
#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

컴파일 시 표준 에러 (stderr) 메시지

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...