|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +#define fastio ios_base::sync_with_stdio(0); cin.tie(0) |
| 5 | +#define LL long long |
| 6 | +#define mod 1000000007 |
| 7 | +#define all(v) v.begin()+1,v.end() |
| 8 | +#define FOR(i, j, k) for (auto i=j ; i<k ; i++) |
| 9 | +#define ROF(i, j, k) for (auto i=j ; i>=k ; i--) |
| 10 | +#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) |
| 11 | +#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false); debug("%s time : %.4fs", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) |
| 12 | + |
| 13 | +const long long INF = 1e18; |
| 14 | +const long long MAX = 1e5+10; |
| 15 | + |
| 16 | +struct pt{ |
| 17 | + LL l,r; |
| 18 | + pt(LL x, LL y){ |
| 19 | + l=x; r=y; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +struct node{ |
| 24 | + pt key; |
| 25 | + node *left,*right; |
| 26 | +}; |
| 27 | + |
| 28 | +node* newNode(pt key){ |
| 29 | + node* temp = (struct node*)malloc(sizeof(struct node)); |
| 30 | + temp->key = key; |
| 31 | + temp->left = temp->right = NULL; |
| 32 | + return temp; |
| 33 | +} |
| 34 | + |
| 35 | +node* insert(node* root, pt key){ |
| 36 | + if(root==NULL) |
| 37 | + return newNode(key); |
| 38 | + if(key.r < root->key.l) |
| 39 | + root->left = insert(root->left, key); |
| 40 | + else |
| 41 | + root->right = insert(root->right, key); |
| 42 | + |
| 43 | + return root; |
| 44 | +} |
| 45 | + |
| 46 | +node* minNode(node* node){ |
| 47 | + struct node* current = node; |
| 48 | + while (current && current->left != NULL) |
| 49 | + current = current->left; |
| 50 | + |
| 51 | + return current; |
| 52 | +} |
| 53 | + |
| 54 | +node* deleteNode(node* root, pt key){ |
| 55 | + if(root==NULL) return root; |
| 56 | + |
| 57 | + if(key.r < root->key.l) |
| 58 | + root->left = deleteNode(root->left, key); |
| 59 | + else if( key.l > root->key.r) |
| 60 | + root->right = deleteNode(root->right, key); |
| 61 | + else { |
| 62 | + if (root->left == NULL && root->right == NULL) |
| 63 | + return NULL; |
| 64 | + else if(root->left == NULL){ |
| 65 | + node *temp = root->right; |
| 66 | + free(root); |
| 67 | + return temp; |
| 68 | + }else if(root->right == NULL){ |
| 69 | + node *temp = root->left; |
| 70 | + free(root); |
| 71 | + return temp; |
| 72 | + } |
| 73 | + node* temp = minNode(root->right); |
| 74 | + root->key = temp->key; |
| 75 | + root->right = deleteNode(root->right, temp->key); |
| 76 | + } |
| 77 | + return root; |
| 78 | +} |
| 79 | + |
| 80 | +// node* search(node* root, pt key){ |
| 81 | +// if(root==NULL || (root->key.l == key.l && root->key.r==key.r)) |
| 82 | +// return root; |
| 83 | +// if(root->key.r < key.l) |
| 84 | +// return search(root->right,key); |
| 85 | + |
| 86 | +// return search(root->left,key); |
| 87 | +// } |
| 88 | + |
| 89 | +void inorder(struct node* root) |
| 90 | +{ |
| 91 | + if (root != NULL) { |
| 92 | + inorder(root->left); |
| 93 | + cout<<root->key.l<<" "<<root->key.r<<"\n"; |
| 94 | + inorder(root->right); |
| 95 | + } |
| 96 | +} |
| 97 | +void search(node* root, pt &key, LL &d, LL x){ |
| 98 | + if(root==NULL) return; |
| 99 | + if(root->key.l<=x && root->key.r>=x){ |
| 100 | + d=0; key.l=root->key.l; |
| 101 | + key.r = root->key.r; |
| 102 | + } |
| 103 | + else if(root->key.l> x){ |
| 104 | + if(root->key.l-x <d || (root->key.l-x == d && key.l>root->key.r)){ |
| 105 | + d = root->key.l-x; |
| 106 | + key.l = root->key.l; |
| 107 | + key.r = root->key.r; |
| 108 | + } |
| 109 | + search(root->left,key,d,x); |
| 110 | + } |
| 111 | + else { |
| 112 | + if(x-root->key.r <d || (x-root->key.r == d && key.l>root->key.r)){ |
| 113 | + d = x-root->key.r; |
| 114 | + key.l = root->key.l; |
| 115 | + key.r = root->key.r; |
| 116 | + } |
| 117 | + search(root->right,key,d,x); |
| 118 | + } |
| 119 | +} |
| 120 | +int main(){ |
| 121 | + time__("TT:") |
| 122 | +{ fastio; |
| 123 | + int t=1; cin>>t; int T=1; |
| 124 | + while(t--){ |
| 125 | + node* root = NULL; |
| 126 | + int n,m; cin>>n>>m; |
| 127 | + FOR(i,0,n){ |
| 128 | + LL x,y; cin>>x>>y; |
| 129 | + root = insert(root,pt(x,y)); |
| 130 | + } |
| 131 | + cout<<"Case #"<<T++<<": "; |
| 132 | + FOR(i,0,m){ |
| 133 | + LL x; cin>>x; |
| 134 | + LL d = 1e18; |
| 135 | + pt p = pt(INT_MAX, INT_MAX); |
| 136 | + search(root,p,d,x); |
| 137 | + root = deleteNode(root, pt(p.l,p.r)); |
| 138 | + int k=x-d>=p.l && x-d<=p.r?x-d:x+d; |
| 139 | + |
| 140 | + cout<<k<<" "; |
| 141 | + if(p.l == p.r) continue; |
| 142 | + if(p.l == k) |
| 143 | + root = insert(root,pt(k+1,p.r)); |
| 144 | + else if(p.r==k) |
| 145 | + root = insert(root,pt(p.l,k-1)); |
| 146 | + else { |
| 147 | + root = insert(root,pt(p.l,k-1)); |
| 148 | + root = insert(root,pt(k+1,p.r)); |
| 149 | + } |
| 150 | + // cout<<i<<"= \n"; |
| 151 | + // cout<<p.l<<" "<<p.r<<" "<<d<<" "<<k<<";; \n"; |
| 152 | + |
| 153 | + // inorder(root); cout<<"\n***\n"; |
| 154 | + } |
| 155 | + cout<<"\n"; |
| 156 | + }} |
| 157 | +} |
0 commit comments