This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "elephants.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define f first
#define s second
const int N = 1e5 + 100;
const int sq = 500;
const ll mod = 1e9 + 7;
const ll inf = 1e16;
int n, len, q = 0, sz = 0;
//int x[N];
int *x;
vector<int> v[sq], tmp;
int nx[sq][N], dp[sq][N];
map<int, int> cnt;
//*
void init(int N, int L, int X[])
{
n = N;
len = L;
x = X;
for(int i = 0; i < n; i++)
cnt[x[i]]++;
}
//*/
void bld(int id)
{
int m = v[id].size();
nx[id][m-1] = v[id][m-1] + len;
dp[id][m-1] = 1;
int pt = m;
for(int i = m - 1; i >= 0; i--)
{
while(pt > 0 && v[id][pt-1] > v[id][i])
pt--;
if(pt >= m)
{
dp[id][i] = 1;
nx[id][i] = v[id][i] + len;
}
else
{
dp[id][i] = dp[id][pt] + 1;
nx[id][i] = nx[id][pt];
}
}
}
void build()
{
sort(x, x + n);
n = unique(x, x+n) - x;
for(int i = 0; i < sq; i++)
v[i].clear();
sz = 0;
for(int i = 0; i < n; i++)
{
v[sz].push_back(x[i]);
if(v[sz].size() == sq+1)
{
bld(sz);
sz++;
}
}
if(v[sz].size() != 0)
bld(sz++);
//cout << "////////" << endl;
}
void add(int k, bool b)
{
int id = 0;
for(int i = 0; i < sz; i++)
{
if(v[i][0] <= k)
{
id = i;
break;
}
}
//cout << k << ' ' << id << endl;
//for(auto a : v[id])
//cout << a << ' ';
//cout << endl;
if(!b)
{
tmp = v[id];
v[id].clear();
for(auto y : tmp)
{
if(y != k)
v[id].push_back(y);
}
}
else
{
bool fl = false;
tmp = v[id];
v[id].clear();
for(auto y : tmp)
{
if(y > k)
{
v[id].push_back(k);
fl = true;
}
v[id].push_back(y);
}
if(!fl)
v[id].push_back(k);
}
//for(auto y : v[id])
//cout << y << ' ';
//cout << endl;
bld(id);
}
int update(int i, int y)
{
if(q%sq == 0)
build();
q++;
cnt[x[i]]--;
if(cnt[x[i]] == 0)
add(x[i], 0);
x[i] = y;
if(cnt[x[i]] == 0)
add(x[i], 1);
cnt[y]++;
int j = 0, ans = 0, t = -1;
for(int i = 0; i < sq; i++)
{
if(v[i].empty())
break;
j = upper_bound(v[i].begin(), v[i].end(), t) - v[i].begin();
if(j < v[i].size())
{
ans += dp[i][j];
t = nx[i][j];
}
}
return ans;
}
/*
int main()
{
cin >> n >> len;
for(int i = 0; i < n; i++)
{
cin >> x[i];
cnt[x[i]]++;
}
int qu;
cin >> qu;
while(qu--)
{
int a, b;
cin >> a >> b;
cout << update(a, b) << endl;
}
return 0;
}
//*/
Compilation message (stderr)
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:164:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | if(j < v[i].size())
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |