Submission #809003

# Submission time Handle Problem Language Result Execution time Memory
809003 2023-08-05T13:44:21 Z TheSahib Dancing Elephants (IOI11_elephants) C++14
100 / 100
5494 ms 11824 KB
#pragma GCC optimize("O3")

#include "elephants.h"
#include <bits/stdc++.h>

#define pii pair<int, int>

using namespace std;

const int oo = 1e9 + 9;
const int MAX = 150005;
const int BLOCK = 380;


int n, l;
vector<int> st;
int arr[MAX];


struct DATA{
    vector<int> v;
    vector<pii> dp;
    void update(int a, bool b){
        auto itr = lower_bound(v.begin(), v.end(), a);
        if(b){
            v.insert(itr, a);
        }
        else{
            v.erase(itr);
        }
    }
    void calc(){
        dp.resize(v.size());
        int p = v.size() - 1;
        for(int i = v.size() - 1; i >= 0; --i){
            while(v[i] + l < v[p]){
                --p;
            }
            int j = p + 1;
            if(j == v.size()){
                dp[i] = {1, v[i] + l};
            }
            else{
                dp[i] = dp[j];
                dp[i].first += 1;
            }
        }
    }
};

int blockCnt = 0;
DATA blocks[MAX / BLOCK + 1];

void build(){
    vector<int> v(n);
    for (int i = 0; i < n; i++)
    {
        v[i] = arr[i];
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < blockCnt; i++){
        blocks[i].v.clear();
    }
    for(int i = 0; i < n; ++i){
        blocks[i / BLOCK].v.push_back(v[i]);
    }
    for(int i = 0; i < blockCnt; i++){
        sort(blocks[i].v.begin(), blocks[i].v.end());
        blocks[i].calc();
    }
}

int findBlock(int a){
    int l = 0, r = blockCnt - 1;
    int ans = 0;
    while(l <= r){
        int mid = (l + r) / 2;
        int mn = oo;
        if(!blocks[mid].v.empty()){
            mn = blocks[mid].v[0];
        }

        if(a < mn){
            r = mid - 1;
        }
        else{
            ans = mid;
            l = mid + 1;
        }
    }
    return ans;
}

void init(int N, int L, int X[])
{
    n = N;
    l = L;
    blockCnt = (N + BLOCK - 1) / BLOCK;
    for(int i = 0; i < N; ++i){
        arr[i] = X[i];
    }
    build();
}

int q = 0;

int update(int i, int y)
{
    q += 1;
    if(q >= BLOCK - 1){
        arr[i] = y;
        build();
        q = 0;
    }
    else{
        int a = findBlock(arr[i]);
        int b = findBlock(y);
        blocks[a].update(arr[i], 0);
        blocks[b].update(y, 1);
        arr[i] = y;
        blocks[a].calc();
        blocks[b].calc();
    }
    int p = -1;
    int ans = 0;
    for(int i = 0; i < blockCnt; ++i){
        if(blocks[i].v.back() <= p) continue;
        int idx = upper_bound(blocks[i].v.begin(), blocks[i].v.end(), p) - blocks[i].v.begin();
        ans += blocks[i].dp[idx].first;
        p = blocks[i].dp[idx].second;
    }
    return ans;
}

Compilation message

elephants.cpp: In member function 'void DATA::calc()':
elephants.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |             if(j == v.size()){
      |                ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 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 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 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 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 146 ms 1232 KB Output is correct
8 Correct 179 ms 1364 KB Output is correct
9 Correct 295 ms 2364 KB Output is correct
10 Correct 419 ms 2188 KB Output is correct
11 Correct 378 ms 2192 KB Output is correct
12 Correct 730 ms 2584 KB Output is correct
13 Correct 420 ms 2156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 146 ms 1232 KB Output is correct
8 Correct 179 ms 1364 KB Output is correct
9 Correct 295 ms 2364 KB Output is correct
10 Correct 419 ms 2188 KB Output is correct
11 Correct 378 ms 2192 KB Output is correct
12 Correct 730 ms 2584 KB Output is correct
13 Correct 420 ms 2156 KB Output is correct
14 Correct 263 ms 1764 KB Output is correct
15 Correct 305 ms 1912 KB Output is correct
16 Correct 1073 ms 2860 KB Output is correct
17 Correct 1302 ms 3676 KB Output is correct
18 Correct 1340 ms 3668 KB Output is correct
19 Correct 771 ms 3044 KB Output is correct
20 Correct 1309 ms 3668 KB Output is correct
21 Correct 1262 ms 3656 KB Output is correct
22 Correct 776 ms 3012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 146 ms 1232 KB Output is correct
8 Correct 179 ms 1364 KB Output is correct
9 Correct 295 ms 2364 KB Output is correct
10 Correct 419 ms 2188 KB Output is correct
11 Correct 378 ms 2192 KB Output is correct
12 Correct 730 ms 2584 KB Output is correct
13 Correct 420 ms 2156 KB Output is correct
14 Correct 263 ms 1764 KB Output is correct
15 Correct 305 ms 1912 KB Output is correct
16 Correct 1073 ms 2860 KB Output is correct
17 Correct 1302 ms 3676 KB Output is correct
18 Correct 1340 ms 3668 KB Output is correct
19 Correct 771 ms 3044 KB Output is correct
20 Correct 1309 ms 3668 KB Output is correct
21 Correct 1262 ms 3656 KB Output is correct
22 Correct 776 ms 3012 KB Output is correct
23 Correct 2726 ms 6352 KB Output is correct
24 Correct 3065 ms 6740 KB Output is correct
25 Correct 2199 ms 6568 KB Output is correct
26 Correct 3410 ms 6016 KB Output is correct
27 Correct 3773 ms 5964 KB Output is correct
28 Correct 689 ms 2492 KB Output is correct
29 Correct 653 ms 2376 KB Output is correct
30 Correct 693 ms 2376 KB Output is correct
31 Correct 654 ms 2372 KB Output is correct
32 Correct 3163 ms 5976 KB Output is correct
33 Correct 3138 ms 6056 KB Output is correct
34 Correct 3428 ms 5976 KB Output is correct
35 Correct 2416 ms 6080 KB Output is correct
36 Correct 1031 ms 5960 KB Output is correct
37 Correct 5309 ms 7864 KB Output is correct
38 Correct 3623 ms 9528 KB Output is correct
39 Correct 3387 ms 10552 KB Output is correct
40 Correct 3568 ms 9548 KB Output is correct
41 Correct 5411 ms 11648 KB Output is correct
42 Correct 5494 ms 11824 KB Output is correct