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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
//~ #define ll int
#define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 2000001
#define INF 1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int N,D,T,A[maxn],L[maxn];
int pre[maxn]; // closest which covers it
bool spec[maxn];
vi adj[maxn];
struct node{
int s,e,m,lazy=0;
pi val = pi(0,-1);
node *l,*r;
node (int ss,int ee){
s = ss; e = ee; m = (s + e) / 2;
val.s = s;
if (s != e){
l = new node(s,m);
r = new node(m+1,e);
}
}
void prop(){
if (s == e || lazy == 0) return;
l->val.f += lazy; r->val.f += lazy;
l->lazy += lazy; r->lazy += lazy;
lazy = 0;
}
pi qry(int a,int b){
prop();
if (a <= s && e <= b) return val;
else if (a > e || s > b) return pi(-1,-1);
return max(l->qry(a,b), r->qry(a,b));
}
void upd(int a,int b,int c){
prop();
if (a <= s && e <= b){
val.f += c;
lazy += c;
}else if (a > e || s > b) return;
else{
l->upd(a,b,c); r->upd(a,b,c);
val = max(l->val, r->val);
}
}
}*root;
int st[maxn], en[maxn],unst[maxn],depth[maxn], co = 1;
void dfs(int x){
unst[co] = x;
st[x] = co++;
aFOR(i,adj[x]){
depth[i] = depth[x] + 1; dfs(i);
}
en[x] = co - 1;
}
int nxt[maxn];
int32_t main(){
fast;
cin >> N >> D >> T;
FOR(i,1,N) cin >> A[i];
stack <pi> sta;
// j covers i if A[j] + i - j <= T
// A[j] + j <= T - i
// increasing stack
FOR(i,1,N){
while (!sta.empty() && sta.top().f >= A[i] - i) sta.pop();
sta.push(pi(A[i] - i, i));
while (!sta.empty() && sta.top().f > T - i) sta.pop();
if (!sta.empty()) pre[i] = sta.top().s;
}
int mn = INF;
int ans = 0; //number which will rebel (covered)
FOR(i,1,N){
mn = min(mn, A[i] - i);
if (A[i] > T && mn <= T - i) spec[i] = 1;
if (mn <= T - i) ans++; // will rebel if no barriers
}
int r = 1;
FOR(i,1,N) if (spec[i]){
r = max(r, i+1);
while (r <= N && (!spec[r] || pre[r] > i)) r++;
nxt[i] = r;
if (r <= N){ // r is parent
adj[r].pb(i);
//~ cout << r << ' ' << i << '\n';
}
}
//~ cout << spec[3] << ' ' << nxt[3] << '\n';
//~ return 0;
FOR(i,1,N) if (spec[i] && !st[i]) dfs(i);
root = new node(1,N);
FOR(i,1,N) root->upd(st[i], st[i], depth[i] + 1);
FOR(i,1,D){
if (root->val.s == -1) break;
int opt = unst[root->val.s];
//~ cout
//~ break;
assert(spec[opt]);
ans -= root->val.f;
for (int x = opt; x <= N; x = nxt[x]){
//~ cout << x << ' ';
root->upd(st[x], en[x], -1);
root->upd(st[x], st[x], -1);
}
}
cout << ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |