#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
int perm[200010];
queue<int>kolejka;
set<int>zera;
int n, k;
bool dodane[200010];
int nast(int val){
if(zera.size()==0)return -1;
if(zera.upper_bound(val)==zera.end()){
return *zera.begin();
}
return *zera.upper_bound(val);
}
void rozpatrz(int val){
if(val==-1)return;
if(dodane[val])return;
if(zera.lower_bound(val)==zera.begin()){
int pom = *zera.rbegin();
if(val - pom+n > k-1){
kolejka.push(val);
dodane[val] = 1;
}
return;
}
else{
auto it = zera.lower_bound(val);
it--;
int pom =*it;
if(val-pom > k-1){
kolejka.push(val);
dodane[val] = 1;
}
}
}
const int base = 1<<18;
int mini[2*base];
int lazy[2*base];
void push(int w){
if(!lazy[w])return;
mini[w*2]+=lazy[w];
lazy[w*2]+=lazy[w];
mini[w*2+1]+=lazy[w];
lazy[w*2+1]+=lazy[w];
mini[w] = min(mini[w*2], mini[w*2+1]);
lazy[w] = 0;
}
void dodaj(int w, int l, int p, int a, int b, int val){
if(b<l || p<a)return;
if(a<=l && p<=b){
mini[w]+=val;
lazy[w]+=val;
return;
}
push(w);
dodaj(w*2, l, (l+p)/2, a, b, val);
dodaj(w*2+1, (l+p)/2+1, p, a, b, val);
mini[w] = min(mini[w*2], mini[w*2+1]);
}
void odejmij(int l, int p){
l = (l+n)%n, p = (p+n)%n;
if(l<=p)
dodaj(1, 0, base-1, l, p, -1);
else{
dodaj(1, 0, base-1, l, n-1, -1);
dodaj(1, 0, base-1, 0, p, -1);
}
}
int znajdz(int w){
if(w>=base){
if(mini[w]==0)return w-base;
else return -1;
}
if(mini[w]>0)return -1;
push(w);
int pom = znajdz(w*2);
if(pom==-1)pom = znajdz(w*2+1);
else znajdz(w*2+1);
mini[w] = min(mini[w*2], mini[w*2+1]);
return pom;
}
pair<int, long long> prawo[200010][18], lewo[200010][18];
int skacz_prawo(int start, int odl){
for(int j = 17;j>=0;j--)
if(odl>=prawo[start][j].second){
odl-=prawo[start][j].second;
start = prawo[start][j].first;
}
return start;
}
int skacz_lewo(int start, int odl){
for(int j = 17;j>=0;j--)
if(odl>=lewo[start][j].second){
odl-=lewo[start][j].second;
start = lewo[start][j].first;
}
return start;
}
void init(int K, std::vector<int> r) {
n = r.size();
int i;
k = K;
//odzyskaj permutacje
for(i=0;i<n;i++)
if(r[i]==0)
zera.insert(i);
for(auto j: zera){
rozpatrz(j);
}
for(i=0;i<base;i++){
if(i>=n || r[i]==0)
dodaj(1, 0, base-1, i, i, 1000000000);
else
dodaj(1, 0, base-1, i, i, r[i]);
}
int numer = n-1;
while(kolejka.size()){
auto x = kolejka.front();
// printf("%d:\n", x);
kolejka.pop();
perm[x] = numer--;
zera.erase(x);
odejmij(x-k+1, x-1);
vector<int>nowe_zera;
while(mini[1]==0){
int j = znajdz(1);
// printf("%d\n", j);
dodaj(1, 0, base-1, j, j, 1000000000);
// for(i=1;i<2*base;i++)printf("%d-%d ", mini[i], lazy[i]);printf("\n");
// printf("drzewo\n");
// exit(0);
zera.insert(j);
nowe_zera.push_back(j);
}
for(auto j: nowe_zera)
rozpatrz(j);
rozpatrz(nast(x));
// for(auto j: nowe_zera)printf("%d ", j);
// printf("\nKONIEC %d\n", x);
}
// for(i=0;i<n;i++)printf("%d ", perm[i]);printf("\n");
//skok w prawo
set<pair<int,int>>sasiedzi;
for(i=0;i<k-1;i++)
sasiedzi.insert({perm[i], i});
for(i=n-1;i>=0;i--){
if(sasiedzi.lower_bound({perm[i], i})==sasiedzi.begin())
prawo[i][0] = {i,0};
else{
auto it = sasiedzi.lower_bound({perm[i], i});
it--;
prawo[i][0] = {(*it).second, (n+(*it).second-i)%n};
}
sasiedzi.erase({perm[(i+k-1)%n], (i+k-1)%n});
sasiedzi.insert({perm[i], i});
}
//lewo
sasiedzi.clear();
for(i=0;i<k-1;i++)
sasiedzi.insert({perm[n-i-1], n-i-1});
for(i=0;i<n;i++){
// for(auto j: sasiedzi)printf("%d-%d\n", j.first, j.second);printf("\n");
if(sasiedzi.lower_bound({perm[i], i})==sasiedzi.begin())
lewo[i][0] = {i,0};
else{
auto it = sasiedzi.lower_bound({perm[i], i});
it--;
lewo[i][0] = {(*it).second, (n-(*it).second+i)%n};
}
sasiedzi.erase({perm[(n+i-(k-1))%n], (n+i-(k-1))%n});
sasiedzi.insert({perm[i], i});
}
for(int j = 1;j<18;j++)
for(i=0;i<n;i++){
prawo[i][j] = {prawo[prawo[i][j-1].first][j-1].first, prawo[i][j-1].second+ prawo[prawo[i][j-1].first][j-1].second};
lewo[i][j] = {lewo[lewo[i][j-1].first][j-1].first, lewo[i][j-1].second+ lewo[lewo[i][j-1].first][j-1].second};
}
/*
for(i=0;i<n;i++)printf("%d ", perm[i]);printf("\n");
printf("PRAWO: ");for(i=0;i<n;i++)
printf("%d-%d " ,prawo[i][0].first,prawo[i][0].second);printf("\n");
printf("LEWO: ");for(i=0;i<n;i++)
printf("%d-%d " ,lewo[i][0].first,lewo[i][0].second);printf("\n");
*/
return;
}
int compare_plants(int x, int y) {
// printf("%d %d\n", x, y);
int odl, z;
//w prawo od x
odl = (n+y-x)%n;
// printf("odl: %d\n", odl);
z = skacz_prawo(x, odl);
// printf("%d %d %d\n", x, y, z);
if((n+y-z)%n<k && perm[z]>=perm[y])return 1;
//w lowe od x
odl = (n+x-y)%n;
z = skacz_lewo(x, odl);
// printf("odl: %d\n", odl);
// printf("%d %d %d\n", x, y, z);
if((n+z-y)%n<k && perm[z]>=perm[y])return 1;
swap(x, y);
//w prawo od x
odl = (n+y-x)%n;
// printf("odl: %d\n", odl);
// for(int i=0;i<n;i++)printf("%d ", skacz_prawo[i]);
z = skacz_prawo(x, odl);
// exit(0);
if((n+y-z)%n<k && perm[z]>=perm[y])return -1;
//w lowe od x
odl = (n+x-y)%n;
// printf("odl: %d\n", odl);
z = skacz_lewo(x, odl);
if((n+z-y)%n<k && perm[z]>=perm[y])return -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
5716 KB |
Output is correct |
2 |
Correct |
38 ms |
5844 KB |
Output is correct |
3 |
Correct |
39 ms |
5712 KB |
Output is correct |
4 |
Correct |
38 ms |
5712 KB |
Output is correct |
5 |
Correct |
41 ms |
3652 KB |
Output is correct |
6 |
Correct |
101 ms |
6140 KB |
Output is correct |
7 |
Correct |
145 ms |
20112 KB |
Output is correct |
8 |
Correct |
464 ms |
125780 KB |
Output is correct |
9 |
Correct |
488 ms |
125520 KB |
Output is correct |
10 |
Correct |
475 ms |
125520 KB |
Output is correct |
11 |
Correct |
450 ms |
126036 KB |
Output is correct |
12 |
Correct |
450 ms |
126036 KB |
Output is correct |
13 |
Correct |
378 ms |
130296 KB |
Output is correct |
14 |
Correct |
513 ms |
121060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5712 KB |
Output is correct |
2 |
Correct |
38 ms |
5700 KB |
Output is correct |
3 |
Correct |
38 ms |
5712 KB |
Output is correct |
4 |
Correct |
39 ms |
5716 KB |
Output is correct |
5 |
Correct |
43 ms |
5900 KB |
Output is correct |
6 |
Correct |
41 ms |
8020 KB |
Output is correct |
7 |
Correct |
96 ms |
13136 KB |
Output is correct |
8 |
Correct |
39 ms |
5968 KB |
Output is correct |
9 |
Correct |
41 ms |
8132 KB |
Output is correct |
10 |
Correct |
94 ms |
13068 KB |
Output is correct |
11 |
Correct |
102 ms |
12884 KB |
Output is correct |
12 |
Correct |
128 ms |
13140 KB |
Output is correct |
13 |
Correct |
92 ms |
13140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5712 KB |
Output is correct |
2 |
Correct |
38 ms |
5700 KB |
Output is correct |
3 |
Correct |
38 ms |
5712 KB |
Output is correct |
4 |
Correct |
39 ms |
5716 KB |
Output is correct |
5 |
Correct |
43 ms |
5900 KB |
Output is correct |
6 |
Correct |
41 ms |
8020 KB |
Output is correct |
7 |
Correct |
96 ms |
13136 KB |
Output is correct |
8 |
Correct |
39 ms |
5968 KB |
Output is correct |
9 |
Correct |
41 ms |
8132 KB |
Output is correct |
10 |
Correct |
94 ms |
13068 KB |
Output is correct |
11 |
Correct |
102 ms |
12884 KB |
Output is correct |
12 |
Correct |
128 ms |
13140 KB |
Output is correct |
13 |
Correct |
92 ms |
13140 KB |
Output is correct |
14 |
Correct |
131 ms |
21072 KB |
Output is correct |
15 |
Correct |
825 ms |
127568 KB |
Output is correct |
16 |
Correct |
133 ms |
21076 KB |
Output is correct |
17 |
Correct |
807 ms |
127828 KB |
Output is correct |
18 |
Correct |
578 ms |
126468 KB |
Output is correct |
19 |
Correct |
651 ms |
126444 KB |
Output is correct |
20 |
Correct |
812 ms |
131312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5724 KB |
Output is correct |
2 |
Correct |
38 ms |
3732 KB |
Output is correct |
3 |
Correct |
105 ms |
10900 KB |
Output is correct |
4 |
Correct |
495 ms |
124756 KB |
Output is correct |
5 |
Correct |
573 ms |
122620 KB |
Output is correct |
6 |
Correct |
592 ms |
121872 KB |
Output is correct |
7 |
Correct |
639 ms |
122192 KB |
Output is correct |
8 |
Correct |
773 ms |
129612 KB |
Output is correct |
9 |
Correct |
464 ms |
127060 KB |
Output is correct |
10 |
Correct |
503 ms |
125844 KB |
Output is correct |
11 |
Correct |
390 ms |
133200 KB |
Output is correct |
12 |
Correct |
573 ms |
124904 KB |
Output is correct |
13 |
Correct |
532 ms |
131412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5716 KB |
Output is correct |
2 |
Correct |
39 ms |
5852 KB |
Output is correct |
3 |
Correct |
38 ms |
5792 KB |
Output is correct |
4 |
Correct |
39 ms |
5852 KB |
Output is correct |
5 |
Correct |
39 ms |
5784 KB |
Output is correct |
6 |
Correct |
40 ms |
5724 KB |
Output is correct |
7 |
Correct |
59 ms |
6480 KB |
Output is correct |
8 |
Correct |
49 ms |
6480 KB |
Output is correct |
9 |
Correct |
52 ms |
6520 KB |
Output is correct |
10 |
Correct |
52 ms |
4628 KB |
Output is correct |
11 |
Correct |
54 ms |
4432 KB |
Output is correct |
12 |
Correct |
54 ms |
6228 KB |
Output is correct |
13 |
Correct |
48 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5716 KB |
Output is correct |
2 |
Correct |
38 ms |
5716 KB |
Output is correct |
3 |
Correct |
43 ms |
5756 KB |
Output is correct |
4 |
Correct |
38 ms |
5712 KB |
Output is correct |
5 |
Correct |
40 ms |
8016 KB |
Output is correct |
6 |
Correct |
554 ms |
121940 KB |
Output is correct |
7 |
Correct |
590 ms |
121940 KB |
Output is correct |
8 |
Correct |
596 ms |
122360 KB |
Output is correct |
9 |
Correct |
756 ms |
128848 KB |
Output is correct |
10 |
Correct |
453 ms |
126288 KB |
Output is correct |
11 |
Correct |
580 ms |
128340 KB |
Output is correct |
12 |
Correct |
395 ms |
127060 KB |
Output is correct |
13 |
Correct |
530 ms |
124752 KB |
Output is correct |
14 |
Correct |
514 ms |
124500 KB |
Output is correct |
15 |
Correct |
591 ms |
124956 KB |
Output is correct |
16 |
Correct |
454 ms |
125176 KB |
Output is correct |
17 |
Correct |
497 ms |
124368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
5716 KB |
Output is correct |
2 |
Correct |
38 ms |
5844 KB |
Output is correct |
3 |
Correct |
39 ms |
5712 KB |
Output is correct |
4 |
Correct |
38 ms |
5712 KB |
Output is correct |
5 |
Correct |
41 ms |
3652 KB |
Output is correct |
6 |
Correct |
101 ms |
6140 KB |
Output is correct |
7 |
Correct |
145 ms |
20112 KB |
Output is correct |
8 |
Correct |
464 ms |
125780 KB |
Output is correct |
9 |
Correct |
488 ms |
125520 KB |
Output is correct |
10 |
Correct |
475 ms |
125520 KB |
Output is correct |
11 |
Correct |
450 ms |
126036 KB |
Output is correct |
12 |
Correct |
450 ms |
126036 KB |
Output is correct |
13 |
Correct |
378 ms |
130296 KB |
Output is correct |
14 |
Correct |
513 ms |
121060 KB |
Output is correct |
15 |
Correct |
38 ms |
5712 KB |
Output is correct |
16 |
Correct |
38 ms |
5700 KB |
Output is correct |
17 |
Correct |
38 ms |
5712 KB |
Output is correct |
18 |
Correct |
39 ms |
5716 KB |
Output is correct |
19 |
Correct |
43 ms |
5900 KB |
Output is correct |
20 |
Correct |
41 ms |
8020 KB |
Output is correct |
21 |
Correct |
96 ms |
13136 KB |
Output is correct |
22 |
Correct |
39 ms |
5968 KB |
Output is correct |
23 |
Correct |
41 ms |
8132 KB |
Output is correct |
24 |
Correct |
94 ms |
13068 KB |
Output is correct |
25 |
Correct |
102 ms |
12884 KB |
Output is correct |
26 |
Correct |
128 ms |
13140 KB |
Output is correct |
27 |
Correct |
92 ms |
13140 KB |
Output is correct |
28 |
Correct |
131 ms |
21072 KB |
Output is correct |
29 |
Correct |
825 ms |
127568 KB |
Output is correct |
30 |
Correct |
133 ms |
21076 KB |
Output is correct |
31 |
Correct |
807 ms |
127828 KB |
Output is correct |
32 |
Correct |
578 ms |
126468 KB |
Output is correct |
33 |
Correct |
651 ms |
126444 KB |
Output is correct |
34 |
Correct |
812 ms |
131312 KB |
Output is correct |
35 |
Correct |
38 ms |
5724 KB |
Output is correct |
36 |
Correct |
38 ms |
3732 KB |
Output is correct |
37 |
Correct |
105 ms |
10900 KB |
Output is correct |
38 |
Correct |
495 ms |
124756 KB |
Output is correct |
39 |
Correct |
573 ms |
122620 KB |
Output is correct |
40 |
Correct |
592 ms |
121872 KB |
Output is correct |
41 |
Correct |
639 ms |
122192 KB |
Output is correct |
42 |
Correct |
773 ms |
129612 KB |
Output is correct |
43 |
Correct |
464 ms |
127060 KB |
Output is correct |
44 |
Correct |
503 ms |
125844 KB |
Output is correct |
45 |
Correct |
390 ms |
133200 KB |
Output is correct |
46 |
Correct |
573 ms |
124904 KB |
Output is correct |
47 |
Correct |
532 ms |
131412 KB |
Output is correct |
48 |
Correct |
38 ms |
5716 KB |
Output is correct |
49 |
Correct |
39 ms |
5852 KB |
Output is correct |
50 |
Correct |
38 ms |
5792 KB |
Output is correct |
51 |
Correct |
39 ms |
5852 KB |
Output is correct |
52 |
Correct |
39 ms |
5784 KB |
Output is correct |
53 |
Correct |
40 ms |
5724 KB |
Output is correct |
54 |
Correct |
59 ms |
6480 KB |
Output is correct |
55 |
Correct |
49 ms |
6480 KB |
Output is correct |
56 |
Correct |
52 ms |
6520 KB |
Output is correct |
57 |
Correct |
52 ms |
4628 KB |
Output is correct |
58 |
Correct |
54 ms |
4432 KB |
Output is correct |
59 |
Correct |
54 ms |
6228 KB |
Output is correct |
60 |
Correct |
48 ms |
4176 KB |
Output is correct |
61 |
Correct |
118 ms |
12628 KB |
Output is correct |
62 |
Correct |
160 ms |
22868 KB |
Output is correct |
63 |
Correct |
526 ms |
125480 KB |
Output is correct |
64 |
Correct |
552 ms |
125048 KB |
Output is correct |
65 |
Correct |
635 ms |
125012 KB |
Output is correct |
66 |
Correct |
623 ms |
125780 KB |
Output is correct |
67 |
Correct |
765 ms |
129624 KB |
Output is correct |
68 |
Correct |
526 ms |
127064 KB |
Output is correct |
69 |
Correct |
725 ms |
129144 KB |
Output is correct |
70 |
Correct |
493 ms |
127876 KB |
Output is correct |
71 |
Correct |
608 ms |
125572 KB |
Output is correct |
72 |
Correct |
623 ms |
125264 KB |
Output is correct |
73 |
Correct |
636 ms |
125704 KB |
Output is correct |
74 |
Correct |
541 ms |
125524 KB |
Output is correct |
75 |
Correct |
528 ms |
125140 KB |
Output is correct |