#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MN = 200010;
int N;
pii P[MN];
vector<pii> ord;
struct BIT {
vector<vector<pii> > tree;
void init() {
tree = vector<vector<pii> >(4 * N);
}
void upd(int idx, pii v, int l, int r, int n) {
if(idx < l || r < idx) return;
while(tree[n].size() && tree[n].back().second >= v.second) tree[n].pop_back();
tree[n].push_back(v);
if(l == r) return;
int m = (l + r)>>1;
upd(idx, v, l, m, 2*n);
upd(idx, v, m + 1, r, 2*n + 1);
}
int quer(int a, int b, int &x, int l, int r, int n) {
if(b < l || r < a) return 0;
if(a <= l && r <= b) {
int s = 0, e = (int)tree[n].size() - 1, p = tree[n].size();
while(s <= e) {
int m = (s + e)>>1;
if(tree[n][m].first < x) {
p = m;
e = m - 1;
}
else s = m + 1;
}
if(p != tree[n].size()) x = min(x, tree[n].back().first);
return (int)tree[n].size() - p;
}
int m = (l + r)>>1;
int L = quer(a, b, x, l, m, 2*n);
int R = quer(a, b, x, m + 1, r, 2*n + 1);
return L + R;
}
} bit;
int main() {
scanf("%d", &N);
for(int i = 0; i < N; i++) {
scanf("%d %d", &P[i].first, &P[i].second);
ord.push_back({ P[i].second, i });
}
sort(ord.begin(), ord.end());
for(int i = 0; i < N; i++) P[ ord[i].second ].second = i;
sort(P, P + N);
ll ans = 0;
bit.init();
for(int i = N - 1; i >= 0; i--) {
int x = 2e9;
ans += bit.quer(P[i].second, N - 1, x, 0, N - 1, 1);
bit.upd(P[i].second, P[i], 0, N - 1, 1);
}
printf("%lld", ans);
}
Compilation message
scarecrows.cpp: In member function 'int BIT::quer(int, int, int&, int, int, int)':
scarecrows.cpp:41:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p != tree[n].size()) x = min(x, tree[n].back().first);
~~^~~~~~~~~~~~~~~~~
scarecrows.cpp: In function 'int main()':
scarecrows.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
scarecrows.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &P[i].first, &P[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
560 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
696 KB |
Output is correct |
6 |
Correct |
2 ms |
696 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2044 KB |
Output is correct |
2 |
Correct |
9 ms |
2044 KB |
Output is correct |
3 |
Correct |
8 ms |
2044 KB |
Output is correct |
4 |
Correct |
7 ms |
2044 KB |
Output is correct |
5 |
Correct |
8 ms |
2044 KB |
Output is correct |
6 |
Correct |
9 ms |
2044 KB |
Output is correct |
7 |
Correct |
9 ms |
2044 KB |
Output is correct |
8 |
Correct |
8 ms |
2044 KB |
Output is correct |
9 |
Correct |
9 ms |
2044 KB |
Output is correct |
10 |
Correct |
10 ms |
2044 KB |
Output is correct |
11 |
Correct |
10 ms |
2044 KB |
Output is correct |
12 |
Correct |
8 ms |
2044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
288 ms |
64744 KB |
Output is correct |
2 |
Correct |
614 ms |
64744 KB |
Output is correct |
3 |
Correct |
331 ms |
64744 KB |
Output is correct |
4 |
Correct |
240 ms |
64744 KB |
Output is correct |
5 |
Correct |
333 ms |
64744 KB |
Output is correct |
6 |
Correct |
453 ms |
64744 KB |
Output is correct |
7 |
Correct |
513 ms |
64744 KB |
Output is correct |
8 |
Correct |
552 ms |
64744 KB |
Output is correct |
9 |
Correct |
302 ms |
65512 KB |
Output is correct |
10 |
Correct |
427 ms |
65512 KB |
Output is correct |
11 |
Correct |
479 ms |
65512 KB |
Output is correct |
12 |
Correct |
550 ms |
65512 KB |
Output is correct |
13 |
Correct |
587 ms |
65512 KB |
Output is correct |
14 |
Correct |
320 ms |
65512 KB |
Output is correct |
15 |
Correct |
487 ms |
65512 KB |
Output is correct |
16 |
Correct |
569 ms |
65512 KB |
Output is correct |
17 |
Correct |
274 ms |
65512 KB |
Output is correct |
18 |
Correct |
462 ms |
65512 KB |
Output is correct |
19 |
Correct |
412 ms |
65512 KB |
Output is correct |
20 |
Correct |
422 ms |
65512 KB |
Output is correct |