이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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]; bool del[maxn];
pi mxdepth[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;
mem(st, -1);
DEC(i,N,1) if (spec[i] && st[i] == -1){
depth[i] = 1; dfs(i);
}
DEC(i,N,1){
mxdepth[i] = pi(depth[i], i);
aFOR(j,adj[i]) mxdepth[i] = max(mxdepth[i], mxdepth[j]);
}
set <pi> roots;
FOR(i,1,N) if (nxt[i] > N && spec[i]) roots.insert(pi(mxdepth[i].f, i));
FOR(i,1,D){
if (roots.empty()) break;
int optRoot = roots.rbegin()->s; roots.erase(--roots.end());
int opt = mxdepth[optRoot].s;
ans -= mxdepth[optRoot].f;
for (int x = opt; x <= N && !del[x]; x = nxt[x]){
del[x] = 1;
aFOR(j, adj[x]) if (!del[j]) roots.insert(pi(mxdepth[j].f, j));
}
}
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... |