Advertisement
Slayerfeed

TOI14 NBK48

May 13th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using pii = pair<int,int>;
  5. vector<pii> sum;
  6. int q1[100010];
  7. int Binarysearch(int y,int n){
  8.     int l=0 , r=n-1;
  9.  
  10.     int cnt =-1;
  11.     while(l<=r){
  12.         int m=(l+r)/2;
  13.         if(sum[m].first<=y){
  14.             if(cnt<q1[m]){
  15.                 cnt=q1[m];
  16.             }
  17.             l=m+1;
  18.         }
  19.         else{
  20.             r=m-1;
  21.         }
  22.     }
  23.     return cnt;
  24. }
  25. int main(){
  26.     int n ,q ;
  27.  
  28.     cin >> n >> q;
  29.  
  30.     int x;
  31.  
  32.     for(int i=0;i<n;++i){
  33.         scanf("%d",&x);
  34.         if(i==0){
  35.            sum.push_back(make_pair(x,i));
  36.         }
  37.         else{
  38.            sum.push_back(make_pair(x+sum[i-1].first,i));
  39.         }
  40.     }
  41.     sort(sum.begin(),sum.end());
  42.  
  43.     int posmax ;
  44.     for(int i=0;i<n;++i){
  45.        if(i==0){
  46.           q1[i]=sum[i].second;
  47.           posmax=i;
  48.        }
  49.        else{
  50.             if(sum[i].second>sum[posmax].second){
  51.                 q1[i]=sum[i].second;
  52.                 posmax =i;
  53.             }
  54.             else{
  55.                 q1[i]=sum[posmax].second;
  56.             }
  57.        }
  58.     }
  59.  
  60.     for(int i=0;i<q;++i){
  61.         scanf("%d",&x);
  62.         cout << Binarysearch(x,n)+1 << "\n";
  63.     }
  64.  
  65.     return 0;
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement