This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
// #define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5;
int t,n;
long long ans;
pii a[N];
map <int, int> mp;
void g(vector <int> v) {
sort(v.begin(),v.end());
int cnt = 0;
for (int i = 0 ; i < v.size(); i++) {
if (i == 0 || v[i] != v[i - 1]) cnt++, mp[v[i]] = cnt;
}
}
void comp() {
vector <int> v;
for (int i = 1; i <= n; i++) {
v.pb(a[i].f);
}
g(v);
for (int i = 1; i <= n; i++) {
a[i].f = mp[a[i].f];
}
v.clear();
for (int i = 1; i <= n; i++) {
v.pb(a[i].s);
}
g(v);
for (int i = 1; i <= n; i++) {
a[i].s = mp[a[i].s];
}
sort(a + 1, a + n + 1);
}
int tree[N];
void upd(int idx, int val){
assert(idx != 0);
for (int i = idx; i < N; i+=i&(-i)) {
tree[i] += val;
}
}
int getans(int idx) {
int pas = 0;
for (int i = idx; i > 0; i-=i&(-i)) {
pas += tree[i];
}
return pas;
}
void solve(int le, int ri) {
if (le == ri) return ;
int mid = (le + ri) / 2;
set <int> s;
s.insert(0);
vector <pii> vecle, vecri;
for (int i = mid + 1; i <= ri; i++) {
// patarebshi maximaluri
auto it = --s.lower_bound(a[i].s);
int x = (*it);
if (x + 1 <= a[i].s)
vecri.pb({x + 1, a[i].s});
s.insert(a[i].s);
}
s.clear();
s.insert(n + 5);
for (int i = mid; i >= le; i--) {
auto it = s.upper_bound(a[i].s);
int x = (*it);
if (a[i].s <= (x - 1))
vecle.pb({a[i].s, x - 1});
s.insert(a[i].s);
}
// ro ikvetebian masetebis raodenoba
sort(vecle.begin(), vecle.end());
sort(vecri.begin(), vecri.end());
/*for (pii sth : vecle) {
cout<<sth.f<<" "<<sth.s<<"\n";
}
for (pii sth : vecri) {
cout<<sth.f<<"-"<<sth.s<<"\n";
}
cout<<"\n";*/
int id = 0;
for (pii sth : vecle) {
while (id < vecri.size() && vecri[id].f <= sth.f) {
upd(vecri[id].s, 1);
id++;
}
ans += (getans(sth.s) - getans(sth.f - 1));
}
for (int i = 0; i < id; i++) {
upd(vecri[i].s, -1);
}
solve(le, mid);
solve(mid + 1, ri);
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>a[i].f>>a[i].s;
}
comp();
solve(1, n);
cout<<ans<<"\n";
}
Compilation message (stderr)
scarecrows.cpp: In function 'void g(std::vector<int>)':
scarecrows.cpp:16:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0 ; i < v.size(); i++) {
| ~~^~~~~~~~~~
scarecrows.cpp: In function 'void solve(int, int)':
scarecrows.cpp:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | while (id < vecri.size() && vecri[id].f <= sth.f) {
| ~~~^~~~~~~~~~~~~~
scarecrows.cpp: At global scope:
scarecrows.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |