Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void merge(vector<long long> &v,int left,int right)
- {
- int mid=(left+right)/2;
- vector<long long> temp(v.size());
- int p1=left,p2=mid+1,k=0;
- while(p1!=mid+1&&p2!=right+1)
- {
- if(v[p1]<v[p2])
- temp[k++]=v[p1++];
- else
- temp[k++]=v[p2++];
- }
- while(p1!=mid+1)
- temp[k++]=v[p1++];
- while(p2!=right+1)
- temp[k++]=v[p2++];
- k=0;
- for(int i=left;i<=right;i++)
- {
- v[i]=temp[k++];
- }
- }
- void ms(vector<long long> &v,int left,int right)
- {
- if(left==right)
- return ;
- int mid=(left+right)/2;
- ms(v,left,mid);
- ms(v,mid+1,right);
- merge(v,left,right);
- }
- int query(vector<long long> v,int q)
- {
- int left=0,right=v.size()-1,mid;
- long long ans=INT_MIN;
- while(left<=right){
- mid=(left+right)/2;
- // cout<<ans<<endl;
- if(v[mid]==q)
- {
- ans=q;
- break;
- }
- else if(v[mid]<q)
- { left=mid+1;
- ans=v[mid];
- }
- else
- right=mid-1;
- }
- return ans;
- }
- int main()
- {
- int size;
- cin>>size;
- vector<long long> v;
- for(int i=0;i<size;i++)
- {
- long long temp;
- cin>>temp;
- v.push_back(temp);
- }
- ms(v,0,v.size()-1);
- int z;
- cin>>z;
- vector<long long> q(z),ans(z);
- for(int i=0;i<z;i++)
- {
- cin>>q[i];
- ans[i]+=query(v,q[i]);
- }
- for(auto x:ans)
- cout<<x<<endl;
- }
Add Comment
Please, Sign In to add comment