이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifdef imeimi
#include <vector>
void init(int k, std::vector<int> r);
int compare_plants(int x, int y);
#include <cstdio>
#include <cassert>
#include <vector>
#define n N
#define k K
static int n, k, q;
static std::vector<int> r;
static std:: vector<int> x;
static std:: vector<int> y;
static std:: vector<int> answer;
int main() {
assert(scanf("%d%d%d", &n, &k, &q) == 3);
r.resize(n);
answer.resize(q);
for (int i = 0; i < n; i++) {
int value;
assert(scanf("%d", &value) == 1);
r[i] = value;
}
x.resize(q);
y.resize(q);
for (int i = 0; i < q; i++) {
assert(scanf("%d%d", &x[i], &y[i]) == 2);
}
fclose(stdin);
init(k, r);
for (int i = 0; i < q; i++) {
answer[i] = compare_plants(x[i], y[i]);
}
for (int i = 0; i < q; i++) {
printf("%d\n", answer[i]);
}
fclose(stdout);
return 0;
}
#undef n
#undef k
#else
#include "plants.h"
#endif
#include <bits/stdc++.h>
using namespace std;
namespace S {
int seg[1 << 19], add[1 << 19];
void update(int i, int s, int e, int x, int y, int v) {
if (e < x || y < s) return;
if (x <= s && e <= y) {
seg[i] += v;
add[i] += v;
return;
}
int m = (s + e) / 2;
update(i + i, s, m, x, y, v);
update(i + i + 1, m + 1, e, x, y, v);
seg[i] = min(seg[i + i], seg[i + i + 1]) + add[i];
}
int find(int i, int s, int e, int a = 0) {
if (seg[i] + a) return 0;
if (s == e) return s;
a += add[i];
int m = (s + e) / 2;
int ret = find(i + i, s, m, a);
if (ret) return ret;
return find(i + i + 1, m + 1, e, a);
}
}
static int n, k;
static int sum[200001], val[200001];
void init(int k, vector<int> r) {
::k = k;
n = r.size();
for (int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1] + r[i - 1];
}
if (k + k > n) {
set<int> can, one;
auto add = [&](int x) {
auto it = can.insert(x).first;
if (int(can.size()) == 1) {
one.insert(x);
return;
}
auto it1 = it;
if (it1 == can.begin()) it1 = prev(can.end());
else --it1;
int d1 = (x - *it1 + n) % n;
auto it2 = next(it);
if (it2 == can.end()) it2 = can.begin();
int d2 = (*it2 - x + n) % n;
if (it1 == it2) {
one.erase(*it2);
if (d1 >= k) one.insert(x);
if (d2 >= k) one.insert(*it2);
return;
}
if (d1 + d2 >= k) one.erase(*it2);
if (d1 >= k) one.insert(x);
if (d2 >= k) one.insert(*it2);
};
auto del = [&](int x) {
auto it1 = can.upper_bound(x);
auto it2 = it1;
can.erase(x);
if (one.count(x)) one.erase(x);
if (can.empty()) return;
if (it1 == can.begin()) it1 = prev(can.end());
else --it1;
int d1 = (x - *it1 + n) % n;
if (it2 == can.end()) it2 = can.begin();
int d2 = (*it2 - x + n) % n;
if (it1 == it2) {
one.insert(*it2);
return;
}
if (d2 >= k) one.erase(*it2);
if (d1 + d2 >= k) one.insert(*it2);
};
for (int i = 1; i <= n; ++i) {
S::update(1, 1, n, i, i, r[i - 1]);
}
for (int v = n; v > 0; --v) {
int x;
while (x = S::find(1, 1, n)) {
add(x);
S::update(1, 1, n, x, x, 1e8);
}
x = *one.begin();
S::update(1, 1, n, x - k + 1, x, -1);
S::update(1, 1, n, n + x - k + 1, n + x, -1);
del(x);
val[x] = v;
}
}
}
int compare_plants(int x, int y) {
if (k == 2) {
int cnt = sum[y] - sum[x];
int max = y - x;
if (cnt == 0) return 1;
if (cnt == max) return -1;
cnt = sum[n] - cnt;
max = n - max;
if (cnt == 0) return -1;
if (cnt == max) return 1;
return 0;
}
if (val[x + 1] < val[y + 1]) return -1;
if (val[x + 1] > val[y + 1]) return 1;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:142:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
142 | while (x = S::find(1, 1, n)) {
| ~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |