Submission #726098

# Submission time Handle Problem Language Result Execution time Memory
726098 2023-04-18T13:14:13 Z Alex_tz307 Dancing Elephants (IOI11_elephants) C++17
100 / 100
7954 ms 13644 KB
#include <bits/stdc++.h>
#include "elephants.h"

using namespace std;

const int kN = 1e5 + 5e4;
const int bucketSize = 500;
const int kBuckets = 300;
int n, len, a[kN], bucket[kN], indexInBucket[kN];
int updates, auxSize, auxIndices[kN];

class bucket_t {
  private:
    int N, indices[2 * bucketSize];
    int req[2 * bucketSize], maxReach[2 * bucketSize];

  public:
    void reset() {
      N = 0;
    }

    bool isEmpty() {
      return N == 0;
    }

    int getSize() {
      return N;
    }

    int last() {
      return a[indices[N - 1]];
    }

    void add(int index) {
      indices[N++] = index;
    }

    void addAll() {
      for (int i = 0; i < N; ++i) {
        auxIndices[auxSize++] = indices[i];
      }
    }

    int jumps(int index) {
      return req[index];
    }

    int furthest(int index) {
      return maxReach[index];
    }

    void build() {
      int r = N - 1;

      for (int l = N - 1; l >= 0; --l) {
        while (a[indices[r]] - a[indices[l]] > len) {
          r -= 1;
        }

        if (r == N - 1) {
          req[l] = 1;
          maxReach[l] = a[indices[l]] + len;
        } else {
          req[l] = req[r + 1] + 1;
          maxReach[l] = maxReach[r + 1];
        }
      }
    }

    void rem(int pos) {
      for (int i = pos; i < N - 1; ++i) {
        indices[i] = indices[i + 1];
        indexInBucket[indices[i]] = i;
      }

      N -= 1;

      build();
    }

    void ins(int index) {
      int ptr = N - 1;

      while (ptr >= 0 && a[index] < a[indices[ptr]]) {
        ptr -= 1;
      }

      for (int i = N; i > ptr + 1; --i) {
        indices[i] = indices[i - 1];
        indexInBucket[indices[i]] = i;
      }

      indices[ptr + 1] = index;
      indexInBucket[index] = ptr + 1;

      N += 1;

      build();
    }

    int upperBound(int x) {
      int l = -1, r = N;

      while (r - l > 1) {
        int mid = (l + r) / 2;

        if (mid != -1 && (mid == N || x < a[indices[mid]])) {
          r = mid;
        } else {
          l = mid;
        }
      }

      return r;
    }
} buckets[kBuckets];

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

  for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
    buckets[i].reset();
  }

  for (int i = 0; i < n; ++i) {
    a[i] = X[i];
    bucket[i] = i / bucketSize;
    indexInBucket[i] = i % bucketSize;
    buckets[i / bucketSize].add(i);
  }

  for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
    buckets[i].build();
  }
}

void rebuild() {
  auxSize = 0;

  for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
    buckets[i].addAll();
    buckets[i].reset();
  }

  for (int i = 0; i < n; ++i) {
    bucket[auxIndices[i]] = i / bucketSize;
    indexInBucket[auxIndices[i]] = i % bucketSize;
    buckets[i / bucketSize].add(auxIndices[i]);
  }

  for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
    buckets[i].build();
  }
}

int query() {
  int res = 0;

  int reach = -1;

  for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
    if (!buckets[i].isEmpty() && reach < buckets[i].last()) {
      int index = buckets[i].upperBound(reach);
      res += buckets[i].jumps(index);
      reach = buckets[i].furthest(index);
    }
  }

  return res;
}

int update(int index, int x) {
  buckets[bucket[index]].rem(indexInBucket[index]);

  a[index] = x;
  bucket[index] = -1;

  for (int i = 0; i <= (n - 1) / bucketSize && bucket[index] == -1; ++i) {
    if (!buckets[i].isEmpty() && x <= buckets[i].last()) {
      bucket[index] = i;
    }
  }

  if (bucket[index] == -1) {
    bucket[index] = (n - 1) / bucketSize;
  }

  buckets[bucket[index]].ins(index);

  updates += 1;

  if (updates == bucketSize) {
    rebuild();
    updates = 0;
  }

  return query();
}

