#include<bits/stdc++.h>
#include "plants.h"
using namespace std;
const int INF=1e8+9;
struct Node
{
int minn;
int lewy,prawy;
int MaxxDiff,ind;
Node(int minn=0, int lewy=0, int prawy=0, int MaxxDiff=0, int ind=0) : minn(minn),lewy(lewy),prawy(prawy),MaxxDiff(MaxxDiff),ind(ind){}
Node operator*(const Node &B)const
{
if(minn<B.minn)
{
return {minn,lewy,prawy,MaxxDiff,ind};
}
else if(B.minn<minn){return B;}
else
{
int Mu=MaxxDiff,u=ind;
if(B.MaxxDiff>Mu)
{
Mu=B.MaxxDiff;
u=B.ind;
}
int kand=B.lewy-prawy;
if(kand>Mu)
{
Mu=kand;
u=B.lewy;
}
return {minn,min(lewy,B.lewy),max(prawy,B.prawy),Mu,u};
}
}
Node& operator +=(const int &dod)
{
this->minn+=dod;
return *this;
}
};
vector<int>tab;
vector<int>examp;
int Gwiazdka[1000000];
Node Tre[1000000];
int n,k,base;
pair<int,int>Tre2[1000000];
int JumpL[400009][20],JumpP[400009][20];
void Przedzial(int v, int a, int b, const int &l, const int &p, const int &dod)
{
if(b<l||p<a){return;}
if(l<=a&&b<=p)
{
Gwiazdka[v]+=dod;
Tre[v]+=dod;
}
else
{
const int mid=(a+b)>>1;
if(Gwiazdka[v]!=0)
{
Gwiazdka[v*2]+=Gwiazdka[v];
Gwiazdka[v*2+1]+=Gwiazdka[v];
Tre[v*2]+=Gwiazdka[v];
Tre[v*2+1]+=Gwiazdka[v];
Gwiazdka[v]=0;
}
Przedzial(v*2,a,mid,l,p,dod);
Przedzial(v*2+1,mid+1,b,l,p,dod);
Tre[v]=Tre[v*2]*Tre[v*2+1];
}
}
int TworzenieDrzewa(int x)
{
int u=1;
while(x>0)
{
x>>=1;
u<<=1;
}
return u;
}
int GetZiom()
{
Node xd=Tre[1];
if(xd.lewy==xd.prawy){return xd.lewy;}
int kand=xd.lewy+n-xd.prawy;
if(kand>xd.MaxxDiff)
{
return xd.lewy;
}
else
{
return xd.ind;
}
}
void init(int K, vector<int>TAB)
{
K--;
tab=TAB;
n=(int)tab.size();
examp.resize(n);
k=K;
base=TworzenieDrzewa(n);
for(int i=0;i<n;i++)
{
Tre[i+base].minn=tab[i];
}
for(int i=n;i<base;i++)
{
Tre[i+base].minn=INF;
}
for(int i=0;i<base;i++)
{
Tre[i+base].lewy=i;
Tre[i+base].prawy=i;
Tre[i+base].MaxxDiff=-1;
Tre[i+base].ind=-1;
}
for(int i=base-1;i>0;i--)
{
Tre[i]=Tre[i*2]*Tre[i*2+1];
}
for(int i=1;i<=n;i++)
{
int u=GetZiom();
examp[u]=n-i+1;
int l=(n+u-k)%n;
int p=(n+u-1)%n;
if(l<=p)
{
Przedzial(1,0,base-1,l,p,-1);
}
else
{
Przedzial(1,0,base-1,l,n-1,-1);
Przedzial(1,0,base-1,0,p,-1);
}
Przedzial(1,0,base-1,u,u,INF);
}
for(int i=0;i<n;i++)
{
examp.push_back(examp[i]);
}
set<pair<int,int>>S={{-1,-1}};
for(int i=0;i<n*2;i++)
{
pair<int,int>xd=*next(S.lower_bound({examp[i],i}),-1);
JumpL[i][0]=xd.second;
S.insert({examp[i],i});
if(i-k>=0)
{
S.erase({examp[i-k],i-k});
}
}
S.clear();
S={{-1,-1}};
for(int i=n*2-1;i>=0;i--)
{
pair<int,int>xd=*next(S.lower_bound({examp[i],i}),-1);
JumpP[i][0]=xd.second;
S.insert({examp[i],i});
if(i+k<n*2)
{
S.erase({examp[i+k],i+k});
}
}
S.clear();
for(int i=0;i<n*2;i++)
{
for(int j=1;j<20;j++)
{
if(JumpL[i][j-1]==-1){JumpL[i][j]=-1;}
else{JumpL[i][j]=JumpL[JumpL[i][j-1]][j-1];}
}
}
for(int i=n*2-1;i>=0;i--)
{
for(int j=1;j<20;j++)
{
if(JumpP[i][j-1]==-1){JumpP[i][j]=-1;}
else{JumpP[i][j]=JumpP[JumpP[i][j-1]][j-1];}
}
}
}
int compare_plants(int x, int y)
{
if(x==y){return 0;}
int mnoznik=1;
if(examp[x]<examp[y])
{
mnoznik=-1;
swap(x,y);
}
///Czy H[x] > H[y];
int a,b;
a=x;
b=y;
if(b<a){b+=n;}
for(int i=19;i>=0;i--)
{
if(JumpP[a][i]!=-1&&JumpP[a][i]<b)
{
a=JumpP[a][i];
}
}
if(b-a<=k&&examp[a]>examp[b]){return mnoznik;}
a=x;
b=y;
if(a<b){a+=n;}
for(int i=19;i>=0;i--)
{
if(JumpL[a][i]!=-1&&b<JumpL[a][i])
{
a=JumpL[a][i];
}
}
if(a-b<=k&&examp[a]>examp[b]){return mnoznik;}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19800 KB |
Output is correct |
2 |
Correct |
8 ms |
19804 KB |
Output is correct |
3 |
Correct |
7 ms |
20008 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
50 ms |
23624 KB |
Output is correct |
7 |
Correct |
93 ms |
31572 KB |
Output is correct |
8 |
Correct |
278 ms |
92480 KB |
Output is correct |
9 |
Correct |
265 ms |
92544 KB |
Output is correct |
10 |
Correct |
270 ms |
92512 KB |
Output is correct |
11 |
Correct |
286 ms |
92484 KB |
Output is correct |
12 |
Correct |
281 ms |
92416 KB |
Output is correct |
13 |
Correct |
275 ms |
92480 KB |
Output is correct |
14 |
Correct |
305 ms |
92392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19804 KB |
Output is correct |
2 |
Correct |
7 ms |
19804 KB |
Output is correct |
3 |
Correct |
7 ms |
19804 KB |
Output is correct |
4 |
Correct |
7 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
20060 KB |
Output is correct |
6 |
Correct |
10 ms |
20316 KB |
Output is correct |
7 |
Correct |
56 ms |
26456 KB |
Output is correct |
8 |
Correct |
9 ms |
20060 KB |
Output is correct |
9 |
Correct |
15 ms |
20400 KB |
Output is correct |
10 |
Correct |
58 ms |
26580 KB |
Output is correct |
11 |
Correct |
56 ms |
26452 KB |
Output is correct |
12 |
Correct |
68 ms |
26452 KB |
Output is correct |
13 |
Correct |
56 ms |
26608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19804 KB |
Output is correct |
2 |
Correct |
7 ms |
19804 KB |
Output is correct |
3 |
Correct |
7 ms |
19804 KB |
Output is correct |
4 |
Correct |
7 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
20060 KB |
Output is correct |
6 |
Correct |
10 ms |
20316 KB |
Output is correct |
7 |
Correct |
56 ms |
26456 KB |
Output is correct |
8 |
Correct |
9 ms |
20060 KB |
Output is correct |
9 |
Correct |
15 ms |
20400 KB |
Output is correct |
10 |
Correct |
58 ms |
26580 KB |
Output is correct |
11 |
Correct |
56 ms |
26452 KB |
Output is correct |
12 |
Correct |
68 ms |
26452 KB |
Output is correct |
13 |
Correct |
56 ms |
26608 KB |
Output is correct |
14 |
Correct |
110 ms |
32144 KB |
Output is correct |
15 |
Correct |
861 ms |
99960 KB |
Output is correct |
16 |
Correct |
105 ms |
32340 KB |
Output is correct |
17 |
Correct |
867 ms |
100012 KB |
Output is correct |
18 |
Correct |
478 ms |
98368 KB |
Output is correct |
19 |
Correct |
543 ms |
99016 KB |
Output is correct |
20 |
Correct |
783 ms |
103492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19800 KB |
Output is correct |
2 |
Correct |
7 ms |
19804 KB |
Output is correct |
3 |
Correct |
58 ms |
25184 KB |
Output is correct |
4 |
Correct |
360 ms |
93252 KB |
Output is correct |
5 |
Correct |
452 ms |
93344 KB |
Output is correct |
6 |
Correct |
571 ms |
93656 KB |
Output is correct |
7 |
Correct |
671 ms |
94272 KB |
Output is correct |
8 |
Correct |
834 ms |
98368 KB |
Output is correct |
9 |
Correct |
352 ms |
93248 KB |
Output is correct |
10 |
Correct |
392 ms |
93476 KB |
Output is correct |
11 |
Correct |
242 ms |
92480 KB |
Output is correct |
12 |
Correct |
385 ms |
93512 KB |
Output is correct |
13 |
Correct |
460 ms |
96324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19804 KB |
Output is correct |
3 |
Correct |
8 ms |
19872 KB |
Output is correct |
4 |
Correct |
8 ms |
19832 KB |
Output is correct |
5 |
Correct |
8 ms |
19800 KB |
Output is correct |
6 |
Correct |
9 ms |
20060 KB |
Output is correct |
7 |
Correct |
18 ms |
20916 KB |
Output is correct |
8 |
Correct |
22 ms |
21084 KB |
Output is correct |
9 |
Correct |
21 ms |
20824 KB |
Output is correct |
10 |
Correct |
18 ms |
21036 KB |
Output is correct |
11 |
Correct |
18 ms |
21080 KB |
Output is correct |
12 |
Correct |
18 ms |
21036 KB |
Output is correct |
13 |
Correct |
16 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19804 KB |
Output is correct |
2 |
Correct |
8 ms |
19804 KB |
Output is correct |
3 |
Correct |
8 ms |
19804 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
11 ms |
20316 KB |
Output is correct |
6 |
Correct |
424 ms |
92484 KB |
Output is correct |
7 |
Correct |
549 ms |
92712 KB |
Output is correct |
8 |
Correct |
733 ms |
93388 KB |
Output is correct |
9 |
Correct |
847 ms |
97368 KB |
Output is correct |
10 |
Correct |
356 ms |
92484 KB |
Output is correct |
11 |
Correct |
547 ms |
96832 KB |
Output is correct |
12 |
Correct |
346 ms |
92224 KB |
Output is correct |
13 |
Correct |
436 ms |
92712 KB |
Output is correct |
14 |
Correct |
505 ms |
92740 KB |
Output is correct |
15 |
Correct |
708 ms |
93504 KB |
Output is correct |
16 |
Correct |
377 ms |
92488 KB |
Output is correct |
17 |
Correct |
428 ms |
92736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19800 KB |
Output is correct |
2 |
Correct |
8 ms |
19804 KB |
Output is correct |
3 |
Correct |
7 ms |
20008 KB |
Output is correct |
4 |
Correct |
8 ms |
19804 KB |
Output is correct |
5 |
Correct |
8 ms |
19804 KB |
Output is correct |
6 |
Correct |
50 ms |
23624 KB |
Output is correct |
7 |
Correct |
93 ms |
31572 KB |
Output is correct |
8 |
Correct |
278 ms |
92480 KB |
Output is correct |
9 |
Correct |
265 ms |
92544 KB |
Output is correct |
10 |
Correct |
270 ms |
92512 KB |
Output is correct |
11 |
Correct |
286 ms |
92484 KB |
Output is correct |
12 |
Correct |
281 ms |
92416 KB |
Output is correct |
13 |
Correct |
275 ms |
92480 KB |
Output is correct |
14 |
Correct |
305 ms |
92392 KB |
Output is correct |
15 |
Correct |
8 ms |
19804 KB |
Output is correct |
16 |
Correct |
7 ms |
19804 KB |
Output is correct |
17 |
Correct |
7 ms |
19804 KB |
Output is correct |
18 |
Correct |
7 ms |
19804 KB |
Output is correct |
19 |
Correct |
8 ms |
20060 KB |
Output is correct |
20 |
Correct |
10 ms |
20316 KB |
Output is correct |
21 |
Correct |
56 ms |
26456 KB |
Output is correct |
22 |
Correct |
9 ms |
20060 KB |
Output is correct |
23 |
Correct |
15 ms |
20400 KB |
Output is correct |
24 |
Correct |
58 ms |
26580 KB |
Output is correct |
25 |
Correct |
56 ms |
26452 KB |
Output is correct |
26 |
Correct |
68 ms |
26452 KB |
Output is correct |
27 |
Correct |
56 ms |
26608 KB |
Output is correct |
28 |
Correct |
110 ms |
32144 KB |
Output is correct |
29 |
Correct |
861 ms |
99960 KB |
Output is correct |
30 |
Correct |
105 ms |
32340 KB |
Output is correct |
31 |
Correct |
867 ms |
100012 KB |
Output is correct |
32 |
Correct |
478 ms |
98368 KB |
Output is correct |
33 |
Correct |
543 ms |
99016 KB |
Output is correct |
34 |
Correct |
783 ms |
103492 KB |
Output is correct |
35 |
Correct |
8 ms |
19800 KB |
Output is correct |
36 |
Correct |
7 ms |
19804 KB |
Output is correct |
37 |
Correct |
58 ms |
25184 KB |
Output is correct |
38 |
Correct |
360 ms |
93252 KB |
Output is correct |
39 |
Correct |
452 ms |
93344 KB |
Output is correct |
40 |
Correct |
571 ms |
93656 KB |
Output is correct |
41 |
Correct |
671 ms |
94272 KB |
Output is correct |
42 |
Correct |
834 ms |
98368 KB |
Output is correct |
43 |
Correct |
352 ms |
93248 KB |
Output is correct |
44 |
Correct |
392 ms |
93476 KB |
Output is correct |
45 |
Correct |
242 ms |
92480 KB |
Output is correct |
46 |
Correct |
385 ms |
93512 KB |
Output is correct |
47 |
Correct |
460 ms |
96324 KB |
Output is correct |
48 |
Correct |
8 ms |
19804 KB |
Output is correct |
49 |
Correct |
8 ms |
19804 KB |
Output is correct |
50 |
Correct |
8 ms |
19872 KB |
Output is correct |
51 |
Correct |
8 ms |
19832 KB |
Output is correct |
52 |
Correct |
8 ms |
19800 KB |
Output is correct |
53 |
Correct |
9 ms |
20060 KB |
Output is correct |
54 |
Correct |
18 ms |
20916 KB |
Output is correct |
55 |
Correct |
22 ms |
21084 KB |
Output is correct |
56 |
Correct |
21 ms |
20824 KB |
Output is correct |
57 |
Correct |
18 ms |
21036 KB |
Output is correct |
58 |
Correct |
18 ms |
21080 KB |
Output is correct |
59 |
Correct |
18 ms |
21036 KB |
Output is correct |
60 |
Correct |
16 ms |
21084 KB |
Output is correct |
61 |
Correct |
55 ms |
25172 KB |
Output is correct |
62 |
Correct |
84 ms |
31652 KB |
Output is correct |
63 |
Correct |
392 ms |
93140 KB |
Output is correct |
64 |
Correct |
432 ms |
93504 KB |
Output is correct |
65 |
Correct |
558 ms |
93764 KB |
Output is correct |
66 |
Correct |
688 ms |
94376 KB |
Output is correct |
67 |
Correct |
860 ms |
98388 KB |
Output is correct |
68 |
Correct |
385 ms |
93248 KB |
Output is correct |
69 |
Correct |
601 ms |
97860 KB |
Output is correct |
70 |
Correct |
376 ms |
93248 KB |
Output is correct |
71 |
Correct |
449 ms |
93364 KB |
Output is correct |
72 |
Correct |
527 ms |
93800 KB |
Output is correct |
73 |
Correct |
689 ms |
94532 KB |
Output is correct |
74 |
Correct |
391 ms |
93252 KB |
Output is correct |
75 |
Correct |
421 ms |
93508 KB |
Output is correct |