#include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef long long LL; typedef vector<int> VI; typedef vector<VI> VVI; const int mod = 1000000007; int l[500000], r[500000]; VVI g; int been[500000]; int c; int pos(const pair<bool, int>& ee) { return ee.FT ? r[ee.SD] : l[ee.SD]; } bool comp(const pair<bool, int>& e1, const pair<bool, int>& e2) { return pos(e1) < pos(e2); } void go(int v, int t) { if (been[v] == t) return; been[v] = t; ++c; FORE(it,g[v]) go(*it, t); } inline LL MODL(LL a, LL b) { return a >= 0 ? a % b : b + ((a + 1) % b) - 1; } LL EXTEUC(LL a, LL b, LL& x, LL& y) { x = 1; y = 0; LL x1 = 0, y1 = 1; while (b != 0) { LL t = b; LL q = a / b; b = a % b; a = t; t = x1; x1 = x - q * x1; x = t; t = y1; y1 = y - q * y1; y = t; } return a; } LL REVM(LL a, LL m) { LL x, y; EXTEUC(m, a, x, y); return MODL(y, m); } int main() { INT(n); REP(i,n) scanf("%d%d", &l[i], &r[i]); vector<pair<bool, int> > e; REP(i,n) { e.PB(MP(false, i)); e.PB(MP(true, i)); } sort(ALL(e), comp); g.resize(n); set<int> s; FORE(it,e) { if (it->FT) { VAR(jt,s.find(-it->SD)); VAR(kt,jt); ++kt; if (kt != s.end()) g[-*kt].PB(it->SD); s.erase(jt); } else { VAR(jt,s.insert(-it->SD).FT); ++jt; if (jt != s.end()) g[-*jt].PB(it->SD); } } int res = 0; REP(i,n) { c = 0; go(i, i + 1); res = (res + REVM(c, mod)) % mod; } printf("%d\n", res); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | #include <algorithm> #include <cstdio> #include <set> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef long long LL; typedef vector<int> VI; typedef vector<VI> VVI; const int mod = 1000000007; int l[500000], r[500000]; VVI g; int been[500000]; int c; int pos(const pair<bool, int>& ee) { return ee.FT ? r[ee.SD] : l[ee.SD]; } bool comp(const pair<bool, int>& e1, const pair<bool, int>& e2) { return pos(e1) < pos(e2); } void go(int v, int t) { if (been[v] == t) return; been[v] = t; ++c; FORE(it,g[v]) go(*it, t); } inline LL MODL(LL a, LL b) { return a >= 0 ? a % b : b + ((a + 1) % b) - 1; } LL EXTEUC(LL a, LL b, LL& x, LL& y) { x = 1; y = 0; LL x1 = 0, y1 = 1; while (b != 0) { LL t = b; LL q = a / b; b = a % b; a = t; t = x1; x1 = x - q * x1; x = t; t = y1; y1 = y - q * y1; y = t; } return a; } LL REVM(LL a, LL m) { LL x, y; EXTEUC(m, a, x, y); return MODL(y, m); } int main() { INT(n); REP(i,n) scanf("%d%d", &l[i], &r[i]); vector<pair<bool, int> > e; REP(i,n) { e.PB(MP(false, i)); e.PB(MP(true, i)); } sort(ALL(e), comp); g.resize(n); set<int> s; FORE(it,e) { if (it->FT) { VAR(jt,s.find(-it->SD)); VAR(kt,jt); ++kt; if (kt != s.end()) g[-*kt].PB(it->SD); s.erase(jt); } else { VAR(jt,s.insert(-it->SD).FT); ++jt; if (jt != s.end()) g[-*jt].PB(it->SD); } } int res = 0; REP(i,n) { c = 0; go(i, i + 1); res = (res + REVM(c, mod)) % mod; } printf("%d\n", res); } |