Submission #764452

# Submission time Handle Problem Language Result Execution time Memory
764452 2023-06-23T12:17:25 Z boris_mihov Dancing Elephants (IOI11_elephants) C++17
50 / 100
9000 ms 7604 KB
#include "elephants.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>
#include <set>

typedef long long llong;
const int MAXNUM = 1000000000;
const int BUCKET_SIZE = 2000;
const int MAXN = 150000 + 10;
const int INF  = 2e9;

int n, len;
int a[MAXN];
int x[MAXN];
std::vector <int> nums[BUCKET_SIZE];
std::vector <std::pair <int,int>> dp[BUCKET_SIZE];

void fix(int idx)
{
    dp[idx].resize(nums[idx].size());
    int ptr = nums[idx].size();

    for (int i = (int)nums[idx].size() - 1 ; i >= 0 ; --i)
    {
        while (ptr > 0 && nums[idx][i] + len < nums[idx][ptr - 1])
        {
            ptr--;
        }

        if (ptr == nums[idx].size())
        {
            dp[idx][i] = {1, nums[idx][i] + len};
        } else
        {
            dp[idx][i] = dp[idx][ptr];
            dp[idx][i].first++;
        }
    }
}

void erase(int idx, int val)
{
    for (int i = 0 ; i < nums[idx].size() ; ++i)
    {
        if (nums[idx][i] == val)
        {
            nums[idx].erase(nums[idx].begin() + i);
            break;
        }
    }

    fix(idx);
}

void insert(int idx, int val)
{
    bool inserted = false;
    for (int i = 0 ; i < nums[idx].size() ; ++i)
    {   
        if (val < nums[idx][i])
        {
            nums[idx].insert(nums[idx].begin() + i, val);
            inserted = true;
            break;
        }
    }   

    if (!inserted)
    {
        nums[idx].push_back(val);
    }

    fix(idx);
}

std::multiset <int> ms;
void init(int N, int L, int X[])
{
    n = N;
    len = L;

    for (int i = 1 ; i <= n ; ++i)
    {
        x[i] = a[i] = X[i - 1];
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        nums[i / BUCKET_SIZE].push_back(a[i]);
    }

    for (int i = 0 ; i < BUCKET_SIZE ; ++i)
    {
        std::sort(nums[i].begin(), nums[i].end());
        fix(i);
    }
}

void rebuild()
{
    for (int i = 1 ; i <= n ; ++i)
    {
        a[i] = x[i];
    }

    for (int i = 0 ; i < BUCKET_SIZE ; ++i)
    {   
        nums[i].clear();
    }

    std::sort(a + 1, a + 1 + n);
    for (int i = 1 ; i <= n ; ++i)
    {
        nums[i / BUCKET_SIZE].push_back(a[i]);
    }

    for (int i = 0 ; i < BUCKET_SIZE ; ++i)
    {
        std::sort(nums[i].begin(), nums[i].end());
        fix(i);
    }
}

int update(int idx, int y)
{
    for (int i = 0 ; i < BUCKET_SIZE ; ++i)
    {
        if (nums[i].size() > 5 * BUCKET_SIZE)
        {
            rebuild();
            break;
        }
    }

    idx++;
    for (int i = 0 ; i < BUCKET_SIZE ; ++i)
    {
        if (nums[i].empty())
        {
            continue;
        }

        if (nums[i][0] <= x[idx] && x[idx] <= nums[i].back())
        {
            erase(i, x[idx]);
            break;
        }
    }

    x[idx] = y;
    bool inserted = false;
    int lastBucket = -1;

    for (int i = 0 ; i < BUCKET_SIZE ; ++i)
    {
        if (nums[i].empty())
        {
            continue;
        }

        lastBucket = i;
        if (nums[i][0] <= x[idx] && x[idx] <= nums[i].back())
        {
            inserted = true;
            insert(i, x[idx]);
            break;
        }

        if (nums[i][0] > x[idx])
        {
            inserted = true;
            insert(i, x[idx]);
        }
    }

    if (!inserted)
    {
        insert(lastBucket, x[idx]);
    }

    int res = 0;
    int coveredTo = -1;
    for (int bucket = 0 ; bucket <= 100 ; ++bucket)
    {
        if (nums[bucket].empty())
        {
            continue;
        }

        if (coveredTo >= nums[bucket].back())
        {
            continue;
        }
        
        int l = -1, r = nums[bucket].size() - 1, mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (nums[bucket][mid] <= coveredTo) l = mid;
            else r = mid;
        }

        res += dp[bucket][r].first;
        coveredTo = dp[bucket][r].second;
    }

    return res;
}

Compilation message

elephants.cpp: In function 'void fix(int)':
elephants.cpp:34:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         if (ptr == nums[idx].size())
      |             ~~~~^~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'void erase(int, int)':
elephants.cpp:47:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for (int i = 0 ; i < nums[idx].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'void insert(int, int)':
elephants.cpp:62:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0 ; i < nums[idx].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~~~
# 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 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 1 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 1 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 2908 ms 2064 KB Output is correct
8 Correct 3936 ms 2244 KB Output is correct
9 Correct 585 ms 2488 KB Output is correct
10 Correct 869 ms 2600 KB Output is correct
11 Correct 873 ms 2324 KB Output is correct
12 Correct 1412 ms 2708 KB Output is correct
13 Correct 867 ms 2508 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 1 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 2908 ms 2064 KB Output is correct
8 Correct 3936 ms 2244 KB Output is correct
9 Correct 585 ms 2488 KB Output is correct
10 Correct 869 ms 2600 KB Output is correct
11 Correct 873 ms 2324 KB Output is correct
12 Correct 1412 ms 2708 KB Output is correct
13 Correct 867 ms 2508 KB Output is correct
14 Correct 1452 ms 2572 KB Output is correct
15 Correct 914 ms 1920 KB Output is correct
16 Correct 2360 ms 2840 KB Output is correct
17 Correct 2097 ms 3604 KB Output is correct
18 Correct 2436 ms 3720 KB Output is correct
19 Execution timed out 9042 ms 7604 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 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 1 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 2908 ms 2064 KB Output is correct
8 Correct 3936 ms 2244 KB Output is correct
9 Correct 585 ms 2488 KB Output is correct
10 Correct 869 ms 2600 KB Output is correct
11 Correct 873 ms 2324 KB Output is correct
12 Correct 1412 ms 2708 KB Output is correct
13 Correct 867 ms 2508 KB Output is correct
14 Correct 1452 ms 2572 KB Output is correct
15 Correct 914 ms 1920 KB Output is correct
16 Correct 2360 ms 2840 KB Output is correct
17 Correct 2097 ms 3604 KB Output is correct
18 Correct 2436 ms 3720 KB Output is correct
19 Execution timed out 9042 ms 7604 KB Time limit exceeded
20 Halted 0 ms 0 KB -