|
| 1 | +#include<iostream> |
| 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 | + pt(){} |
| 22 | +}; |
| 23 | + |
| 24 | +struct node{ |
| 25 | + pt key; |
| 26 | + node *left,*right; |
| 27 | +}; |
| 28 | + |
| 29 | +node* newNode(pt key){ |
| 30 | + node* temp = (struct node*)malloc(sizeof(struct node)); |
| 31 | + temp->key = key; |
| 32 | + temp->left = temp->right = NULL; |
| 33 | + return temp; |
| 34 | +} |
| 35 | + |
| 36 | +node* insert(node* root, pt key){ |
| 37 | + if(root==NULL) |
| 38 | + return newNode(key); |
| 39 | + if(key.r < root->key.l) |
| 40 | + root->left = insert(root->left, key); |
| 41 | + else |
| 42 | + root->right = insert(root->right, key); |
| 43 | + |
| 44 | + return root; |
| 45 | +} |
| 46 | + |
| 47 | +node* minNode(node* node){ |
| 48 | + struct node* current = node; |
| 49 | + while (current && current->left != NULL) |
| 50 | + current = current->left; |
| 51 | + |
| 52 | + return current; |
| 53 | +} |
| 54 | + |
| 55 | +node* deleteNode(node* root, pt key){ |
| 56 | + if(root==NULL) return root; |
| 57 | + |
| 58 | + if(key.r < root->key.l) |
| 59 | + root->left = deleteNode(root->left, key); |
| 60 | + else if( key.l > root->key.r) |
| 61 | + root->right = deleteNode(root->right, key); |
| 62 | + else { |
| 63 | + if (root->left == NULL && root->right == NULL) |
| 64 | + return NULL; |
| 65 | + else if(root->left == NULL){ |
| 66 | + node *temp = root->right; |
| 67 | + free(root); |
| 68 | + return temp; |
| 69 | + }else if(root->right == NULL){ |
| 70 | + node *temp = root->left; |
| 71 | + free(root); |
| 72 | + return temp; |
| 73 | + } |
| 74 | + node* succParent = root; |
| 75 | + node* succ = root->right; |
| 76 | + while (succ->left != NULL) { |
| 77 | + succParent = succ; |
| 78 | + succ = succ->left; |
| 79 | + } |
| 80 | + if (succParent != root) |
| 81 | + succParent->left = succ->right; |
| 82 | + else |
| 83 | + succParent->right = succ->right; |
| 84 | + |
| 85 | + root->key = succ->key; |
| 86 | + |
| 87 | + delete succ; |
| 88 | + return root; |
| 89 | + } |
| 90 | + return root; |
| 91 | +} |
| 92 | + |
| 93 | +void inorder(struct node* root) |
| 94 | +{ |
| 95 | + if (root != NULL) { |
| 96 | + inorder(root->left); |
| 97 | + cout<<root->key.l<<" "<<root->key.r<<"\n"; |
| 98 | + inorder(root->right); |
| 99 | + } |
| 100 | +} |
| 101 | +void search(node* root, pt &key, LL &d, LL x){ |
| 102 | + if(root==NULL) return; |
| 103 | + if(root->key.l<=x && root->key.r>=x){ |
| 104 | + d=0; key.l=root->key.l; |
| 105 | + key.r = root->key.r; |
| 106 | + } |
| 107 | + else if(root->key.l> x){ |
| 108 | + if(root->key.l-x <d || (root->key.l-x == d && key.l>root->key.r)){ |
| 109 | + d = root->key.l-x; |
| 110 | + key.l = root->key.l; |
| 111 | + key.r = root->key.r; |
| 112 | + } |
| 113 | + search(root->left,key,d,x); |
| 114 | + } |
| 115 | + else { |
| 116 | + if(x-root->key.r <d || (x-root->key.r == d && key.l>root->key.r)){ |
| 117 | + d = x-root->key.r; |
| 118 | + key.l = root->key.l; |
| 119 | + key.r = root->key.r; |
| 120 | + } |
| 121 | + search(root->right,key,d,x); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +bool cmp(pt a , pt b){ |
| 126 | + return (a.l<b.l || (a.l==b.l && a.r<=b.r)); |
| 127 | +} |
| 128 | + |
| 129 | +void merge(pt array[], int const left, int const mid, int const right) |
| 130 | +{ |
| 131 | + int const subArrayOne = mid - left + 1; |
| 132 | + int const subArrayTwo = right - mid; |
| 133 | + |
| 134 | + pt *leftArray = new pt[subArrayOne], |
| 135 | + *rightArray = new pt[subArrayTwo]; |
| 136 | + |
| 137 | + for (int i = 0; i < subArrayOne; i++) |
| 138 | + leftArray[i] = array[left + i]; |
| 139 | + for (int j = 0; j < subArrayTwo; j++) |
| 140 | + rightArray[j] = array[mid + 1 + j]; |
| 141 | + |
| 142 | + int indexOfSubArrayOne = 0, |
| 143 | + indexOfSubArrayTwo = 0; |
| 144 | + int indexOfMergedArray = left; |
| 145 | + |
| 146 | + while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) { |
| 147 | + if (cmp(leftArray[indexOfSubArrayOne] , rightArray[indexOfSubArrayTwo])) { |
| 148 | + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; |
| 149 | + indexOfSubArrayOne++; |
| 150 | + } |
| 151 | + else { |
| 152 | + array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; |
| 153 | + indexOfSubArrayTwo++; |
| 154 | + } |
| 155 | + indexOfMergedArray++; |
| 156 | + } |
| 157 | + |
| 158 | + while (indexOfSubArrayOne < subArrayOne) { |
| 159 | + array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; |
| 160 | + indexOfSubArrayOne++; |
| 161 | + indexOfMergedArray++; |
| 162 | + } |
| 163 | + |
| 164 | + while (indexOfSubArrayTwo < subArrayTwo) { |
| 165 | + array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; |
| 166 | + indexOfSubArrayTwo++; |
| 167 | + indexOfMergedArray++; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +void mergeSort(pt array[], int const begin, int const end) |
| 172 | +{ |
| 173 | + if (begin >= end) |
| 174 | + return; |
| 175 | + |
| 176 | + int mid = begin + (end - begin) / 2; |
| 177 | + mergeSort(array, begin, mid); |
| 178 | + mergeSort(array, mid + 1, end); |
| 179 | + merge(array, begin, mid, end); |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +int main(){ |
| 184 | + fastio; |
| 185 | + int t=1; cin>>t; int T=1; |
| 186 | + while(t--){ |
| 187 | + node* root = NULL; |
| 188 | + int n,m; cin>>n>>m; |
| 189 | + pt v[n]; |
| 190 | + LL vv[m]; |
| 191 | + LL ans[m]; |
| 192 | + FOR(i,0,n){ |
| 193 | + cin>>v[i].l>>v[i].r; |
| 194 | + } |
| 195 | + FOR(i,0,m) cin>>vv[i]; |
| 196 | + mergeSort(v,0,n-1); |
| 197 | + int l = n/2-1,r=n/2; |
| 198 | + while(1){ |
| 199 | + if(l>=0) root = insert(root,pt(v[l].l,v[l].r)); |
| 200 | + if(r<n) root = insert(root,pt(v[r].l,v[r].r)); |
| 201 | + if(l<0 && r>=n) break; |
| 202 | + l--;r++; |
| 203 | + } |
| 204 | + inorder(root); |
| 205 | + FOR(i,0,m){ |
| 206 | + LL x=vv[i];// cin>>x; |
| 207 | + LL d = 1e18; |
| 208 | + pt p = pt(1e18, 1e18); |
| 209 | + search(root,p,d,x); |
| 210 | + root = deleteNode(root, pt(p.l,p.r)); |
| 211 | + LL k=x-d>=p.l && x-d<=p.r?x-d:x+d; |
| 212 | + ans[i] = k; |
| 213 | + // cout<<k<<" "; |
| 214 | + if(p.l == p.r) continue; |
| 215 | + if(p.l == k) |
| 216 | + root = insert(root,pt(k+1,p.r)); |
| 217 | + else if(p.r==k) |
| 218 | + root = insert(root,pt(p.l,k-1)); |
| 219 | + else { |
| 220 | + root = insert(root,pt(p.l,k-1)); |
| 221 | + root = insert(root,pt(k+1,p.r)); |
| 222 | + } |
| 223 | + // cout<<i<<"= \n"; |
| 224 | + // cout<<p.l<<" "<<p.r<<" "<<d<<" "<<k<<";; \n"; |
| 225 | + |
| 226 | + // inorder(root); cout<<"\n***\n"; |
| 227 | + } |
| 228 | + cout<<"Case #"<<T++<<": "; |
| 229 | + for(int i =0;i<ans.size();i++) cout<<ans[i]<<" "; |
| 230 | + cout<<"\n"; |
| 231 | + } |
| 232 | +} |
0 commit comments