#include #include #include #include using namespace std; using namespace atcoder; typedef long long ll; struct S{ll len,val = 0;}; struct F{ll x;}; S op(S l,S r){return S{l.len + r.len,l.val + r.val};} S e(){return S{0,0};} S mapping(F l,S r){return S{r.len,r.val + l.x*r.len};} F compo(F l,F r){return F{l.x + r.x};} F id(){return F{0};} ll a[200010],t[200010],d[200010],l[200010],r[200010]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int i,n; cin >> n; vector z; z.push_back(0); z.push_back(1); for(i=1;i<=n;i++){ cin >> a[i] >> t[i]; z.push_back(t[i]); z.push_back(a[i] + t[i]); } vector> b(n + 1),c(n + 1); int q; cin >> q; for(i=0;i> d[i] >> l[i] >> r[i]; l[i]--; z.push_back(d[i] + 1); b[l[i]].push_back(i); c[r[i]].push_back(i); } sort(z.begin(),z.end()); z.erase(unique(z.begin(),z.end()),z.end()); vector v; for(i=0;i seg(v); auto prod = [&](int l,int r){ int le = lower_bound(z.begin(),z.end(),l) - z.begin(); int ri = lower_bound(z.begin(),z.end(),r) - z.begin(); return seg.prod(le,ri).val; }; auto add = [&](int l,int r,ll x){ int le = lower_bound(z.begin(),z.end(),l) - z.begin(); int ri = lower_bound(z.begin(),z.end(),r) - z.begin(); seg.apply(le,ri,{x}); }; vector ans(q); for(i=0;i<=n;i++){ add(0,1,a[i]); if(i){ add(t[i],t[i] + a[i],-1); } for(int id:b[i]){ ans[id] -= prod(0,d[id] + 1); } for(int id:c[i]){ ans[id] += prod(0,d[id] + 1); } // for(int j=0;j