#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
scanf("%d", &n);
vector<int> x(n);
vector<int> y(n);
{
vector<pair<int, int>> ps;
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d %d", &x, &y);
ps.emplace_back(x, y);
}
sort(ps.begin(), ps.end());
for (int i = 0; i < n; ++i) {
x[i] = ps[i].first;
y[i] = ps[i].second;
}
}
ll ans = 0;
function<void(int, int)> solve = [&](int l, int r) {
if (l == r) {
return;
}
int md = (l + r) >> 1;
vector<tuple<int, int, int>> vals;
for (int i = l; i <= md; ++i) {
vals.emplace_back(y[i], x[i], -1);
}
for (int i = md + 1; i <= r; ++i) {
vals.emplace_back(y[i], x[i], +1);
}
sort(vals.begin(), vals.end());
vector<pair<int, int>> st1, st2;
auto Assert = [&](vector<pair<int, int>> ps) {
int sz = ps.size();
for (int i = 1; i < sz; ++i) {
assert(ps[i].first > ps[i - 1].first);
assert(ps[i].second < ps[i - 1].second);
}
};
for (auto v : vals) {
int y, x, tp;
tie(y, x, tp) = v;
if (tp == -1) {
while (st1.size() && x > st1.back().second) {
st1.pop_back();
}
st1.emplace_back(y, x);
} else {
while (st2.size() && x < st2.back().second) {
st2.pop_back();
}
int lim;
if (st2.empty()) lim = -1;
else lim = st2.back().first;
int fs = upper_bound(st1.begin(), st1.end(), make_pair(lim, 0)) - st1.begin();
ans += (st1.size()) - fs;
st2.emplace_back(y, x);
}
// Assert(st1);
// Assert(st2);
}
solve(l, md);
solve(md + 1, r);
};
solve(0, n - 1);
printf("%lld\n", ans);
}
Compilation message
scarecrows.cpp: In lambda function:
scarecrows.cpp:40:12: warning: variable 'Assert' set but not used [-Wunused-but-set-variable]
auto Assert = [&](vector<pair<int, int>> ps) {
^~~~~~
scarecrows.cpp: In function 'int main()':
scarecrows.cpp:9:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
scarecrows.cpp:16:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
236 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
504 KB |
Output is correct |
2 |
Correct |
11 ms |
504 KB |
Output is correct |
3 |
Correct |
10 ms |
708 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
10 ms |
636 KB |
Output is correct |
6 |
Correct |
11 ms |
632 KB |
Output is correct |
7 |
Correct |
11 ms |
632 KB |
Output is correct |
8 |
Correct |
8 ms |
632 KB |
Output is correct |
9 |
Correct |
10 ms |
604 KB |
Output is correct |
10 |
Correct |
10 ms |
632 KB |
Output is correct |
11 |
Correct |
11 ms |
632 KB |
Output is correct |
12 |
Correct |
8 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
261 ms |
8972 KB |
Output is correct |
2 |
Correct |
482 ms |
8004 KB |
Output is correct |
3 |
Correct |
390 ms |
12872 KB |
Output is correct |
4 |
Correct |
262 ms |
12812 KB |
Output is correct |
5 |
Correct |
412 ms |
12016 KB |
Output is correct |
6 |
Correct |
446 ms |
11920 KB |
Output is correct |
7 |
Correct |
464 ms |
11824 KB |
Output is correct |
8 |
Correct |
479 ms |
11844 KB |
Output is correct |
9 |
Correct |
259 ms |
12748 KB |
Output is correct |
10 |
Correct |
425 ms |
11844 KB |
Output is correct |
11 |
Correct |
455 ms |
11924 KB |
Output is correct |
12 |
Correct |
471 ms |
11888 KB |
Output is correct |
13 |
Correct |
484 ms |
11888 KB |
Output is correct |
14 |
Correct |
265 ms |
12740 KB |
Output is correct |
15 |
Correct |
454 ms |
11844 KB |
Output is correct |
16 |
Correct |
482 ms |
11972 KB |
Output is correct |
17 |
Correct |
278 ms |
7552 KB |
Output is correct |
18 |
Correct |
475 ms |
11976 KB |
Output is correct |
19 |
Correct |
470 ms |
11844 KB |
Output is correct |
20 |
Correct |
467 ms |
11916 KB |
Output is correct |