/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package samples; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.security.KeyPair; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; /** * * @author cteam075 */ public class Samples { public static BufferedReader in; /** * */ public static List samples; /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // in = new BufferedReader(new InputStreamReader(System.in)); in = new BufferedReader(new FileReader("input.txt")); String rows; while ((rows = in.readLine()) != null) { int numOfSamples = Integer.parseInt(rows); samples = new ArrayList(); for (int i = 0; i < numOfSamples; i++) { String row = in.readLine(); samples.add(new Pair(Integer.parseInt(row.split(" ")[0]), Integer.parseInt(row.split(" ")[1]))); } int numOfEvaluations = Integer.parseInt(in.readLine()); for (int i = 0; i < numOfEvaluations; i++) { String eval = in.readLine(); String relationOperator = eval.split(" ")[0]; String aggregateOperator = eval.split(" ")[1]; int seconds = Integer.parseInt(eval.split(" ")[2]); switch (aggregateOperator) { case "avg": { if (relationOperator.compareTo("gt") == 0) { System.out.println(avgFunc(seconds, true)); } else { System.out.println(avgFunc(seconds, false)); } break; } case "min": { if (relationOperator.compareTo("gt") == 0) { System.out.println(minFunc(seconds, true)); } else { System.out.println(minFunc(seconds, false)); } break; } case "max": { if (relationOperator.compareTo("gt") == 0) { System.out.println(maxFunc(seconds, true)); } else { System.out.println(maxFunc(seconds, false)); } break; } } } } } public static int avgFunc(int seconds, boolean gt) { int res = 0; int sum = 0; int lowestIndex = 0; for (int i = 0; i < samples.size(); i++) { int avg = 0; int count = 0; int actSecond = samples.get(i).getKey(); while(samples.get(lowestIndex).getKey() < actSecond - seconds){ lowestIndex++; avg-=samples.get(lowestIndex).getValue(); count--; } if (gt) { if (samples.get(i).getValue() > ((float) ((float) avg / (float) count))) { res++; } } else if (samples.get(i).getValue() < ((float) ((float) avg / (float) count))) { res++; } avg+=samples.get(i).getValue(); count++; } return res; } public static int minFunc(int seconds, boolean gt) { int res = 0; List mins = new LinkedList<>(); int lowestIndex = 0; mins.add(samples.get(0)); int min = Integer.MAX_VALUE; for (int i = 1; i < samples.size(); i++) { if (samples.get(i).getKey() - seconds > samples.get(i - 1).getKey()) { continue; } int actSecond = samples.get(i).getKey(); while(samples.get(lowestIndex).getKey() < actSecond - seconds){ int temp = mins.get(0).getValue(); mins.remove(0); if(min == temp)min = getMin(mins); lowestIndex++; } if (gt) { if (samples.get(i).getValue() > min) { res++; } } else if (samples.get(i).getValue() < min) { res++; } if(min > samples.get(i).getValue()){ min = samples.get(i).getValue(); mins.clear(); } mins.add(samples.get(i)); } return res; } public static int maxFunc(int seconds, boolean gt) { int res = 0; List maxs = new LinkedList<>(); int lowestIndex = 0; maxs.add(samples.get(0)); int max = 0; for (int i = 1; i < samples.size(); i++) { if (samples.get(i).getKey() - seconds > samples.get(i - 1).getKey()) { continue; } int actSecond = samples.get(i).getKey(); while(samples.get(lowestIndex).getKey() < actSecond - seconds){ int temp = maxs.get(0).getValue(); maxs.remove(0); if(max == temp)max = getMax(maxs); lowestIndex++; } if (gt) { if (samples.get(i).getValue() > max) { res++; } } else if (samples.get(i).getValue() < max) { res++; } if(max < samples.get(i).getValue()){ max = samples.get(i).getValue(); maxs.clear(); } maxs.add(samples.get(i)); } return res; } private static int getMax(List maxs) { if(maxs.size() == 0)return 0; int max = maxs.get(0).getValue(); for(Pair p : maxs){ if(p.getValue() > max)max = p.getValue(); } return max; } private static int getMin(List maxs) { if(maxs.size() == 0)return Integer.MAX_VALUE; int max = maxs.get(0).getValue(); for(Pair p : maxs){ if(p.getValue() < max)max = p.getValue(); } return max; } public static class Pair { private int key; private int value; public Pair(int key, int value) { this.key = key; this.value = value; } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } }