// Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, m, a, b, x, y, teraz; long long res, res1 = 1; char slowo[5]; vector <int> l[500007]; vector <pair<int, int>> v; int byl[500007], morze, num[500007], poczatki[500007]; long long tab[500007], suma[500007]; void dfs(int nr) { // cout << "odpala sie dfs w " << nr << endl; byl[nr] = teraz; if(nr > a && nr <= a+b) { // cout << "doszedl do morza w " << nr << endl; morze = min(morze, nr); } for(int s : l[nr]) { if(byl[s] != teraz) dfs(s); } } int main() { scanf("%d %d %d %d", &n, &m, &a, &b); forr(i, m) { scanf("%d %s %d", &x, slowo, &y); l[x].PB(y); if(slowo[1] == '-') l[y].PB(x); } teraz++; FOR(i, 1, a) // <<< tu cos nie gra... { // cout << "probuje dla i = " << i << endl; teraz++; dfs(i); } int nn = 0; FOR(i, a+1, a+b) { num[i] = nn; if(byl[i] != 0) nn++; else res1 = (res1*2)%M; } FOR(i, 1, a) { // cout << "sprawdza " << i << endl; teraz++; morze = INF; dfs(i); int p = morze, k = morze; while(k < a+b && (byl[k+1] == teraz || byl[k+1] == 0)) k++; while(byl[k] == 0) k--; while(p > a+1 && (byl[p-1] == teraz || byl[p-1] == 0)) p--; if(p == a+1 && k < a+b && (byl[a+b] == teraz || byl[a+b] == 0)) p = a+b; while(p > a+1 && (byl[p-1] == teraz || byl[p-1] == 0)) p--; while(p <= a+b && byl[p] == 0) p++; if(p == a+b+1) p = a+1; while(byl[p] == 0) p++; // cout << "przedzial dla " << i << " to od " << p << " do " << k; p = num[p]; k = num[k]; // cout << ", po przenumerowaniu od " << p << " do " << k << endl; v.PB(MP(p, k)); } FOR(start, 0, nn-1) { forr(i, nn+3) { tab[i] = suma[i] = 0; poczatki[i] = -1; } // cout << "startuje w " << start << endl; for(auto el : v) { int p = el.first; int k = el.second; if(!(k >= start && (p <= start || p > k))) { // cout << "doklada przedzial od " << p << " do " << (k>=p ? k : nn-1) << endl; if(k >= p) poczatki[k] = max(poczatki[k], p); else poczatki[nn-1] = max(poczatki[nn-1], p); } } FOR(i, 1, nn-1) poczatki[i] = max(poczatki[i], poczatki[i-1]); // forr(i, nn) // cout << "poczatki[" << i << "] = " << poczatki[i] << endl; if(start > 0 && poczatki[start-1] != -1) { // cout << "wyskakuje" << endl; continue; } tab[start] = 1; suma[start] = 1; FOR(i, start+1, nn) { tab[i] = (suma[i-1] - (poczatki[i-1] <= 0 ? 0 : suma[poczatki[i-1]-1]) + M) % M; suma[i] = (suma[i-1] + tab[i]) % M; // cout << "tab[" << i << "] = " << tab[i] << endl; } res = (res + tab[nn]) % M; } printf("%lld\n", (res * res1)%M); 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 | // Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, m, a, b, x, y, teraz; long long res, res1 = 1; char slowo[5]; vector <int> l[500007]; vector <pair<int, int>> v; int byl[500007], morze, num[500007], poczatki[500007]; long long tab[500007], suma[500007]; void dfs(int nr) { // cout << "odpala sie dfs w " << nr << endl; byl[nr] = teraz; if(nr > a && nr <= a+b) { // cout << "doszedl do morza w " << nr << endl; morze = min(morze, nr); } for(int s : l[nr]) { if(byl[s] != teraz) dfs(s); } } int main() { scanf("%d %d %d %d", &n, &m, &a, &b); forr(i, m) { scanf("%d %s %d", &x, slowo, &y); l[x].PB(y); if(slowo[1] == '-') l[y].PB(x); } teraz++; FOR(i, 1, a) // <<< tu cos nie gra... { // cout << "probuje dla i = " << i << endl; teraz++; dfs(i); } int nn = 0; FOR(i, a+1, a+b) { num[i] = nn; if(byl[i] != 0) nn++; else res1 = (res1*2)%M; } FOR(i, 1, a) { // cout << "sprawdza " << i << endl; teraz++; morze = INF; dfs(i); int p = morze, k = morze; while(k < a+b && (byl[k+1] == teraz || byl[k+1] == 0)) k++; while(byl[k] == 0) k--; while(p > a+1 && (byl[p-1] == teraz || byl[p-1] == 0)) p--; if(p == a+1 && k < a+b && (byl[a+b] == teraz || byl[a+b] == 0)) p = a+b; while(p > a+1 && (byl[p-1] == teraz || byl[p-1] == 0)) p--; while(p <= a+b && byl[p] == 0) p++; if(p == a+b+1) p = a+1; while(byl[p] == 0) p++; // cout << "przedzial dla " << i << " to od " << p << " do " << k; p = num[p]; k = num[k]; // cout << ", po przenumerowaniu od " << p << " do " << k << endl; v.PB(MP(p, k)); } FOR(start, 0, nn-1) { forr(i, nn+3) { tab[i] = suma[i] = 0; poczatki[i] = -1; } // cout << "startuje w " << start << endl; for(auto el : v) { int p = el.first; int k = el.second; if(!(k >= start && (p <= start || p > k))) { // cout << "doklada przedzial od " << p << " do " << (k>=p ? k : nn-1) << endl; if(k >= p) poczatki[k] = max(poczatki[k], p); else poczatki[nn-1] = max(poczatki[nn-1], p); } } FOR(i, 1, nn-1) poczatki[i] = max(poczatki[i], poczatki[i-1]); // forr(i, nn) // cout << "poczatki[" << i << "] = " << poczatki[i] << endl; if(start > 0 && poczatki[start-1] != -1) { // cout << "wyskakuje" << endl; continue; } tab[start] = 1; suma[start] = 1; FOR(i, start+1, nn) { tab[i] = (suma[i-1] - (poczatki[i-1] <= 0 ? 0 : suma[poczatki[i-1]-1]) + M) % M; suma[i] = (suma[i-1] + tab[i]) % M; // cout << "tab[" << i << "] = " << tab[i] << endl; } res = (res + tab[nn]) % M; } printf("%lld\n", (res * res1)%M); return 0; } |