#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define D(a) cerr << ">>> " << #a << " >" << a << "<" << endl; #define R1(a) scanf("%d", &a); #define R2(a) scanf("%d%d", &a,&b); #define R3(a) scanf("%d%d%d", &a,&b,&c); #define DR1(a) int a;scanf("%d", &a); #define DR2(a) int a,b;scanf("%d%d", &a,&b); #define DR3(a) int a,b,c;scanf("%d%d%d", &a,&b,&c); #define INF 1e9 typedef long long int ll; typedef pair pii; typedef vector vi; int t[100002]; int v[100002]; char tmp[3]; int cmp; // lt, gt int agg; // min, max, avg int len; int N; int C; int compare(double a, double b) { return cmp == 0 ? a < b : a > b; } int avg() { int low = 0, up = 0, samples = -1, res = 0; ll sum = 0; v[0] = 0; while(up < N) { up += 1; samples += 1; sum += v[up - 1]; while (t[up] - t[low] > len) { sum -= v[low]; samples -= 1; low += 1; } if (samples > 0 && compare((double) v[up], ((double) sum)/samples)) { res += 1; } } return res; } int min() { int low = 0, up = 0, samples = -1, res = 0, min = INF; v[0] = INF; while(up < N) { up += 1; samples += 1; if (v[up - 1] < min) { min = v[up - 1]; } while (t[up] - t[low] > len) { if (v[low] == min) { min = INF; FOR(i,low+1,up-1) { if (v[i] < min) { min = v[i]; } } } samples -= 1; low += 1; } if (samples > 0 && compare(v[up], min)) { res += 1; } } return res; } int max() { int low = 0, up = 0, samples = -1, res = 0, max = -1; v[0] = -1; while(up < N) { up += 1; samples += 1; if (v[up - 1] > max) { max = v[up - 1]; } while (t[up] - t[low] > len) { if (v[low] == max) { max = -1; FOR(i,low+1,up-1) { if (v[i] > max) { max = v[i]; } } } samples -= 1; low += 1; } if (samples > 0 && compare(v[up], max)) { res += 1; } } return res; } int main() { while(scanf("%d", &N) > 0) { t[0] = 0; v[0] = 0; FOR(i,1,N) { scanf("%d%d", &t[i], &v[i]); } scanf("%d", &C); FOR(i,0,C-1) { scanf("%s", tmp); if (strcmp(tmp, "lt") == 0) { cmp = 0; } else { cmp = 1; } scanf("%s", tmp); if (strcmp(tmp, "min") == 0) { agg = 0; } else if (strcmp(tmp, "max") == 0) { agg = 1; } else { agg = 2; } scanf("%d", &len); switch (agg) { case 0: // min printf("%d\n", min()); break; case 1: // max printf("%d\n", max()); break; default: // avg printf("%d\n", avg()); } } } return 0; }