#include #include using namespace std; using namespace atcoder; typedef long long ll; typedef pair P; using S = ll; S op(S a, S b) { return a + b; } S e() { return 0; } struct query { int d, l, r; int id; bool operator<(const query &q) const { return (d != q.d ? d < q.d : id < q.id); } }; ll a[200005]; int t[200005]; P p[400005]; query qr[200005]; ll ans[200005]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> t[i]; p[i] = P(t[i] - 1, i); p[i + n] = P(t[i] + a[i] - 1, i + n); } sort(p, p + n * 2); segtree asum(n), start(n), dcount(n); for (int i = 0; i < n; i++) { asum.set(i, a[i]); start.set(i, 0); dcount.set(i, 0); } int q; cin >> q; for (int i = 0; i < q; i++) { cin >> qr[i].d >> qr[i].l >> qr[i].r; qr[i].l--; qr[i].id = i; } sort(qr, qr + q); int j = 0; for (int i = 0; i < q; i++) { while (j < n * 2 && p[j].first < qr[i].d) { if (p[j].second < n) { start.set(p[j].second, p[j].first); dcount.set(p[j].second, 1); } else { asum.set(p[j].second - n, 0); start.set(p[j].second - n, 0); dcount.set(p[j].second - n, 0); } j++; } int l = qr[i].l, r = qr[i].r; ans[qr[i].id] = asum.prod(l, r) - (qr[i].d * dcount.prod(l, r) - start.prod(l, r)); } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } }