#include #define REP(A,B) for(int (A)=0;(A)<(B);(A)++) #define ll long long #define TS (1<<18) #define pb push_back using namespace std; vector T; int V[111111]; typedef struct { ll mini, maxi, sum; } ND; ND tree[2*TS+2]; void upd(int idx, int L, int R, int P, ll VV) { // printf("U %d %d %d %d %lld\n", idx, L, R, P, VV); if(L > P || R < P) return; if(L == R) { tree[idx] = (ND){min(tree[idx].mini, VV), max(tree[idx].maxi, VV), tree[idx].sum+VV}; return; } int mid = (L+R)/2; upd(2*idx, L, mid, P, VV); upd(2*idx+1, mid+1, R, P, VV); tree[idx].mini = min(tree[idx].mini, tree[idx*2].mini); tree[idx].maxi = max(tree[idx].maxi, tree[idx*2].maxi); tree[idx].mini = min(tree[idx].mini, tree[idx*2+1].mini); tree[idx].maxi = min(tree[idx].maxi, tree[idx*2+1].maxi); tree[idx].sum = tree[2*idx].sum+tree[idx*2+1].sum; } ND query(int idx, int L, int R, int l, int r) { //printf("Q %d %d %d %d %d\n", idx, L, R, l, r); if(l > R || r < L) { // printf("WTF"); return {1LL<<62, -(1LL<<62), 0}; } int mid = (L+R)/2; if(l <= L && R <= r) { // printf("WTF2"); return tree[idx]; } // 1 .. mid, mid+1...r ND q1 = {1LL<<62, -(1LL<<62), 0}; ND q2 = {1LL<<62, -(1LL<<62), 0}; q1 = query(2*idx, L, mid, l, r); q2 = query(2*idx+1, mid+1, R, l, r); return ND{min(q1.mini, q2.mini), max(q1.maxi, q2.maxi), q1.sum+q2.sum}; } char s1[10], s2[10]; int main() { int n; while(scanf("%d", &n)==1) { T.clear(); REP(i, 2*TS+2) { tree[i].mini = 1LL<<62; tree[i].maxi = -(1LL<<62); tree[i].sum = 0; } REP(i, n) { ll TTT; scanf("%lld%d", &TTT, V+i); T.pb(TTT); upd(1, 1, TS, i+1, V[i]); } int C; scanf("%d", &C); REP(i, C) { ll L; scanf("%s%s%d", s1, s2, &L); string A = string(s1); string B = string(s2); int cnt = 0; REP(j, n) { ll prevTime = T[j]-L; auto it = lower_bound(T.begin(), T.end(), prevTime); int startIdx = it-T.begin(); if(startIdx == j) continue; ND val = query(1, 1, TS, startIdx+1, j); //printf("startIdx=%d, from interval tree for j=%d: %lld %lld %lld, my val is %d, %s %s\n", startIdx, j, val.mini, val.maxi, val.sum, V[j], A.c_str(), B.c_str()); ll RES = val.mini; if(B=="maxi") RES = val.maxi; else if(B == "avg") { if(A=="lt" && val.sum > (j-startIdx)*V[j]) { cnt++; } else if(A=="gt"&&val.sum < (j-startIdx)*V[j]) { cnt++; } } if(B=="max"||B=="min") { if(A=="lt" && RES>V[j]) cnt++; else if(A=="gt" && RES