/* -*- coding: utf-8 -*- * * 2101.cc: No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_QN = 200000; const int INF = 1 << 30; const long long LINF = 1LL << 60; /* typedef */ typedef long long ll; typedef vector vi; typedef queue qi; typedef pair pii; typedef pair pll; struct Query { int d, l, r, i; Query() {} void read(int _i) { scanf("%d%d%d", &d, &l, &r), l--, i = _i; } bool operator<(const Query &q) const { return d < q.d; } }; template struct SegTreeOp { int e2; vector nodes; T defv, (*fop)(T,T); SegTreeOp() {} void init(int n, T _defv, T (*_fop)(T,T)) { defv = _defv, fop = _fop; for (e2 = 1; e2 < n; e2 <<= 1); nodes.assign(e2 * 2, defv); } T &geti(int i) { return nodes[e2 - 1 + i]; } void seti(int i, T v) { geti(i) = v; } void setall() { for (int j = e2 - 2; j >= 0; j--) nodes[j] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]); } void set(int i, T v) { int j = e2 - 1 + i; nodes[j] = v; while (j > 0) { j = (j - 1) / 2; nodes[j] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]); } } T op_range(int r0, int r1, int k, int i0, int i1) { if (r1 <= i0 || i1 <= r0) return defv; if (r0 <= i0 && i1 <= r1) return nodes[k]; int im = (i0 + i1) / 2; T v0 = op_range(r0, r1, k * 2 + 1, i0, im); T v1 = op_range(r0, r1, k * 2 + 2, im, i1); return fop(v0, v1); } T op_range(int r0, int r1) { return op_range(r0, r1, 0, 0, e2); } }; /* global variables */ int as[MAX_N], ts[MAX_N], ds[MAX_QN]; Query qs[MAX_QN]; vi pvs[MAX_QN], rvs[MAX_QN]; SegTreeOp st; ll xs[MAX_QN]; /* subroutines */ pll op_add(pll a, pll b) { return pll(a.first + b.first, a.second + b.second); } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", as + i, ts + i); int qn; scanf("%d", &qn); for (int i = 0; i < qn; i++) qs[i].read(i); sort(qs, qs + qn); for (int i = 0; i < qn; i++) ds[i] = qs[i].d; for (int i = 0; i < n; i++) if (as[i] > 0) { int k = lower_bound(ds, ds + qn, ts[i]) - ds; if (k < qn) { pvs[k].push_back(i); int c = ts[i] + as[i] - 1; int k1 = lower_bound(ds, ds + qn, c) - ds; if (k1 < qn) rvs[k1].push_back(i); } } st.init(n, pll(0, 0), op_add); for (int i = 0; i < n; i++) st.seti(i, pll(0, as[i])); st.setall(); for (int i = 0; i < qn; i++) { for (auto u: pvs[i]) st.set(u, pll(-1, ts[u] + as[u] - 1)); for (auto u: rvs[i]) st.set(u, pll(0, 0)); if (false) { printf("%d:", ds[i]); for (int j = 0; j < n; j++) { pll p = st.geti(j); printf(" %lld", p.first * ds[i] + p.second); } putchar('\n'); } pll p = st.op_range(qs[i].l, qs[i].r); ll v = p.first * ds[i] + p.second; xs[qs[i].i] = v; } for (int i = 0; i < qn; i++) printf("%lld\n", xs[i]); return 0; }