File size: 4,042 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
package exam;
import java.util.HashMap;
class Grandpabernie{
public static class RBBST{
Node root;
class Node{
Node left, right;
int key;
int val;
int size;
boolean color;
Node(int key, int val, int s, boolean color){
this.key = key;
this.color = color;
this.val = val;
size = s;
}
public int index(){
if( right != null){
return size - right.size;
}
return size;
}
}
public int get_key(int i){
return get_key(root, i);
}
public int get_key(Node h, int i){
int lb, ub;
if (h.left != null){
lb = h.left.size+1;
}
else{
lb = i;
}
ub = lb + h.val;
if( i >= lb && i < ub){
return h.key;
}
if( i < h.index()){
return get_key(h.left, i);
}
else{
return get_key(h.right, i-h.index());
}
}
boolean isRed(Node h){
if(h==null) return false;
return h.color == true;
}
Node rotateLeft(Node h){
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = true;
x.size = h.size;
h.size = h.val + size(h.left) + size(h.right);
return x;
}
int size(Node h){
if (h==null) return 0;
return h.size;
}
Node rotateRight(Node h){
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = true;
x.size = h.size;
h.size = h.val + size(h.left) + size(h.right);
return x;
}
void flipColors(Node h){
h.color = true;
h.left.color = false;
h.right.color = false;
}
public void insert(int ins_key, int val){
root = insert(root, ins_key, val);
root.color = false;
}
RBBST(int ins_key){
insert(ins_key, 1);
}
public Node insert(Node h, int ins_key, int val){
if(h==null) return new Node(ins_key, val, 1, true);
int cmp = Integer.compare(ins_key, h.key);
if(cmp<0) h.left = insert(h.left, ins_key, val);
else if(cmp>0) h.right = insert(h.right, ins_key, val);
else h.val += val;
if( isRed(h.right) && ! isRed(h.left)) h = rotateLeft(h);
if( h.left != null && isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
if( isRed(h.right) && isRed(h.left)) flipColors(h);
h.size = size(h.left) + size(h.right) + h.val;
return h;
}
}
public static void main(String[] args){
Kattio io = new Kattio(System.in, System.out);
int n = io.getInt();
HashMap<String, RBBST> h = new HashMap<>(n);
for(int i = 0; i < n; i++){
String country = io.getWord();
int year = io.getInt();
if(h.containsKey(country)){
h.get(country).insert(year, 1);
}
else{
RBBST temp = new RBBST(year);
h.put(country, temp);
}
}
int q = io.getInt();
for(int i = 0; i<q; i++){
String country = io.getWord();
int kthTrip = io.getInt();
io.print(h.get(country).get_key(kthTrip));
io.print('\n');
}
io.flush();
io.close();
}
} |