#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
pair<int,int> sgt[800001];
int mod[800001];
int h[200001];
int n,k;
int cn;
void push(int v){
sgt[v<<1].first+=mod[v];
mod[v<<1]+=mod[v];
sgt[v<<1|1].first+=mod[v];
mod[v<<1|1]+=mod[v];
mod[v] = 0;
}
void update(int v,int l,int r,int tl,int tr,int val){
if(r < tl or tr < l) return;
if(tl <= l and r <= tr){
sgt[v].first+=val;
mod[v]+=val;
return;
}
push(v);
int m = (l+r)>>1;
update(v<<1,l,m,tl,tr,val);
update(v<<1|1,m+1,r,tl,tr,val);
sgt[v] = min(sgt[v<<1],sgt[v<<1|1]);
}
void update(int v,int l,int r,int tl,int tr,pair<int,int> val){
if(r < tl or tr < l) return;
if(tl <= l and r <= tr){
sgt[v]=val;
mod[v]=val.first;
return;
}
push(v);
int m = (l+r)>>1;
update(v<<1,l,m,tl,tr,val);
update(v<<1|1,m+1,r,tl,tr,val);
sgt[v] = min(sgt[v<<1],sgt[v<<1|1]);
}
pair<int,int> get(int v,int l,int r,int tl,int tr){
if(r < tl or tr < l) return {1e9,1e9};
if(tl <= l and r <= tr) return sgt[v];
push(v);
int m = (l+r)>>1;
return min(get(v<<1,l,m,tl,tr),get(v<<1|1,m+1,r,tl,tr));
}
void update(int l,int r,int val){
if(l <= r){
update(1,0,n-1,l,r,val);
}
else{
update(1,0,n-1,0,r,val);
update(1,0,n-1,l,n-1,val);
}
}
pair<int,int> get(int l,int r){
if(l <= r) return get(1,0,n-1,l,r);
return min(get(1,0,n-1,0,r),get(1,0,n-1,l,n-1));
}
void extarct(int x){
//cout << "extarct " << x << "\n";
pair<int,int> back = get((n+x-k+1)%n,x-1);
while(back.first == 0){
extarct(back.second);
//cout << back.second << "\n";
back = get((n+x-k+1)%n,x-1);
}
update(x,x,1e9);
update((n+x-k+1)%n,x-1,-1);
h[x] = cn--;
}
vector<int> G[200000];
vector<int> Gtr[200000];
pair<int,int> sgt_mx[800001];
void update_mx(int v,int l,int r,int t,pair<int,int> val){
if(r < t or t < l) return;
if(l == r){
sgt_mx[v] = val;
return;
}
int m = (l+r)>>1;
update_mx(v<<1,l,m,t,val);
update_mx(v<<1|1,m+1,r,t,val);
sgt_mx[v] = max(sgt_mx[v<<1],sgt_mx[v<<1|1]);
}
pair<int,int> get_mx(int v,int l,int r,int tl,int tr){
if(tr < l or r < tl) return {-1e9,-1e9};
if(tl <= l and r <= tr) return sgt_mx[v];
int m = (l+r)>>1;
return max(get_mx(v<<1,l,m,tl,tr),get_mx(v<<1|1,m+1,r,tl,tr));
}
bool pos_to_0[200000];
bool pos_from_0[200000];
int used[200000];
bool dfs_to_0(int v){
if(v < 0) return 0;
if(used[v] == 2) return pos_to_0[v];
used[v] = 2;
for(auto e:G[v]){
if(dfs_to_0(e)){
pos_to_0[v] = 1;
return 1;
}
}
pos_to_0[v] = 0;
return 0;
}
void dfs_from_0(int v){
if(v < 0) return;
if(used[v] == 1) return;
used[v] = 1;
pos_from_0[v] = 1;
for(auto e:G[v]){
dfs_from_0(e);
}
}
void init(int k_, vector<int> r) {
k = k_;
n = r.size();
for (int i = 0; i < n; ++i)
{
update(1,0,n-1,i,i,{r[i],i});
}
cn = n;
pair<int,int> x = get(0,n-1);
while(x.first == 0){
extarct(x.second);
x = get(0,n-1);
}
for (int i = 0; i < n; ++i)
{
G[i].resize(2);
}
int tr = 0;
for (int i = 0; i < n; ++i)
{
update_mx(1,1,n,i,{-1e9,-1e9});
}
for (int l = 0;l < n; ++l)
{
while(abs((n+tr-l)%n) < k-1){
//cout << l << " " << tr << "\n";
update_mx(1,1,n,tr,{h[tr],tr}),tr++,tr%=n;
}
G[tr][0] = get_mx(1,1,n,1,h[tr]-1).second;
int id = (n+l-1)%n;
G[id][1] = get_mx(1,1,n,1,h[id]-1).second;
//cout << l << " " << tr << " -> " << tr << " " << id << "\n";
update_mx(1,1,n,l,{-1e9,-1e9});
}
// for (int i = 0; i < n; ++i)
// {
//cout << G[i][0] << " " << G[i][1] << "\n";
// }
//return;
dfs_from_0(0);
for (int i = 0; i < n; ++i)
{
for(auto e:G[i]){
//cout << i << " - " << e << "\n";
if(e >= 0) Gtr[e].push_back(i);
}
}
for (int i = 0; i < n; ++i)
{
swap(Gtr[i],G[i]);
}
dfs_to_0(0);
}
int compare_plants(int x, int y) {
if(pos_from_0[y]) return 1;
if(pos_to_0[y]) return -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9716 KB |
Output is correct |
3 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9720 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Incorrect |
5 ms |
9736 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9720 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Incorrect |
5 ms |
9736 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9712 KB |
Output is correct |
3 |
Incorrect |
5 ms |
9688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9716 KB |
Output is correct |
3 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |