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>
typedef long long ll;
using namespace std;
const int INF = 1e9;
struct Seg{
int tree[400400], sz;
void init(int n){
sz = n;
fill(tree, tree+sz*2, INF);
}
void update(int p, int x){
for (tree[p+=sz]=x;p>1;p>>=1) tree[p>>1] = min(tree[p], tree[p^1]);
}
int query(int l, int r){
int ret = INF;
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret = min(ret, tree[l++]);
if (r&1) ret = min(ret, tree[--r]);
}
return ret;
}
}tree1, tree2;
pair<int, int> a[200200];
ll ans;
void compress(vector<int> &a){
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
}
int getidx(int x, vector<int> &a){
return lower_bound(a.begin(), a.end(), x) - a.begin();
}
void dnc(int l, int r){
if (l==r) return;
int m = (l+r)>>1;
vector<pair<int, int>> V;
for (int i=l;i<=r;i++) V.emplace_back(-a[i].second, i);
sort(V.begin(), V.end());
tree1.init(m-l+1);
tree2.init(r-l+1);
vector<int> st;
int cnt = 0;
for (auto &p:V){
int i = p.second;
if (i<=m){
int L = -tree1.query(i-l, m-l+1);
if (L==-INF) L = 0;
int mn = tree2.query(L, cnt+1);
ans += st.end() - lower_bound(st.begin(), st.end(), mn);
tree1.update(i-l, -cnt);
}
else{
tree2.update(cnt, i);
while(!st.empty() && st.back() > i) st.pop_back();
st.push_back(i);
}
cnt++;
}
dnc(l, m);
dnc(m+1, r);
}
int main(){
int n;
scanf("%d", &n);
vector<int> X, Y;
X.push_back(-INF);
Y.push_back(-INF);
for (int i=1;i<=n;i++){
scanf("%d %d", &a[i].first, &a[i].second);
X.push_back(a[i].first);
Y.push_back(a[i].second);
}
compress(X); compress(Y);
assert((int)X.size()==n+1);
assert((int)Y.size()==n+1);
for (int i=1;i<=n;i++){
a[i].first = getidx(a[i].first, X);
a[i].second = getidx(a[i].second, Y);
}
sort(a+1, a+n+1);
dnc(1, n);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
scarecrows.cpp: In function 'int main()':
scarecrows.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
scarecrows.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d %d", &a[i].first, &a[i].second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |