Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.StringTokenizer;
  7. import java.util.TreeMap;
  8.  
  9. public class SettingExam {
  10.  
  11.  
  12.     static class FloorPair {
  13.         int queryIdx;
  14.         int val;
  15.  
  16.         public FloorPair(int queryIdx, int val) {
  17.             this.queryIdx = queryIdx;
  18.             this.val = val;
  19.         }
  20.     }
  21.  
  22.     public static void main(String[] args) throws IOException {
  23.         Scanner sc = new Scanner(System.in);
  24.         int n = sc.nextInt();
  25.         int y = 0;
  26.  
  27.         int[][] queries = new int[n][2]; // first idx for type, 2nd idx for val/res
  28.         // 0 -> add, 1 -> remove, -1 -> floor
  29.  
  30.         TreeMap<Integer, Integer> freqs = new TreeMap<>();
  31.         ArrayList<FloorPair>[] floors = new ArrayList[n + 1];
  32.  
  33.         for (int i = 0; i < queries.length; i++) {
  34.             queries[i][0] = -1;
  35.             queries[i][1] = -1;
  36.         }
  37.  
  38.         for (int i = 0; i < floors.length; i++) {
  39.             floors[i] = new ArrayList<>();
  40.         }
  41.  
  42.         for (int i = 0; i < n; i++) {
  43.             String type = sc.next();
  44.             switch (type) {
  45.                 case "floor":
  46.                     floors[sc.nextInt()].add(new FloorPair(i, sc.nextInt()));
  47.                     break;
  48.                 case "add":
  49.                     queries[i][0] = 0;
  50.                     queries[i][1] = sc.nextInt();
  51.                     break;
  52.                 case "remove":
  53.                     queries[i][0] = 1;
  54.                     queries[i][1] = sc.nextInt();
  55.                     break;
  56.             }
  57.         }
  58.  
  59.         int ver = 0;
  60.         for (int i = 0; i < queries.length; i++) {
  61.             int type = queries[i][0];
  62.             int val = queries[i][1];
  63.             switch (type) {
  64.                 case 0: // add
  65.                     freqs.put(val ^ y, freqs.getOrDefault(val, 0) + 1);
  66.                     ver++;
  67.                     break;
  68.                 case 1: // remove
  69.                     int prev = freqs.getOrDefault(val, -1);
  70.                     if (prev != -1) {
  71.                         if (prev == 1)
  72.                             freqs.remove(val);
  73.                         else
  74.                             freqs.put(val, prev - 1);
  75.                     }
  76.                     ver++;
  77.                     break;
  78.                 default: // floor
  79.                     y = Math.max(0, val);
  80.                     if (val <= -1) {
  81.                         System.out.println("No Questions");
  82.                     } else {
  83.                         System.out.println(val);
  84.                     }
  85.                     break;
  86.             }
  87.  
  88.             for (FloorPair p : floors[ver]) {
  89.                 Integer f = freqs.floorKey(p.val);
  90.                 if (f != null) {
  91.                     queries[p.queryIdx][1] = f;
  92.                 }
  93.             }
  94.         }
  95.  
  96.     }
  97.  
  98.     static class Scanner {
  99.         StringTokenizer st;
  100.         BufferedReader br;
  101.  
  102.         public Scanner(InputStream s) {
  103.             br = new BufferedReader(new InputStreamReader(s));
  104.         }
  105.  
  106.         public String next() throws IOException {
  107.             while (st == null || !st.hasMoreTokens())
  108.                 st = new StringTokenizer(br.readLine());
  109.             return st.nextToken();
  110.         }
  111.  
  112.         public int nextInt() throws IOException {
  113.             return Integer.parseInt(next());
  114.         }
  115.  
  116.         public long nextLong() throws IOException {
  117.             return Long.parseLong(next());
  118.         }
  119.  
  120.         public String nextLine() throws IOException {
  121.             return br.readLine();
  122.         }
  123.  
  124.         public double nextDouble() throws IOException {
  125.             String x = next();
  126.             StringBuilder sb = new StringBuilder("0");
  127.             double res = 0, f = 1;
  128.             boolean dec = false, neg = false;
  129.             int start = 0;
  130.             if (x.charAt(0) == '-') {
  131.                 neg = true;
  132.                 start++;
  133.             }
  134.             for (int i = start; i < x.length(); i++)
  135.                 if (x.charAt(i) == '.') {
  136.                     res = Long.parseLong(sb.toString());
  137.                     sb = new StringBuilder("0");
  138.                     dec = true;
  139.                 } else {
  140.                     sb.append(x.charAt(i));
  141.                     if (dec)
  142.                         f *= 10;
  143.                 }
  144.             res += Long.parseLong(sb.toString()) / f;
  145.             return res * (neg ? -1 : 1);
  146.         }
  147.  
  148.         public boolean ready() throws IOException {
  149.             return br.ready();
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement