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;
using ll = long long;
const int N = 5e5 + 10;
vector<int> g[N];
vector<int> order;
vector<bool> us;
void topsort(int s) {
us[s] = 1;
for(int to : g[s]) {
if(us[to]) continue;
topsort(to);
}
order.push_back(s);
}
void dfs(int s) {
us[s] = 1;
for(int to : g[s]) {
if(us[to]) continue;
dfs(to);
}
}
int n;
pair<ll, ll> pnts[N];
ll A[N], B[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> pnts[i].first >> pnts[i].second;
sort(pnts + 1, pnts + n + 1);
for(int i = 1; i <= n; i++)
A[i] = pnts[i].second - pnts[i].first,
B[i] = pnts[i].second + pnts[i].first;
vector<int> v;
for(int i = 1; i <= n; i++) {
while(!v.empty() && A[v.back()] <= A[i]) {
g[i].push_back(v.back());
v.pop_back();
}
v.push_back(i);
}
v.clear();
for(int i = n; i >= 1; i--) {
while(!v.empty() && B[v.back()] <= B[i]) {
g[i].push_back(v.back());
v.pop_back();
}
v.push_back(i);
}
us.assign(n + 1, false);
for(int i = 1; i <= n; i++) {
if(!us[i]) topsort(i);
}
reverse(order.begin(), order.end());
us.assign(n + 1, false);
int cmp = 0;
for(int c : order) {
if(!us[c]) dfs(c), cmp++;
}
cout << cmp;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |