#include <cstdio>
#include <memory.h>
#include <cstring>
#include <vector>
#include <deque>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <functional>
#include <iostream>
#include <set>
#include <list>
#include <map>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
const ll MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
priority_queue<int, vector<int>, greater<int> > pq;
vector<int> v;
pii vec[200000];
pii temp[200000];
ll ans = 0;
void go(int le, int ri) {
int mid = (le + ri) / 2;
if (le < ri) { go(le, mid); go(mid + 1, ri); }
int pt, l = le;
vector<pii> a, b;
for (int i = mid + 1; i <= ri; i++) {
while (!b.empty() && b.back().second > vec[i].second) b.pop_back();
if (b.empty()) pt = -1;
else pt = b.back().first;
b.push_back(vec[i]);
while (l <= mid && vec[l].first < vec[i].first) {
while (!a.empty() && a.back().second < vec[l].second) a.pop_back();
a.push_back(vec[l++]);
}
int t = upper_bound(all(a), pii(pt, -1)) - a.begin();
ans += sz(a) - t;
}
merge(vec + le, vec + mid + 1, vec + mid + 1, vec + ri + 1, temp);
memcpy(vec + le, temp, sizeof(pii) * (ri - le + 1));
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d", &vec[i].first, &vec[i].second);
swap(vec[i].first, vec[i].second);
}
sort(vec, vec + n, [](pii v1, pii v2) { return v1.second < v2.second; });
go(0, n - 1);
printf("%lld", ans);
}
Compilation message
scarecrows.cpp: In function 'int main()':
scarecrows.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
scarecrows.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &vec[i].first, &vec[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
8 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
472 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
8 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
4460 KB |
Output is correct |
2 |
Correct |
210 ms |
3560 KB |
Output is correct |
3 |
Correct |
152 ms |
4600 KB |
Output is correct |
4 |
Correct |
146 ms |
4508 KB |
Output is correct |
5 |
Correct |
176 ms |
3752 KB |
Output is correct |
6 |
Correct |
171 ms |
3576 KB |
Output is correct |
7 |
Correct |
186 ms |
3620 KB |
Output is correct |
8 |
Correct |
208 ms |
3652 KB |
Output is correct |
9 |
Correct |
119 ms |
3596 KB |
Output is correct |
10 |
Correct |
151 ms |
3896 KB |
Output is correct |
11 |
Correct |
195 ms |
3744 KB |
Output is correct |
12 |
Correct |
203 ms |
3576 KB |
Output is correct |
13 |
Correct |
227 ms |
3584 KB |
Output is correct |
14 |
Correct |
111 ms |
3576 KB |
Output is correct |
15 |
Correct |
181 ms |
3576 KB |
Output is correct |
16 |
Correct |
200 ms |
3608 KB |
Output is correct |
17 |
Correct |
121 ms |
2652 KB |
Output is correct |
18 |
Correct |
211 ms |
3676 KB |
Output is correct |
19 |
Correct |
221 ms |
3624 KB |
Output is correct |
20 |
Correct |
193 ms |
3696 KB |
Output is correct |