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 "teams.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segment
{
int l, r;
segment(int _l = 0, int _r = 0)
{
l = _l;
r = _r;
}
};
const int maxn = 5e5 + 10;
int n;
segment s[maxn];
void init(int N, int A[], int B[])
{
n = N;
for (int i = 1; i <= n; i ++)
{
s[i] = segment(A[i - 1], B[i - 1]);
}
}
int zeta(int a, int b)
{
int cnt = 0;
for (int i = 1; i <= n; i ++)
{
if (s[i].l <= a && s[i].r >= a)
continue;
if (s[i].l <= b && s[i].r >= b)
cnt ++;
}
return cnt;
}
int pivot_change(int a, int b, int diff)
{
/// dp_a + z(a, i) > dp_b + z(b, i)
/// dp_b - dp_a < z(a, i) - z(b, i)
/// diff < ....
int lf = b, rf = n;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (diff < zeta(a, mf) - zeta(b, mf))
lf = mf + 1;
else
rf = mf - 1;
}
return lf;
}
int dp[maxn];
int can(int M, int K[])
{
sort(K, K + M);
set < int > act;
set < pair < int, int > > event;
for (int i = 0; i < M; i ++)
{
while(!event.empty() && (*event.begin()).first <= K[i])
{
set < int > :: iterator it = act.find((*event.begin()).second);
event.erase({pivot_change(K[*prev(it)], K[*it], dp[*it] - dp[*prev(it)]), *it});
if (next(it) != act.end())
{
event.erase({pivot_change(K[*it], K[*next(it)], dp[*next(it)] - dp[*it]), *next(it)});
event.insert({pivot_change(K[*prev(it)], K[*next(it)], dp[*next(it)] - dp[*prev(it)]), *next(it)});
}
act.erase(it);
}
dp[i] = zeta(0, K[i]) - K[i];
if (!act.empty())
{
int bk = *act.rbegin();
dp[i] = min(dp[i], dp[bk] + zeta(K[bk], K[i]) - K[i]);
}
/**for (int j : act)
{
dp[i] = min(dp[i], dp[j] + zeta(K[j], K[i]) - K[i]);
}*/
if (dp[i] < 0)
return 0;
if (!act.empty())
{
int bk = *act.rbegin();
event.insert({pivot_change(K[bk], K[i], dp[i] - dp[bk]), i});
}
act.insert(i);
}
return 1;
}
# | 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... |