#include <bits/stdc++.h>
#include "plants.h"
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const int II = 2 * 1000 * 1000 * 1000;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 1<<18;
int n, k;
int drz[2 * N], laz[2 * N];
int tab[N];
ll jp[N][20];
ll jp2[N][20];
vector<int> cur;
inline void Push(int v)
{
drz[v * 2] += laz[v]; laz[v * 2] += laz[v];
drz[v * 2 + 1] += laz[v]; laz[v * 2 + 1] += laz[v];
laz[v] = 0;
}
int Find(int v, int a, int b, int pz, int kz)
{
if(b < pz || a > kz || drz[v] > 0) return -1;
if(v > N)
{
drz[v] = II;
return v - N;
}
Push(v);
int ans = Find(v * 2 + 1, (a + b) / 2 + 1, b, pz, kz);
drz[v] = min(drz[v * 2], drz[v * 2 + 1]);
if(ans != -1) return ans;
ans = Find(v * 2, a, (a + b) / 2, pz, kz);
drz[v] = min(drz[v * 2], drz[v * 2 + 1]);
return ans;
}
void Add(int v, int a, int b, int pz, int kz)
{
if(b < pz || a > kz) return;
if(a >= pz && b <= kz)
{
--drz[v]; --laz[v];
return;
}
Add(v * 2, a, (a + b) / 2, pz, kz);
Add(v * 2 + 1, (a + b) / 2 + 1, b, pz, kz);
drz[v] = min(drz[v * 2], drz[v * 2 + 1]) + laz[v];
}
int F(int i)
{
int a = -1;
if(i > 1)
a = Find(1, 0, N - 1, max(i - k, 1), i - 1);
if(a != -1) return a;
if(i <= k)
a = Find(1, 0, N - 1, n - (k - i), n);
return a;
}
void D(int i)
{
if(i > 1)
Add(1, 0, N - 1, max(i - k, 1), i - 1);
if(i <= k)
Add(1, 0, N - 1, n - (k - i), n);
}
inline int Dis(int i, int j)
{
if(i < j)
return j - i;
return n - (i - j);
}
inline int J(int i, ll d)
{
i += d % (ll)n;
if(i > n) i -= n;
if(i < 1) i += n;
return i;
}
void DoJP1()
{
//cerr << n << " " << k << "\n";
set<pair<int, int>> zbi;
set<pair<int, int>>::iterator it;
for(int i = 2; i <= k + 1; ++i)
zbi.insert(make_pair(tab[i], i));
for(int i = 1; i <= n; ++i)
{
it = zbi.lower_bound(make_pair(tab[i], 0));
//cerr << "jp1 " << (*it).nd << "\n";
if(it == zbi.end())
jp[i][0] = 0;
else
jp[i][0] = Dis(i, (*it).nd);
if(i < n)
zbi.erase(make_pair(tab[i + 1], i + 1));
int nxt = J(i, k + 1);
zbi.insert(make_pair(tab[nxt], nxt));
}
for(int j = 1; j <= 19; ++j)
for(int i = 1; i <= n; ++i)
jp[i][j] = jp[i][j - 1] + jp[J(i, jp[i][j - 1])][j - 1];
/*for(int i = 1; i <= n; ++i)
cerr << jp[i][0] << " ";
cerr << "\n";*/
}
void DoJP2()
{
set<pair<int, int>> zbi;
set<pair<int, int>>::iterator it;
for(int i = n; i > n - k; --i)
zbi.insert(make_pair(tab[i], i));
for(int i = 1; i <= n; ++i)
{
it = zbi.lower_bound(make_pair(tab[i], 0));
//cerr << "jp2: " << (*it).nd << "\n";
if(it == zbi.end())
jp2[i][0] = 0;
else
jp2[i][0] = -Dis((*it).nd, i);
int pr = J(i, -k);
zbi.erase(make_pair(tab[pr], pr));
zbi.insert(make_pair(tab[i], i));
}
for(int j = 1; j <= 19; ++j)
for(int i = 1; i <= n; ++i)
jp2[i][j] = jp2[i][j - 1] + jp2[J(i, jp2[i][j - 1])][j - 1];
//for(int i = 1; i <= n; ++i)
//cerr << jp2[i][0] << " ";
//cerr << "\n";
}
void init(int KK, vector<int> r)
{
n = r.size();
k = KK - 1;
/*cerr << "init: \n";
for(int i = 0; i < n; ++i)
cerr << r[i] << " ";
cerr << "\n";*/
for(int i = 0; i < n; ++i)
drz[i + N + 1] = r[i];
drz[N] = II;
for(int i = n + 1; i < N; ++i)
drz[i + N] = II;
for(int i = N - 1; i >= 1; --i)
drz[i] = min(drz[i * 2], drz[i * 2 + 1]);
for(int i = n; i >= 1; --i)
{
vector<int> cur;
cur.pb(Find(1, 0, N - 1, 1, n));
//cerr << "cur: " << i << "\n";
while(cur.size() > 0)
{
//cout << "beg: " << cur.back() << "\n";
int x = F(cur.back());
while(x != -1)
{
cur.pb(x);
x = F(x);
}
int v = cur.back();
//cerr << i << " " << v << "\n";
tab[v] = i; --i;
D(v);
cur.pop_back();
}
//cerr << "\n";
++i;
}
/*for(int i = 1; i <= n; ++i)
cerr << tab[i] << " ";
cerr << "\n";*/
//cerr << "xd\n";
//cerr << k << "\n";
DoJP1();
DoJP2();
}
int Jump(int a, int d)
{
for(int i = 19; i >= 0; --i)
if(jp[a][i] <= (ll)d)
{
d -= jp[a][i];
a = J(a, jp[a][i]);
}
return a;
}
int Jump2(int a, int d)
{
for(int i = 19; i >= 0; --i)
if(-jp2[a][i] <= (ll)d)
{
d += jp2[a][i];
a = J(a, jp2[a][i]);
}
return a;
}
bool Cp(int x, int y)
{
int a = Jump(x, Dis(x, y));
int b = Jump2(x, -Dis(x, y) + n);
//cerr << -Dis(x, y) + n << "\n";
if(a == y || b == y) return true;
if(Dis(a, y) <= k && tab[a] < tab[y]) return true;
if(Dis(y, b) <= k && tab[b] < tab[y]) return true;
return false;
}
int compare_plants(int x, int y)
{
++x; ++y;
int answer = 0;
if(tab[x] < tab[y] && Cp(x, y))
answer = -1;
if(tab[x] > tab[y] && Cp(y, x))
answer = 1;
//cerr << "que: " << x << " " << y << " tab: " << tab[x] << " " << tab[y] << " ans: " << answer << "\n";
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2488 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
82 ms |
6140 KB |
Output is correct |
7 |
Correct |
102 ms |
13964 KB |
Output is correct |
8 |
Correct |
268 ms |
74324 KB |
Output is correct |
9 |
Correct |
264 ms |
74324 KB |
Output is correct |
10 |
Correct |
284 ms |
74280 KB |
Output is correct |
11 |
Correct |
297 ms |
74404 KB |
Output is correct |
12 |
Correct |
314 ms |
74288 KB |
Output is correct |
13 |
Correct |
341 ms |
74320 KB |
Output is correct |
14 |
Correct |
334 ms |
74260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
4 ms |
2980 KB |
Output is correct |
7 |
Correct |
56 ms |
9044 KB |
Output is correct |
8 |
Correct |
3 ms |
2648 KB |
Output is correct |
9 |
Correct |
4 ms |
2908 KB |
Output is correct |
10 |
Correct |
69 ms |
9044 KB |
Output is correct |
11 |
Correct |
95 ms |
8824 KB |
Output is correct |
12 |
Correct |
93 ms |
8948 KB |
Output is correct |
13 |
Correct |
57 ms |
9148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
4 ms |
2980 KB |
Output is correct |
7 |
Correct |
56 ms |
9044 KB |
Output is correct |
8 |
Correct |
3 ms |
2648 KB |
Output is correct |
9 |
Correct |
4 ms |
2908 KB |
Output is correct |
10 |
Correct |
69 ms |
9044 KB |
Output is correct |
11 |
Correct |
95 ms |
8824 KB |
Output is correct |
12 |
Correct |
93 ms |
8948 KB |
Output is correct |
13 |
Correct |
57 ms |
9148 KB |
Output is correct |
14 |
Correct |
93 ms |
14576 KB |
Output is correct |
15 |
Correct |
650 ms |
80724 KB |
Output is correct |
16 |
Correct |
92 ms |
14676 KB |
Output is correct |
17 |
Correct |
679 ms |
80724 KB |
Output is correct |
18 |
Correct |
514 ms |
79296 KB |
Output is correct |
19 |
Correct |
570 ms |
79852 KB |
Output is correct |
20 |
Correct |
709 ms |
84560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
84 ms |
7560 KB |
Output is correct |
4 |
Correct |
343 ms |
74508 KB |
Output is correct |
5 |
Correct |
388 ms |
74576 KB |
Output is correct |
6 |
Correct |
436 ms |
74580 KB |
Output is correct |
7 |
Correct |
497 ms |
75348 KB |
Output is correct |
8 |
Correct |
609 ms |
79184 KB |
Output is correct |
9 |
Correct |
391 ms |
74392 KB |
Output is correct |
10 |
Correct |
358 ms |
74488 KB |
Output is correct |
11 |
Correct |
340 ms |
74436 KB |
Output is correct |
12 |
Correct |
381 ms |
74324 KB |
Output is correct |
13 |
Correct |
442 ms |
77344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2340 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
4 ms |
2652 KB |
Output is correct |
7 |
Correct |
20 ms |
3420 KB |
Output is correct |
8 |
Correct |
13 ms |
3420 KB |
Output is correct |
9 |
Correct |
19 ms |
3504 KB |
Output is correct |
10 |
Correct |
12 ms |
3392 KB |
Output is correct |
11 |
Correct |
20 ms |
3512 KB |
Output is correct |
12 |
Correct |
19 ms |
3512 KB |
Output is correct |
13 |
Correct |
12 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2472 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2392 KB |
Output is correct |
5 |
Correct |
2 ms |
2752 KB |
Output is correct |
6 |
Correct |
310 ms |
73556 KB |
Output is correct |
7 |
Correct |
333 ms |
73848 KB |
Output is correct |
8 |
Correct |
442 ms |
74248 KB |
Output is correct |
9 |
Correct |
596 ms |
78260 KB |
Output is correct |
10 |
Correct |
297 ms |
73888 KB |
Output is correct |
11 |
Correct |
464 ms |
77908 KB |
Output is correct |
12 |
Correct |
300 ms |
73336 KB |
Output is correct |
13 |
Correct |
322 ms |
73552 KB |
Output is correct |
14 |
Correct |
393 ms |
73808 KB |
Output is correct |
15 |
Correct |
426 ms |
74320 KB |
Output is correct |
16 |
Correct |
311 ms |
73408 KB |
Output is correct |
17 |
Correct |
362 ms |
73488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2488 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
82 ms |
6140 KB |
Output is correct |
7 |
Correct |
102 ms |
13964 KB |
Output is correct |
8 |
Correct |
268 ms |
74324 KB |
Output is correct |
9 |
Correct |
264 ms |
74324 KB |
Output is correct |
10 |
Correct |
284 ms |
74280 KB |
Output is correct |
11 |
Correct |
297 ms |
74404 KB |
Output is correct |
12 |
Correct |
314 ms |
74288 KB |
Output is correct |
13 |
Correct |
341 ms |
74320 KB |
Output is correct |
14 |
Correct |
334 ms |
74260 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2396 KB |
Output is correct |
20 |
Correct |
4 ms |
2980 KB |
Output is correct |
21 |
Correct |
56 ms |
9044 KB |
Output is correct |
22 |
Correct |
3 ms |
2648 KB |
Output is correct |
23 |
Correct |
4 ms |
2908 KB |
Output is correct |
24 |
Correct |
69 ms |
9044 KB |
Output is correct |
25 |
Correct |
95 ms |
8824 KB |
Output is correct |
26 |
Correct |
93 ms |
8948 KB |
Output is correct |
27 |
Correct |
57 ms |
9148 KB |
Output is correct |
28 |
Correct |
93 ms |
14576 KB |
Output is correct |
29 |
Correct |
650 ms |
80724 KB |
Output is correct |
30 |
Correct |
92 ms |
14676 KB |
Output is correct |
31 |
Correct |
679 ms |
80724 KB |
Output is correct |
32 |
Correct |
514 ms |
79296 KB |
Output is correct |
33 |
Correct |
570 ms |
79852 KB |
Output is correct |
34 |
Correct |
709 ms |
84560 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
84 ms |
7560 KB |
Output is correct |
38 |
Correct |
343 ms |
74508 KB |
Output is correct |
39 |
Correct |
388 ms |
74576 KB |
Output is correct |
40 |
Correct |
436 ms |
74580 KB |
Output is correct |
41 |
Correct |
497 ms |
75348 KB |
Output is correct |
42 |
Correct |
609 ms |
79184 KB |
Output is correct |
43 |
Correct |
391 ms |
74392 KB |
Output is correct |
44 |
Correct |
358 ms |
74488 KB |
Output is correct |
45 |
Correct |
340 ms |
74436 KB |
Output is correct |
46 |
Correct |
381 ms |
74324 KB |
Output is correct |
47 |
Correct |
442 ms |
77344 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
2 ms |
2340 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
4 ms |
2652 KB |
Output is correct |
54 |
Correct |
20 ms |
3420 KB |
Output is correct |
55 |
Correct |
13 ms |
3420 KB |
Output is correct |
56 |
Correct |
19 ms |
3504 KB |
Output is correct |
57 |
Correct |
12 ms |
3392 KB |
Output is correct |
58 |
Correct |
20 ms |
3512 KB |
Output is correct |
59 |
Correct |
19 ms |
3512 KB |
Output is correct |
60 |
Correct |
12 ms |
3420 KB |
Output is correct |
61 |
Correct |
120 ms |
7504 KB |
Output is correct |
62 |
Correct |
122 ms |
14004 KB |
Output is correct |
63 |
Correct |
333 ms |
74320 KB |
Output is correct |
64 |
Correct |
396 ms |
74360 KB |
Output is correct |
65 |
Correct |
457 ms |
74724 KB |
Output is correct |
66 |
Correct |
512 ms |
75348 KB |
Output is correct |
67 |
Correct |
702 ms |
79088 KB |
Output is correct |
68 |
Correct |
399 ms |
74540 KB |
Output is correct |
69 |
Correct |
555 ms |
78796 KB |
Output is correct |
70 |
Correct |
408 ms |
74436 KB |
Output is correct |
71 |
Correct |
394 ms |
74480 KB |
Output is correct |
72 |
Correct |
453 ms |
74580 KB |
Output is correct |
73 |
Correct |
514 ms |
75292 KB |
Output is correct |
74 |
Correct |
332 ms |
74212 KB |
Output is correct |
75 |
Correct |
384 ms |
74596 KB |
Output is correct |