#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k, l) for(int i = (j); i < (k); i += (l))
#define FORD(i, j, k, l) for(int i = (j); i >= (k); i -= (l))
#define REP(i, n) FOR(i, 0, n, 1)
#define REPD(i, n) FORD(i, n, 0, 1)
typedef long long ll;
#define pb push_back
int subtask = 1;
const int INF = (int)1e9;
array<int, 2> nl = {INF, INF};
int on;
struct segtree{
vector<array<int, 2>> tree;
vector<int> lazy;
int n;
void push(int v){
lazy[v << 1] += lazy[v];
lazy[v << 1 | 1] += lazy[v];
tree[v << 1][0] += lazy[v];
tree[v << 1 | 1][0] += lazy[v];
lazy[v] = 0;
}
array<int, 2> comb(array<int, 2> a, array<int, 2> b){
if(a[0] == b[0]){
if(a[1] < b[1])
swap(a, b);
if(a[1] - b[1] > (b[1] + on - a[1]))
return a;
else
return b;
}
else{
return min(a, b);
}
}
segtree(){}
segtree(int sz, vector<array<int, 2>> &init){
n = 1;
while(n < sz) n *= 2;
tree.resize(2 * n, nl);
lazy.resize(2 * n, 0);
REP(i, n){
if(i < init.size())
tree[i + n] = init[i];
}
FORD(i, n - 1, 1, 1)
tree[i] = comb(tree[i << 1], tree[i << 1 | 1]);
}
void upd(int l, int r, int val){ upd0(l, r, 0, n, 1, val); }
void upd0(int l, int r, int beg, int end, int v, int val){
if(beg >= l && end <= r){
tree[v][0] += val;
lazy[v] += val;
return;
}
if(beg >= r || end <= l)
return;
push(v);
int mid = (beg + end) >> 1;
upd0(l, r, beg, mid, v << 1, val);
upd0(l, r, mid, end, v << 1 | 1, val);
tree[v] = comb(tree[v << 1], tree[v << 1 | 1]);
}
array<int, 2> query(int l, int r){return query0(l, r, 0, n, 1); }
array<int, 2> query0(int l, int r, int beg, int end, int v){
if(beg >= l && end <= r)
return tree[v];
if(beg >= r || end <= l)
return nl;
push(v);
int mid = (beg + end) >> 1;
return comb(query0(l, r, beg, mid, v << 1),query0(l, r, mid, end, v << 1 | 1));
}
};
segtree st;
int n;
vector<int> pos;
int k;
vector<int> ps;
void init(int kk, std::vector<int> r) {
k = kk;
n = r.size();
if(kk == 2)
subtask = 1;
else
subtask = 2;
if(subtask == 1){
ps.resize(n + 1, 0);
REP(i, n){
if(r[i] == 1){
ps[i + 1] = -1;
}
else{
ps[i + 1] = 1;
}
ps[i + 1] += ps[i];
}
return;
}
on = r.size();
pos.resize(n, 0);
vector<array<int, 2>> in(n);
REP(i, n){
in[i] = {r[i], i};
}
st = segtree(n, in);
int curr = 0;
REP(i, n){
auto x = st.query(0, n);
pos[x[1]] = curr++;
st.upd(x[1], x[1] + 1, INF);
int r = x[1];
int l = x[1] - (k - 1);
if(l >= 0){
st.upd(l, r, -1);
}
else{
st.upd(0, r, -1);
l += n,
st.upd(l, n, -1);
}
}
}
int compare_plants(int x, int y) {
if(subtask == 1){
x++; y++;
bool swapped = false;
if(x > y){
swap(x, y);
swapped = true;
}
int ans = 0;
if(ps[y - 1] - ps[x - 1] == y - x){
ans = 1;
}
else if(ps[y - 1] - ps[x - 1] == x - y){
ans = -1;
}
int sum = ps[n] - ps[y - 1] + ps[x - 1];
int dist = n - (y - x);
if(sum == dist){
ans = -1;
}
else if(sum == -dist){
ans = 1;
}
if(swapped) ans *= -1;
return ans;
}
if(pos[x] < pos[y])
return 1;
else
return -1;
}
Compilation message
plants.cpp: In constructor 'segtree::segtree(int, std::vector<std::array<int, 2> >&)':
plants.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if(i < init.size())
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
73 ms |
3548 KB |
Output is correct |
7 |
Correct |
68 ms |
4972 KB |
Output is correct |
8 |
Correct |
95 ms |
7084 KB |
Output is correct |
9 |
Correct |
105 ms |
7012 KB |
Output is correct |
10 |
Correct |
108 ms |
7012 KB |
Output is correct |
11 |
Correct |
96 ms |
6980 KB |
Output is correct |
12 |
Correct |
88 ms |
7036 KB |
Output is correct |
13 |
Correct |
90 ms |
6980 KB |
Output is correct |
14 |
Correct |
101 ms |
7012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
276 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
68 ms |
5180 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
68 ms |
5204 KB |
Output is correct |
11 |
Correct |
94 ms |
5104 KB |
Output is correct |
12 |
Correct |
66 ms |
5324 KB |
Output is correct |
13 |
Correct |
73 ms |
5188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
276 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
68 ms |
5180 KB |
Output is correct |
8 |
Correct |
3 ms |
380 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
68 ms |
5204 KB |
Output is correct |
11 |
Correct |
94 ms |
5104 KB |
Output is correct |
12 |
Correct |
66 ms |
5324 KB |
Output is correct |
13 |
Correct |
73 ms |
5188 KB |
Output is correct |
14 |
Correct |
131 ms |
5852 KB |
Output is correct |
15 |
Correct |
541 ms |
14476 KB |
Output is correct |
16 |
Correct |
97 ms |
5924 KB |
Output is correct |
17 |
Correct |
535 ms |
14416 KB |
Output is correct |
18 |
Correct |
396 ms |
14536 KB |
Output is correct |
19 |
Correct |
450 ms |
14660 KB |
Output is correct |
20 |
Correct |
563 ms |
14532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
66 ms |
3780 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
292 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
73 ms |
3548 KB |
Output is correct |
7 |
Correct |
68 ms |
4972 KB |
Output is correct |
8 |
Correct |
95 ms |
7084 KB |
Output is correct |
9 |
Correct |
105 ms |
7012 KB |
Output is correct |
10 |
Correct |
108 ms |
7012 KB |
Output is correct |
11 |
Correct |
96 ms |
6980 KB |
Output is correct |
12 |
Correct |
88 ms |
7036 KB |
Output is correct |
13 |
Correct |
90 ms |
6980 KB |
Output is correct |
14 |
Correct |
101 ms |
7012 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
276 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
3 ms |
332 KB |
Output is correct |
21 |
Correct |
68 ms |
5180 KB |
Output is correct |
22 |
Correct |
3 ms |
380 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
68 ms |
5204 KB |
Output is correct |
25 |
Correct |
94 ms |
5104 KB |
Output is correct |
26 |
Correct |
66 ms |
5324 KB |
Output is correct |
27 |
Correct |
73 ms |
5188 KB |
Output is correct |
28 |
Correct |
131 ms |
5852 KB |
Output is correct |
29 |
Correct |
541 ms |
14476 KB |
Output is correct |
30 |
Correct |
97 ms |
5924 KB |
Output is correct |
31 |
Correct |
535 ms |
14416 KB |
Output is correct |
32 |
Correct |
396 ms |
14536 KB |
Output is correct |
33 |
Correct |
450 ms |
14660 KB |
Output is correct |
34 |
Correct |
563 ms |
14532 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Incorrect |
66 ms |
3780 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |