#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,_n) for(int i=0;i<_n;++i)
#define all(x) x.begin(),x.end()
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
using ll = long long;
const int N=2e5+55;
int p[N], L[N], R[N];
int n;
int k;
struct sgt {
vector<pi> t;
vector<int> lazy;
int sz=1;
pi merge(pi a, pi b) {
if (a.f > b.f) return a;
return b;
}
sgt(int n) {
while (sz<n) sz<<=1;
t.assign(2*sz,{0,n});
forn(i,n) t[sz-1+i]={0,i};
lazy.assign(4*sz,0);
}
void push(int v) {
t[v].f+=lazy[v];
lazy[2*v+1]+=lazy[v];
lazy[2*v+2]+=lazy[v];
lazy[v]=0;
}
void add(int v, int l, int r, int lx, int rx, int x) {
if (lazy[v]) push(v);
if (rx<=l || r<=lx) return;
if (lx<=l && r<=rx) {
lazy[v]+=x;
push(v);
return;
}
int m=(l+r)>>1;
add(2*v+1,l,m,lx,rx,x);
add(2*v+2,m,r,lx,rx,x);
t[v]=merge(t[2*v+1],t[2*v+2]);
}
void add(int l, int r, int x) {
add(0,0,sz,l,r,x);
}
pi query(int v, int l, int r, int lx, int rx) {
if (lazy[v]) push(v);
if (rx<=l || r<=lx) return {-2e9,n};
if (lx<=l && r<=rx) return t[v];
int m=(l+r)>>1;
auto lq=query(2*v+1,l,m,lx,rx);
auto rq=query(2*v+2,m,r,lx,rx);
t[v]=merge(t[2*v+1],t[2*v+2]);
return merge(lq,rq);
}
pi query(int l, int r) {
return query(0,0,sz,l,r);
}
};
sgt st(N);
vector<int> adj[305];
int vis[305];
bitset<305> bs[305];
void dfs(int u) {
if (vis[u]==1) assert(0);
if (vis[u]) return;
bs[u][u]=1;
vis[u]=1;
for(auto&v:adj[u]) {
dfs(v);
bs[u]|=bs[v];
}
vis[u]=2;
}
void init(int _k, vector<int> r) {
k=_k;
n=r.size();
if (n<=300) {
forn(i,n) r[i]=k-1-r[i];
forn(i,n) r.pb(r[i]);
forn(i,n) r.pb(r[i]);
forn(it,n) {
for (int i=n; i<2*n; ++i) {
if (r[i]==k-1) {
int z=i;
for (int j=i; j>z-k; --j) if (r[j]==k-1) z=j;
for (int j=z+1; j<z+k; ++j) {
if (vis[j%n]) adj[j%n].pb(z%n);
else adj[z%n].pb(j%n);
}
vis[z%n]=1;
r[z%n]=-1e9;
for (int j=z; j>z-k; --j) {
r[(j%n)]++;
r[(j%n)+n]++;
r[(j%n)+2*n]++;
}
break;
}
}
}
forn(i,n) vis[i]=0;
forn(i,n) dfs(i);
return;
}
if (k==2) {
int z=0;
forn(i,n) r[i]^=1;
forn(i,n) if (r[i]==0) z=i;
for (int i=z+n; i>z; --i) {
if (r[i%n]) R[i%n]=R[(i+1)%n];
else R[i%n]=i%n;
}
z=0;
forn(i,n) if (r[i]) z=i;
++z;
for (int i=z; i<z+n; ++i) {
if (r[(i-1+n)%n]) L[i%n]=i%n;
else L[i%n]=L[(i-1+n)%n];
}
forn(i,n) {
L[i]+=n, R[i]+=n;
}
forn(i,n) {
if (L[i]==R[i] && L[i]==i+n) continue;
if (L[i]>=R[i]) L[i]-=n;
}
return;
}
forn(i,n) r.pb(r[i]);
forn(i,n) r.pb(r[i]);
sgt st(n);
forn(i,n) st.add(i,i+1,r[i]);
int z;
for (int i=n; i<2*n; ++i) {
if (r[i]==k-1) {
z=i;
for (int j=i; j>z-k; --j) if (r[j]==k-1) z=j;
break;
}
}
set<int> s;
set<int> ok;
int ri=-1;
for (int l=z; l<z+n; ++l) {
if (r[l]==k-1) {
s.insert(l%n);
s.insert(l%n+n);
s.insert(l%n+2*n);
if (l>=ri) {
ok.insert(l%n);
}
ri=l+k;
}
}
int nxt=1;
forn(it,n) {
auto x=*ok.begin(); ok.erase(ok.begin());
p[x]=nxt++;
s.erase(x);
s.erase(x+n);
s.erase(x+2*n);
st.add(x,x+1,-1e9);
int j=x;
if (s.size()) {
auto it1=s.lower_bound(x);
j=*it1;
}
j%=n;
if (x-k+1>=0) {
st.add(x-k+1,x,1);
} else {
st.add(0,x,1);
st.add(x-k+1+n,n,1);
}
vector<int> v;
while(1) {
pi q;
if (x-k+1>=0) {
q=st.query(x-k+1,x);
} else {
auto q1=st.query(0,x);
auto q2=st.query(x-k+1+n,n);
q=st.merge(q1,q2);
}
if (q.f<k-1) break;
if (q.s > j) {
j+=n;
}
if (j-q.s>=k) {
ok.insert(j%n);
}
j=q.s;
s.insert(j);
s.insert(j+n);
s.insert(j+2*n);
st.add(j,j+1,-1e9);
v.pb(j);
}
if (!s.size()) break;
for(auto&x:v) st.add(x,x+1,1e9);
auto it2 = s.find(j+n);
--it2;
int t = *it2;
if (j+n-t>=k) ok.insert(j);
}
}
int compare_plants(int x, int y) {
if (n<=300) {
if (bs[x][y]) {
return 1;
}
if (bs[y][x]) {
return -1;
}
return 0;
}
if (k==2) {
if (L[x]<=y && y<=R[x]) return 1;
if (L[x]<=y+n && y+n<=R[x]) return 1;
swap(x,y);
if (L[x]<=y && y<=R[x]) return -1;
if (L[x]<=y+n && y+n<=R[x]) return -1;
return 0;
}
if (p[x]>p[y]) return 1;
else return -1;
}
Compilation message
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:161:19: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
161 | for (int l=z; l<z+n; ++l) {
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
40 ms |
11284 KB |
Output is correct |
7 |
Correct |
46 ms |
11608 KB |
Output is correct |
8 |
Correct |
62 ms |
13988 KB |
Output is correct |
9 |
Correct |
66 ms |
14028 KB |
Output is correct |
10 |
Correct |
62 ms |
14004 KB |
Output is correct |
11 |
Correct |
61 ms |
14084 KB |
Output is correct |
12 |
Correct |
60 ms |
13988 KB |
Output is correct |
13 |
Correct |
59 ms |
14024 KB |
Output is correct |
14 |
Correct |
65 ms |
14116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
3 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
7 ms |
8660 KB |
Output is correct |
7 |
Correct |
54 ms |
11528 KB |
Output is correct |
8 |
Correct |
5 ms |
8672 KB |
Output is correct |
9 |
Correct |
7 ms |
8660 KB |
Output is correct |
10 |
Correct |
54 ms |
11460 KB |
Output is correct |
11 |
Correct |
48 ms |
11332 KB |
Output is correct |
12 |
Correct |
50 ms |
11892 KB |
Output is correct |
13 |
Correct |
53 ms |
11460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
3 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
7 ms |
8660 KB |
Output is correct |
7 |
Correct |
54 ms |
11528 KB |
Output is correct |
8 |
Correct |
5 ms |
8672 KB |
Output is correct |
9 |
Correct |
7 ms |
8660 KB |
Output is correct |
10 |
Correct |
54 ms |
11460 KB |
Output is correct |
11 |
Correct |
48 ms |
11332 KB |
Output is correct |
12 |
Correct |
50 ms |
11892 KB |
Output is correct |
13 |
Correct |
53 ms |
11460 KB |
Output is correct |
14 |
Correct |
98 ms |
12400 KB |
Output is correct |
15 |
Correct |
713 ms |
23020 KB |
Output is correct |
16 |
Correct |
97 ms |
12388 KB |
Output is correct |
17 |
Correct |
707 ms |
23004 KB |
Output is correct |
18 |
Correct |
473 ms |
23012 KB |
Output is correct |
19 |
Correct |
599 ms |
37104 KB |
Output is correct |
20 |
Correct |
693 ms |
23020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8532 KB |
Output is correct |
2 |
Correct |
3 ms |
8532 KB |
Output is correct |
3 |
Correct |
44 ms |
11512 KB |
Output is correct |
4 |
Correct |
447 ms |
32404 KB |
Output is correct |
5 |
Correct |
533 ms |
25220 KB |
Output is correct |
6 |
Correct |
583 ms |
23200 KB |
Output is correct |
7 |
Correct |
645 ms |
22944 KB |
Output is correct |
8 |
Correct |
728 ms |
23072 KB |
Output is correct |
9 |
Correct |
460 ms |
23964 KB |
Output is correct |
10 |
Correct |
492 ms |
26052 KB |
Output is correct |
11 |
Correct |
60 ms |
13960 KB |
Output is correct |
12 |
Correct |
473 ms |
51168 KB |
Output is correct |
13 |
Correct |
460 ms |
22980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
3 ms |
8532 KB |
Output is correct |
5 |
Correct |
3 ms |
8532 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
12 ms |
9172 KB |
Output is correct |
8 |
Correct |
13 ms |
9344 KB |
Output is correct |
9 |
Correct |
12 ms |
9172 KB |
Output is correct |
10 |
Correct |
17 ms |
9316 KB |
Output is correct |
11 |
Correct |
12 ms |
9172 KB |
Output is correct |
12 |
Correct |
16 ms |
9208 KB |
Output is correct |
13 |
Correct |
15 ms |
9620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
3 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Incorrect |
6 ms |
8532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
40 ms |
11284 KB |
Output is correct |
7 |
Correct |
46 ms |
11608 KB |
Output is correct |
8 |
Correct |
62 ms |
13988 KB |
Output is correct |
9 |
Correct |
66 ms |
14028 KB |
Output is correct |
10 |
Correct |
62 ms |
14004 KB |
Output is correct |
11 |
Correct |
61 ms |
14084 KB |
Output is correct |
12 |
Correct |
60 ms |
13988 KB |
Output is correct |
13 |
Correct |
59 ms |
14024 KB |
Output is correct |
14 |
Correct |
65 ms |
14116 KB |
Output is correct |
15 |
Correct |
4 ms |
8532 KB |
Output is correct |
16 |
Correct |
4 ms |
8532 KB |
Output is correct |
17 |
Correct |
4 ms |
8532 KB |
Output is correct |
18 |
Correct |
3 ms |
8532 KB |
Output is correct |
19 |
Correct |
4 ms |
8532 KB |
Output is correct |
20 |
Correct |
7 ms |
8660 KB |
Output is correct |
21 |
Correct |
54 ms |
11528 KB |
Output is correct |
22 |
Correct |
5 ms |
8672 KB |
Output is correct |
23 |
Correct |
7 ms |
8660 KB |
Output is correct |
24 |
Correct |
54 ms |
11460 KB |
Output is correct |
25 |
Correct |
48 ms |
11332 KB |
Output is correct |
26 |
Correct |
50 ms |
11892 KB |
Output is correct |
27 |
Correct |
53 ms |
11460 KB |
Output is correct |
28 |
Correct |
98 ms |
12400 KB |
Output is correct |
29 |
Correct |
713 ms |
23020 KB |
Output is correct |
30 |
Correct |
97 ms |
12388 KB |
Output is correct |
31 |
Correct |
707 ms |
23004 KB |
Output is correct |
32 |
Correct |
473 ms |
23012 KB |
Output is correct |
33 |
Correct |
599 ms |
37104 KB |
Output is correct |
34 |
Correct |
693 ms |
23020 KB |
Output is correct |
35 |
Correct |
3 ms |
8532 KB |
Output is correct |
36 |
Correct |
3 ms |
8532 KB |
Output is correct |
37 |
Correct |
44 ms |
11512 KB |
Output is correct |
38 |
Correct |
447 ms |
32404 KB |
Output is correct |
39 |
Correct |
533 ms |
25220 KB |
Output is correct |
40 |
Correct |
583 ms |
23200 KB |
Output is correct |
41 |
Correct |
645 ms |
22944 KB |
Output is correct |
42 |
Correct |
728 ms |
23072 KB |
Output is correct |
43 |
Correct |
460 ms |
23964 KB |
Output is correct |
44 |
Correct |
492 ms |
26052 KB |
Output is correct |
45 |
Correct |
60 ms |
13960 KB |
Output is correct |
46 |
Correct |
473 ms |
51168 KB |
Output is correct |
47 |
Correct |
460 ms |
22980 KB |
Output is correct |
48 |
Correct |
4 ms |
8532 KB |
Output is correct |
49 |
Correct |
4 ms |
8532 KB |
Output is correct |
50 |
Correct |
4 ms |
8532 KB |
Output is correct |
51 |
Correct |
3 ms |
8532 KB |
Output is correct |
52 |
Correct |
3 ms |
8532 KB |
Output is correct |
53 |
Correct |
4 ms |
8532 KB |
Output is correct |
54 |
Correct |
12 ms |
9172 KB |
Output is correct |
55 |
Correct |
13 ms |
9344 KB |
Output is correct |
56 |
Correct |
12 ms |
9172 KB |
Output is correct |
57 |
Correct |
17 ms |
9316 KB |
Output is correct |
58 |
Correct |
12 ms |
9172 KB |
Output is correct |
59 |
Correct |
16 ms |
9208 KB |
Output is correct |
60 |
Correct |
15 ms |
9620 KB |
Output is correct |
61 |
Incorrect |
48 ms |
13288 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |