#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; const long long mod = 1000000007; int IdCount = 0; struct Nodeqwe { set<Nodeqwe*> To; long long CombinationSum = -1; Nodeqwe* Bucket; bool Visited = false; int Id = IdCount++; }; struct Mine { int Position; int Range; vector<Mine*> To; vector<Mine*> From; Nodeqwe* Node = NULL; int PostOrder = -1; bool Visited = false; }; int PostOrderCount = 0; vector<Mine*> Mines; vector<Nodeqwe*> Nodes; class MineComp { public: bool operator()(const Mine* a, const int b) const { return a->Position < b; } bool operator()(const int a, const Mine* b) const { return a < b->Position; } }; bool mineCompByPost(Mine* a, Mine* b) { return a->PostOrder > b->PostOrder; } void postOrderInsert(Mine* m) { if (m->Visited) return; m->Visited = true; for (vector<Mine*>::iterator w = m->To.begin(); w != m->To.end();w++) { postOrderInsert(*w); } m->PostOrder = PostOrderCount++; return; } bool findGropu(Mine* m, Nodeqwe* g) { if (m->Node != NULL) return false; m->Node = g; for (vector<Mine*>::iterator w = m->From.begin(); w != m->From.end();w++) { findGropu(*w, g); } return true; } Nodeqwe* findBucket(Nodeqwe* n) { if (n->Bucket == n) return n; return n->Bucket = findBucket(n->Bucket); } void foo(Nodeqwe* n) { if (n->Visited) return; n->Visited = true; n->Bucket = n; for (set<Nodeqwe*>::iterator w = n->To.begin(); w != n->To.end();w++) { foo(*w); findBucket(*w)->Bucket = n; } n->CombinationSum = 1; for (set<Nodeqwe*>::iterator w = n->To.begin(); w != n->To.end();w++) { n->CombinationSum *= (*w)->CombinationSum; n->CombinationSum %= mod; } n->CombinationSum++; } int main() { int n; cin >> n; Mines.resize(n); for (int i = 0;i < n;i++) { int a, b; cin >> a >> b; Mine* m = new Mine(); m->Position = a; m->Range = b; Mines[i] = m; } vector<Mine*>::iterator beg, end, it, fbeg, fend, fit; it = beg = Mines.begin(); end = Mines.end(); MineComp cmp; for (;it != end;it++) { Mine* mc = *it; fit = fbeg = lower_bound(beg, end, mc->Position - mc->Range, cmp); fend = upper_bound(beg, end, mc->Position + mc->Range, cmp); for (;fit != fend;fit++) { if (fit == it) continue; mc->To.push_back(*fit); (*fit)->From.push_back(mc); } } for (it = beg;it != end;it++) { postOrderInsert(*it); } sort(Mines.begin(), Mines.end(), mineCompByPost); Nodeqwe* node = new Nodeqwe(); for (it = Mines.begin();it != Mines.end();it++) { if (findGropu(*it, node)) { Nodes.push_back(node); node = new Nodeqwe(); } } for (it = Mines.begin();it != Mines.end();it++) { for (vector<Mine*>::iterator to = (*it)->From.begin();to != (*it)->From.end();to++) { if ((*it)->Node == (*to)->Node) continue; (*it)->Node->To.insert((*to)->Node); } } for (vector<Nodeqwe*>::iterator it = Nodes.begin();it != Nodes.end();it++) { foo(*it); } long long acc = 1; for (vector<Nodeqwe*>::iterator it = Nodes.begin();it != Nodes.end();it++) { if ((*it)->Bucket != *it) continue; acc *= (*it)->CombinationSum; acc %= mod; } cout << acc; return 0; }
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; const long long mod = 1000000007; int IdCount = 0; struct Nodeqwe { set<Nodeqwe*> To; long long CombinationSum = -1; Nodeqwe* Bucket; bool Visited = false; int Id = IdCount++; }; struct Mine { int Position; int Range; vector<Mine*> To; vector<Mine*> From; Nodeqwe* Node = NULL; int PostOrder = -1; bool Visited = false; }; int PostOrderCount = 0; vector<Mine*> Mines; vector<Nodeqwe*> Nodes; class MineComp { public: bool operator()(const Mine* a, const int b) const { return a->Position < b; } bool operator()(const int a, const Mine* b) const { return a < b->Position; } }; bool mineCompByPost(Mine* a, Mine* b) { return a->PostOrder > b->PostOrder; } void postOrderInsert(Mine* m) { if (m->Visited) return; m->Visited = true; for (vector<Mine*>::iterator w = m->To.begin(); w != m->To.end();w++) { postOrderInsert(*w); } m->PostOrder = PostOrderCount++; return; } bool findGropu(Mine* m, Nodeqwe* g) { if (m->Node != NULL) return false; m->Node = g; for (vector<Mine*>::iterator w = m->From.begin(); w != m->From.end();w++) { findGropu(*w, g); } return true; } Nodeqwe* findBucket(Nodeqwe* n) { if (n->Bucket == n) return n; return n->Bucket = findBucket(n->Bucket); } void foo(Nodeqwe* n) { if (n->Visited) return; n->Visited = true; n->Bucket = n; for (set<Nodeqwe*>::iterator w = n->To.begin(); w != n->To.end();w++) { foo(*w); findBucket(*w)->Bucket = n; } n->CombinationSum = 1; for (set<Nodeqwe*>::iterator w = n->To.begin(); w != n->To.end();w++) { n->CombinationSum *= (*w)->CombinationSum; n->CombinationSum %= mod; } n->CombinationSum++; } int main() { int n; cin >> n; Mines.resize(n); for (int i = 0;i < n;i++) { int a, b; cin >> a >> b; Mine* m = new Mine(); m->Position = a; m->Range = b; Mines[i] = m; } vector<Mine*>::iterator beg, end, it, fbeg, fend, fit; it = beg = Mines.begin(); end = Mines.end(); MineComp cmp; for (;it != end;it++) { Mine* mc = *it; fit = fbeg = lower_bound(beg, end, mc->Position - mc->Range, cmp); fend = upper_bound(beg, end, mc->Position + mc->Range, cmp); for (;fit != fend;fit++) { if (fit == it) continue; mc->To.push_back(*fit); (*fit)->From.push_back(mc); } } for (it = beg;it != end;it++) { postOrderInsert(*it); } sort(Mines.begin(), Mines.end(), mineCompByPost); Nodeqwe* node = new Nodeqwe(); for (it = Mines.begin();it != Mines.end();it++) { if (findGropu(*it, node)) { Nodes.push_back(node); node = new Nodeqwe(); } } for (it = Mines.begin();it != Mines.end();it++) { for (vector<Mine*>::iterator to = (*it)->From.begin();to != (*it)->From.end();to++) { if ((*it)->Node == (*to)->Node) continue; (*it)->Node->To.insert((*to)->Node); } } for (vector<Nodeqwe*>::iterator it = Nodes.begin();it != Nodes.end();it++) { foo(*it); } long long acc = 1; for (vector<Nodeqwe*>::iterator it = Nodes.begin();it != Nodes.end();it++) { if ((*it)->Bucket != *it) continue; acc *= (*it)->CombinationSum; acc %= mod; } cout << acc; return 0; } |