답안 #724998

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
724998 2023-04-16T12:24:11 Z finn__ 코끼리 (Dancing Elephants) (IOI11_elephants) C++17
100 / 100
3486 ms 12460 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")

#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;

constexpr size_t MAX_N = 151000, B = 387;
int l;

struct Bucket
{
    size_t n;
    int x[2 * B], k[2 * B], o[2 * B];

    void build(size_t m) // Recalculate the first m elephants.
    {
        size_t i = m - 1, j = upper_bound(x, x + n, x[i] + l) - x;
        while (i < m)
        {
            while (j > i + 1 && x[i] + l < x[j - 1])
                --j;
            if (j == n)
                k[i] = 1, o[i] = x[i] + l;
            else
                k[i] = k[j] + 1, o[i] = o[j];
            --i;
        }
    }

    void insert(int y)
    {
        x[n] = y;
        size_t i = n;
        while (i && x[i] < x[i - 1])
            swap(x[i], x[i - 1]), swap(k[i], k[i - 1]), swap(o[i], o[i - 1]), --i;
        ++n;
        build(i + 1);
    }

    void erase(size_t i)
    {
        for (size_t j = i; j < n - 1; ++j)
            swap(x[j], x[j + 1]), swap(k[j], k[j + 1]), swap(o[j], o[j + 1]);
        --n;
        if (i)
            build(i);
    }
};

size_t iteration_count = 0;
Bucket b[MAX_N / B];
int u[MAX_N], v[MAX_N];

void init(int N, int L, int X[])
{
    l = L;
    for (size_t i = 0; i <= N / B; ++i)
    {
        for (size_t j = 0; j < B && i * B + j < N; ++j)
            b[i].x[j] = X[i * B + j], ++b[i].n;
        b[i].build(b[i].n);
    }
    memcpy(u, X, N * sizeof *X);
}

int update(int i, int y)
{
    if (iteration_count == B - 1)
    {
        size_t k = 0;
        for (size_t i = 0; i < MAX_N / B; ++i)
        {
            for (size_t j = 0; j < b[i].n; ++j)
                v[k++] = b[i].x[j];
            b[i].n = 0;
        }
        for (size_t i = 0; i <= k / B; ++i)
        {
            for (size_t j = 0; j < B && i * B + j < k; ++j)
                b[i].x[j] = v[i * B + j], ++b[i].n;
            b[i].build(b[i].n);
        }
        iteration_count = 0;
    }

    ++iteration_count;
    for (size_t j = 0;; ++j)
        if (b[j].x[0] <= u[i] && u[i] <= b[j].x[b[j].n - 1])
        {
            b[j].erase(lower_bound(b[j].x, b[j].x + b[j].n, u[i]) - b[j].x);
            break;
        }
    u[i] = y;
    for (size_t j = 0;; ++j)
        if ((!j || b[j].x[0] <= y) && (!b[j + 1].n || y <= b[j + 1].x[0]))
        {
            b[j].insert(y);
            break;
        }

    int covered = -1, cameras = 0;
    for (size_t j = 0; b[j].n; ++j)
        if (b[j].x[b[j].n - 1] > covered)
        {
            size_t const k = upper_bound(b[j].x, b[j].x + b[j].n, covered) - b[j].x;
            covered = b[j].o[k];
            cameras += b[j].k[k];
        }
    return cameras;
}

Compilation message

elephants.cpp: In function 'void init(int, int, int*)':
elephants.cpp:60:47: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         for (size_t j = 0; j < B && i * B + j < N; ++j)
      |                                     ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 308 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 308 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 478 ms 2392 KB Output is correct
8 Correct 483 ms 2556 KB Output is correct
9 Correct 555 ms 4244 KB Output is correct
10 Correct 397 ms 4088 KB Output is correct
11 Correct 382 ms 3972 KB Output is correct
12 Correct 695 ms 4120 KB Output is correct
13 Correct 436 ms 3824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 308 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 478 ms 2392 KB Output is correct
8 Correct 483 ms 2556 KB Output is correct
9 Correct 555 ms 4244 KB Output is correct
10 Correct 397 ms 4088 KB Output is correct
11 Correct 382 ms 3972 KB Output is correct
12 Correct 695 ms 4120 KB Output is correct
13 Correct 436 ms 3824 KB Output is correct
14 Correct 575 ms 3280 KB Output is correct
15 Correct 776 ms 3484 KB Output is correct
16 Correct 1061 ms 4728 KB Output is correct
17 Correct 1095 ms 5652 KB Output is correct
18 Correct 1235 ms 5588 KB Output is correct
19 Correct 1120 ms 5784 KB Output is correct
20 Correct 1136 ms 5664 KB Output is correct
21 Correct 1148 ms 5620 KB Output is correct
22 Correct 710 ms 5176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 308 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 478 ms 2392 KB Output is correct
8 Correct 483 ms 2556 KB Output is correct
9 Correct 555 ms 4244 KB Output is correct
10 Correct 397 ms 4088 KB Output is correct
11 Correct 382 ms 3972 KB Output is correct
12 Correct 695 ms 4120 KB Output is correct
13 Correct 436 ms 3824 KB Output is correct
14 Correct 575 ms 3280 KB Output is correct
15 Correct 776 ms 3484 KB Output is correct
16 Correct 1061 ms 4728 KB Output is correct
17 Correct 1095 ms 5652 KB Output is correct
18 Correct 1235 ms 5588 KB Output is correct
19 Correct 1120 ms 5784 KB Output is correct
20 Correct 1136 ms 5664 KB Output is correct
21 Correct 1148 ms 5620 KB Output is correct
22 Correct 710 ms 5176 KB Output is correct
23 Correct 2943 ms 12404 KB Output is correct
24 Correct 3225 ms 12460 KB Output is correct
25 Correct 2549 ms 12408 KB Output is correct
26 Correct 2822 ms 12408 KB Output is correct
27 Correct 3486 ms 12252 KB Output is correct
28 Correct 1699 ms 5180 KB Output is correct
29 Correct 1664 ms 5180 KB Output is correct
30 Correct 1703 ms 5192 KB Output is correct
31 Correct 1711 ms 5184 KB Output is correct
32 Correct 2136 ms 11832 KB Output is correct
33 Correct 1707 ms 11160 KB Output is correct
34 Correct 2205 ms 12040 KB Output is correct
35 Correct 1677 ms 12340 KB Output is correct
36 Correct 1710 ms 11816 KB Output is correct
37 Correct 2969 ms 12232 KB Output is correct
38 Correct 2456 ms 11120 KB Output is correct
39 Correct 3121 ms 12080 KB Output is correct
40 Correct 2474 ms 11072 KB Output is correct
41 Correct 3320 ms 11824 KB Output is correct
42 Correct 3414 ms 12060 KB Output is correct