区间合并:
#include <bits/stdc++.h> using namespace std; typedef pair <int,int> pii; vector<pii>pos; void merge(vector<pii>&pos) { vector <pii>ans; sort(pos.begin(),pos.end()); int st=-1e9+10,ed=-1e9+10; for (auto ver:pos) { if (ed<ver.first) { if (st!=-1e9+10)ans.push_back({st,ed}); st=ver.first,ed=ver.second; } else { ed=max(ed,ver.second); } } if (st!=-1e9+10)ans.push_back({st,ed}); pos=ans; } int main() { int n; cin>>n; while (n--) { int l,r; cin>>l>>r; pos.push_back({l,r}); } merge(pos); cout<<pos.size()<<endl; //for (auto ver:pos)cout<<ver.first<<' '<<ver.second<<endl; return 0; }