#include <algorithm> #include <cstdio> #include <stack> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; const int mod = 1000000007; LL a[300000]; LL r[300000]; int range[300000]; int killer[300000]; int with[300000]; int without[300000]; int main() { INT(n); REP(i,n) scanf("%lld%lld", &a[i], &r[i]); stack<int> s; REP(i,n) { int lr = i; while (!s.empty() && a[i] - a[s.top()] <= r[i]) { lr = min(lr, range[s.top()]); s.pop(); } range[i] = lr; s.push(i); } while (!s.empty()) s.pop(); REP(i,n) { while (!s.empty() && a[s.top()] + r[s.top()] < a[i]) s.pop(); killer[i] = !s.empty() ? s.top() : -1; s.push(i); } with[0] = 0; without[0] = 1; REP(i,n) { with[i + 1] = (with[range[i]] + without[range[i]]) % mod; without[i + 1] = ((with[i] + without[i]) % mod + mod - with[killer[i] + 1]) % mod; } printf("%d\n", (with[n] + without[n]) % mod); }
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 | #include <algorithm> #include <cstdio> #include <stack> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; const int mod = 1000000007; LL a[300000]; LL r[300000]; int range[300000]; int killer[300000]; int with[300000]; int without[300000]; int main() { INT(n); REP(i,n) scanf("%lld%lld", &a[i], &r[i]); stack<int> s; REP(i,n) { int lr = i; while (!s.empty() && a[i] - a[s.top()] <= r[i]) { lr = min(lr, range[s.top()]); s.pop(); } range[i] = lr; s.push(i); } while (!s.empty()) s.pop(); REP(i,n) { while (!s.empty() && a[s.top()] + r[s.top()] < a[i]) s.pop(); killer[i] = !s.empty() ? s.top() : -1; s.push(i); } with[0] = 0; without[0] = 1; REP(i,n) { with[i + 1] = (with[range[i]] + without[range[i]]) % mod; without[i + 1] = ((with[i] + without[i]) % mod + mod - with[killer[i] + 1]) % mod; } printf("%d\n", (with[n] + without[n]) % mod); } |