/*
4 10 5
10
15
17
20
2 16 1
1 25 2
3 35 2
0 38 2
2 0 3

#define MAX_N 1000000
#define MAX_M 1000000

static int N,L,M;
static int X[MAX_N];
static int ii[MAX_M];
static int yy[MAX_M];
static int sol[MAX_M];

inline
void my_assert(int e) {if (!e) abort();}

void read_input()
{
  int i;
  my_assert(3==scanf("%d %d %d",&N,&L,&M));
  for(i=0; i<N; i++)
    my_assert(1==scanf("%d",&X[i]));
  for(i=0; i<M; i++)
    my_assert(3==scanf("%d %d %d",&ii[i],&yy[i],&sol[i]));
}

int main()
{
  int i, ans;

  read_input();
  init(N,L,X);
  for(i=0; i<M; i++) {
    ans = update(ii[i],yy[i]);
    if(ans==sol[i])continue;
      printf("Incorrect.  In %d-th move, answered %d (%d expected).\n",
	     i+1, ans, sol[i]);
    return 0;
  }
  printf("Correct.\n");
  return 0;
}
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 404 ms 1460 KB Output is correct
8 Correct 451 ms 1740 KB Output is correct
9 Correct 697 ms 3068 KB Output is correct
10 Correct 656 ms 3080 KB Output is correct
11 Correct 526 ms 3072 KB Output is correct
12 Correct 1012 ms 3072 KB Output is correct
13 Correct 683 ms 3076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 404 ms 1460 KB Output is correct
8 Correct 451 ms 1740 KB Output is correct
9 Correct 697 ms 3068 KB Output is correct
10 Correct 656 ms 3080 KB Output is correct
11 Correct 526 ms 3072 KB Output is correct
12 Correct 1012 ms 3072 KB Output is correct
13 Correct 683 ms 3076 KB Output is correct
14 Correct 415 ms 1908 KB Output is correct
15 Correct 667 ms 2152 KB Output is correct
16 Correct 1668 ms 3296 KB Output is correct
17 Correct 1914 ms 4160 KB Output is correct
18 Correct 1926 ms 4156 KB Output is correct
19 Correct 1344 ms 4160 KB Output is correct
20 Correct 1847 ms 4156 KB Output is correct
21 Correct 1735 ms 4156 KB Output is correct
22 Correct 1271 ms 4212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 404 ms 1460 KB Output is correct
8 Correct 451 ms 1740 KB Output is correct
9 Correct 697 ms 3068 KB Output is correct
10 Correct 656 ms 3080 KB Output is correct
11 Correct 526 ms 3072 KB Output is correct
12 Correct 1012 ms 3072 KB Output is correct
13 Correct 683 ms 3076 KB Output is correct
14 Correct 415 ms 1908 KB Output is correct
15 Correct 667 ms 2152 KB Output is correct
16 Correct 1668 ms 3296 KB Output is correct
17 Correct 1914 ms 4160 KB Output is correct
18 Correct 1926 ms 4156 KB Output is correct
19 Correct 1344 ms 4160 KB Output is correct
20 Correct 1847 ms 4156 KB Output is correct
21 Correct 1735 ms 4156 KB Output is correct
22 Correct 1271 ms 4212 KB Output is correct
23 Correct 6406 ms 8532 KB Output is correct
24 Correct 6217 ms 13544 KB Output is correct
25 Correct 5729 ms 13644 KB Output is correct
26 Correct 5477 ms 13548 KB Output is correct
27 Correct 5643 ms 13404 KB Output is correct
28 Correct 1557 ms 5196 KB Output is correct
29 Correct 1461 ms 5216 KB Output is correct
30 Correct 1564 ms 5132 KB Output is correct
31 Correct 1508 ms 5224 KB Output is correct
32 Correct 4489 ms 12976 KB Output is correct
33 Correct 3192 ms 12304 KB Output is correct
34 Correct 5717 ms 13260 KB Output is correct
35 Correct 3010 ms 13480 KB Output is correct
36 Correct 1141 ms 12964 KB Output is correct
37 Correct 7210 ms 13376 KB Output is correct
38 Correct 6187 ms 12188 KB Output is correct
39 Correct 6028 ms 13224 KB Output is correct
40 Correct 6643 ms 12220 KB Output is correct
41 Correct 7839 ms 12968 KB Output is correct
42 Correct 7954 ms 13200 KB Output is correct