//#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <random> #include <chrono> #include <ctime> #include<list> #include <numeric> #include<bitset> #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<functional> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<utility> #include<fstream> #include<sstream> #include<cmath> #include<stack> #include<assert.h> #include<unordered_map> #include<unordered_set> #include <array> #include <complex> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) typedef long long int LL; //typedef __int128 LLL; typedef long long LLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<string> VS; typedef long double ld; #define MAXN 203 #define MAXN2 MAXN*MAXN //#define MAXN 1006 const LL MOD = 1000000007; //const LL MOD = 998244353; //const LL INF = 2000000000000000001LL; //2e18 + 1 int n; LL tree[300005]; //return cf[idx] LL read(int idx) { LL sum = 0; while (idx > 0) { sum += tree[idx]; if (sum >= MOD) sum -= MOD; idx -= (idx & -idx); } return sum; } //add val to f[idx]. //MaxVal = Just max value of index void update(int idx, LL val) { while (idx <= 300000) { tree[idx] += val; if (tree[idx] >= MOD) tree[idx] -= MOD; idx += (idx & -idx); } } VI FindNextR(VPL& V) { VI ret(n); // (where am i, left border) priority_queue<PLL, VPL, greater<PLL>> Q; for (int i = n - 1; i >= 0; i--) { ret[i] = i; while (Q.size() && Q.top().second > V[i].first) { Q.pop(); } if (Q.size()) { ret[i] = Q.top().first; } Q.push({ i, V[i].first - V[i].second }); } return ret; } VI FindNextL(VPL& V) { VI ret(n); // (where am i, right border) priority_queue<PLL> Q; for (int i = 0; i < n; i++) { ret[i] = i; while (Q.size() && Q.top().second < V[i].first) { Q.pop(); } if (Q.size()) { ret[i] = Q.top().first; } Q.push({ i, V[i].first + V[i].second }); } return ret; } VI FindSpanL(VI& nxtR) { VI ret(n); for (int i = 0; i < n; i++) { ret[i] = i; } for (int i = 0; i < n; i++) { ret[nxtR[i]] = MIN(ret[nxtR[i]], ret[i]); } return ret; } LL dp[300005][2]; void solve(int ks) { scanf("%d", &n); // (pos, radius). VPL V(n); for (auto& p : V) { scanf("%lld %lld", &p.first, &p.second); } VI nxtR = FindNextR(V); VI nxtL = FindNextL(V); VI spanL = FindSpanL(nxtR); stack<int> active_ones; for (int i = 0; i < n; i++) { if (i == 0) { dp[i][0] = dp[i][1] = 1; update(0 + 1, 1); active_ones.push(0); continue; } // Compute dp[i][0]. int left_point = spanL[i]; if (left_point == 0) { // I cover till "0". dp[i][0] = 1; } else { // I have someone on my left. dp[i][0] = (dp[left_point - 1][0] + dp[left_point - 1][1]) % MOD; } // Compute dp[i][1]. if (nxtL[i] == i) { dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) % MOD; } else { // sum up (nxtL[i], i - 1). dp[i][1] = read(i - 1 + 1) - read(nxtL[i] - 1 + 1); if (dp[i][1] < 0) dp[i][1] += MOD; } // if there is a zero at (i), we should clean upto and including spanL[i]. while (active_ones.size() && active_ones.top() >= left_point) { int id = active_ones.top(); LL val = MOD - dp[id][1]; if (val == MOD) val = 0; update(id + 1, val); active_ones.pop(); } update(i + 1, dp[i][1]); active_ones.push(i); } LL ans = (dp[n - 1][0] + dp[n - 1][1]) % MOD; printf("%lld\n", ans); } void gen() { } int main() { double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contests\\F\\sample.in", "r", stdin); // freopen("out.out", "w", stdout); #endif gen(); if (0) { int T; scanf("%d", &T); //AIN(T, 1, 5000); for (int ks = 1; ks <= T; ks++) { solve(ks); //if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks); } } else { solve(0); } double end_time = clock(); fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <random> #include <chrono> #include <ctime> #include<list> #include <numeric> #include<bitset> #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<functional> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<utility> #include<fstream> #include<sstream> #include<cmath> #include<stack> #include<assert.h> #include<unordered_map> #include<unordered_set> #include <array> #include <complex> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) typedef long long int LL; //typedef __int128 LLL; typedef long long LLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<string> VS; typedef long double ld; #define MAXN 203 #define MAXN2 MAXN*MAXN //#define MAXN 1006 const LL MOD = 1000000007; //const LL MOD = 998244353; //const LL INF = 2000000000000000001LL; //2e18 + 1 int n; LL tree[300005]; //return cf[idx] LL read(int idx) { LL sum = 0; while (idx > 0) { sum += tree[idx]; if (sum >= MOD) sum -= MOD; idx -= (idx & -idx); } return sum; } //add val to f[idx]. //MaxVal = Just max value of index void update(int idx, LL val) { while (idx <= 300000) { tree[idx] += val; if (tree[idx] >= MOD) tree[idx] -= MOD; idx += (idx & -idx); } } VI FindNextR(VPL& V) { VI ret(n); // (where am i, left border) priority_queue<PLL, VPL, greater<PLL>> Q; for (int i = n - 1; i >= 0; i--) { ret[i] = i; while (Q.size() && Q.top().second > V[i].first) { Q.pop(); } if (Q.size()) { ret[i] = Q.top().first; } Q.push({ i, V[i].first - V[i].second }); } return ret; } VI FindNextL(VPL& V) { VI ret(n); // (where am i, right border) priority_queue<PLL> Q; for (int i = 0; i < n; i++) { ret[i] = i; while (Q.size() && Q.top().second < V[i].first) { Q.pop(); } if (Q.size()) { ret[i] = Q.top().first; } Q.push({ i, V[i].first + V[i].second }); } return ret; } VI FindSpanL(VI& nxtR) { VI ret(n); for (int i = 0; i < n; i++) { ret[i] = i; } for (int i = 0; i < n; i++) { ret[nxtR[i]] = MIN(ret[nxtR[i]], ret[i]); } return ret; } LL dp[300005][2]; void solve(int ks) { scanf("%d", &n); // (pos, radius). VPL V(n); for (auto& p : V) { scanf("%lld %lld", &p.first, &p.second); } VI nxtR = FindNextR(V); VI nxtL = FindNextL(V); VI spanL = FindSpanL(nxtR); stack<int> active_ones; for (int i = 0; i < n; i++) { if (i == 0) { dp[i][0] = dp[i][1] = 1; update(0 + 1, 1); active_ones.push(0); continue; } // Compute dp[i][0]. int left_point = spanL[i]; if (left_point == 0) { // I cover till "0". dp[i][0] = 1; } else { // I have someone on my left. dp[i][0] = (dp[left_point - 1][0] + dp[left_point - 1][1]) % MOD; } // Compute dp[i][1]. if (nxtL[i] == i) { dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) % MOD; } else { // sum up (nxtL[i], i - 1). dp[i][1] = read(i - 1 + 1) - read(nxtL[i] - 1 + 1); if (dp[i][1] < 0) dp[i][1] += MOD; } // if there is a zero at (i), we should clean upto and including spanL[i]. while (active_ones.size() && active_ones.top() >= left_point) { int id = active_ones.top(); LL val = MOD - dp[id][1]; if (val == MOD) val = 0; update(id + 1, val); active_ones.pop(); } update(i + 1, dp[i][1]); active_ones.push(i); } LL ans = (dp[n - 1][0] + dp[n - 1][1]) % MOD; printf("%lld\n", ans); } void gen() { } int main() { double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contests\\F\\sample.in", "r", stdin); // freopen("out.out", "w", stdout); #endif gen(); if (0) { int T; scanf("%d", &T); //AIN(T, 1, 5000); for (int ks = 1; ks <= T; ks++) { solve(ks); //if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks); } } else { solve(0); } double end_time = clock(); fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); return 0; } |