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
|
#include <algorithm> #include <cstdio> #include <cstring> #include <set>
template <typename T> void read(T &n) { char c; for (; (c = getchar()) < '0' || c > '9';) ; for (n = c - '0'; (c = getchar()) >= '0' && c <= '9'; (n *= 10) += c - '0') ; }
typedef long long ll; typedef int const ic;
static ic N = 100005; int numn, root[N << 1]; struct Pony { int s, m, r; } p[N]; struct Event { int t, p, b; } e[N]; struct Query { int t, l, r; bool operator<(Query const &a) const { return l < a.l; } } q[N]; struct SegmentTree { int child[2]; ll a, b; } node[N << 6]; std::set<Query> st;
void update(ic &root) { if (!root) return; node[root].a = node[node[root].child[0]].a + node[node[root].child[1]].a; node[root].b = node[node[root].child[0]].b + node[node[root].child[1]].b; }
void modify(int &root, ic &nl, ic &nr, ic &p, ic &a, ic &b, ic &flag) { if (p > nr || nl > p) return; if (flag) node[++numn] = node[root], root = numn; else if (!root) root = ++numn; if (nl == nr) { node[root].a = a, node[root].b = b; return; } ic mid = (nl + nr) >> 1; modify(node[root].child[0], nl, mid, p, a, b, flag); modify(node[root].child[1], mid + 1, nr, p, a, b, flag); update(root); }
ll query(ic &root, ic &nl, ic &nr, ic &t, ic &l, ic &r) { if (!root || l > nr || nl > r) return 0; if (l <= nl && nr <= r) return node[root].a * t + node[root].b; ic mid = (nl + nr) >> 1; return query(node[root].child[0], nl, mid, t, l, r) + query(node[root].child[1], mid + 1, nr, t, l, r); }
int main() { int n, m; read(n); for (int i = 0; i < n; ++i) read(p[i].s), read(p[i].m), read(p[i].r); read(m); for (int i = 0; i < m; ++i) read(q[i].t), read(q[i].l), read(q[i].r), --q[i].l, --q[i].r; for (int i = 0; i < n; ++i) modify(root[0], 0, n - 1, i, p[i].r, 0, 0), e[i] = (Event){p[i].r ? p[i].m / p[i].r + 1 : N, i, p[i].m}; std::sort(e, e + n, [](Event const &a, Event const &b) { return a.t < b.t; }); for (int i = 1, p = 0; i < N; ++i) { root[i] = root[i - 1]; for (; p < n && e[p].t == i; ++p) modify(root[i], 0, n - 1, e[p].p, 0, e[p].b, 1); } for (int i = 0; i < n; ++i) modify(root[N], 0, n - 1, i, p[i].r, p[i].s, 0), e[i] = (Event){p[i].r ? (p[i].m - p[i].s) / p[i].r + 1 : N, i, p[i].m}; std::sort(e, e + n, [](Event const &a, Event const &b) { return a.t < b.t; }); for (int i = 1, p = 0; i < N; ++i) { root[N + i] = root[N + i - 1]; for (; p < n && e[p].t == i; ++p) modify(root[N + i], 0, n - 1, e[p].p, 0, e[p].b, 1); } st.insert((Query){0, 0, n - 1}); for (int i = 0; i < m; ++i) { auto rec = st.lower_bound((Query){q[i].t, q[i].l, q[i].r}); if (rec == st.end()) --rec; for (; rec != st.begin() && rec->r >= q[i].l; --rec) ; if (rec->r < q[i].l) ++rec; auto tmp = rec; Query l = (Query){rec->t, rec->l, q[i].l - 1}; ll ans = 0; for (; rec != st.end() && rec->l <= q[i].r; ++rec) ans += rec->t ? query(root[std::min(q[i].t - rec->t, N - 1)], 0, n - 1, q[i].t - rec->t, std::max(rec->l, q[i].l), std::min(rec->r, q[i].r)) : query(root[std::min(q[i].t - rec->t, N - 1) + N], 0, n - 1, q[i].t - rec->t, std::max(rec->l, q[i].l), std::min(rec->r, q[i].r)); printf("%lld\n", ans), --rec; Query r = (Query){rec->t, q[i].r + 1, rec->r}; for (; tmp != st.end() && tmp->l <= q[i].r;) st.erase(tmp++); st.insert((Query){q[i].t, q[i].l, q[i].r}); if (l.l <= l.r) st.insert(l); if (r.l <= r.r) st.insert(r); } return 0; }
|