Advertisement
Guest User

G - Hiring

a guest
Oct 17th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <sstream>
  2. #include <queue>
  3. #include <stack>
  4. #include <set>
  5. #include <map>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cctype>
  9. #include <complex>
  10. #include <cmath>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include <algorithm>
  17. #include <bitset>
  18. #include <list>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <time.h>
  22.  
  23. using namespace std;
  24.  
  25. #define SZ(x) ((int)x.size())
  26. #define all(a) a.begin(),a.end()
  27. #define allr(a) a.rbegin(),a.rend()
  28. #define clrall(name,val) memset(name,(val),sizeof(name));
  29. #define EPS 10e-9
  30. #define ll long long
  31. #define ull long long unsigned
  32. #define sf scanf
  33. #define pf printf
  34. #define psb(b) push_back((b))
  35. #define ppb() pop_back()
  36. #define oo (1<<28)
  37. #define mp make_pair
  38. #define mt make_tuple
  39. #define get(a,b) get<b>(a)
  40. #define fs first
  41. #define sc second
  42. #define Read freopen("in.txt","r",stdin)
  43. #define Write freopen("out.txt","w",stdout)
  44. #define __ std::ios_base::sync_with_stdio (false)
  45.  
  46. ll MulModL(ll B,ll P,ll M) {    ll R=0; while(P>0)      { if((P&1ll)==1) { R=(R+B); if(R>=M) R-=M; } P>>=1ll; B=(B+B); if(B>=M) B-=M; } return R; }
  47.  
  48. ll MulModD(ll B, ll P,ll M) {   ll I=((long double)B*(long double)P/(long double)M);    ll R=B*P-M*I; R=(R%M+M)%M;  return R; }
  49.  
  50. ll BigMod(ll B,ll P,ll M) {     ll R=1; while(P>0)      { if(P%2==1) { R=(R*B)%M; } P/=2; B=(B*B)%M; } return R; } /// (B^P)%M
  51.  
  52. template<class T1> void deb(T1 e1){cout<<e1<<"\n";}
  53. template<class T1,class T2> void deb(T1 e1,T2 e2){cout<<e1<<" "<<e2<<"\n";}
  54. template<class T1,class T2,class T3> void deb(T1 e1,T2 e2,T3 e3){cout<<e1<<" "<<e2<<" "<<e3<<"\n";}
  55. template<class T1,class T2,class T3,class T4> void deb(T1 e1,T2 e2,T3 e3,T4 e4){cout<<e1<<" "<<e2<<" "<<e3<<" "<<e4<<"\n";}
  56. template<class T1,class T2,class T3,class T4,class T5> void deb(T1 e1,T2 e2,T3 e3,T4 e4,T5 e5){cout<<e1<<" "<<e2<<" "<<e3<<" "<<e4<<" "<<e5<<"\n";}
  57. template<class T1,class T2,class T3,class T4,class T5,class T6> void deb(T1 e1,T2 e2,T3 e3,T4 e4,T5 e5,T6 e6){cout<<e1<<" "<<e2<<" "<<e3<<" "<<e4<<" "<<e5<<" "<<e6<<"\n";}
  58. template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> void deb(T1 e1,T2 e2,T3 e3,T4 e4,T5 e5,T6 e6,T7 e7){cout<<e1<<" "<<e2<<" "<<e3<<" "<<e4<<" "<<e5<<" "<<e6<<" "<<e7<<"\n";}
  59.  
  60. //int dx[]= {-1,-1,0,0,1,1};
  61. //int dy[]= {-1,0,-1,1,0,1};
  62. //int dx[]= {0,0,1,-1};/*4 side move*/
  63. //int dy[]= {-1,1,0,0};/*4 side move*/
  64. //int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
  65. //int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
  66. //int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
  67. //int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/
  68.  
  69. const int MAX = 200700;
  70. const int MAXD = 1e6+7;
  71. vector<int> idx[MAXD+7];
  72. pair<ll,ll> tree[MAX*4+10];
  73. ll dur[MAX];
  74. pair<pair<ll,ll>,int> Query[MAX];
  75. int ans[MAX];
  76.  
  77. #define mson int mid=(l+r)>>1,nidx=(idx<<1)
  78.  
  79. void build(int idx,int l,int r)
  80. {
  81.     if(l==r)
  82.     {
  83.         tree[idx].fs=dur[l];
  84.         tree[idx].sc=1;
  85.         return ;
  86.     }
  87.     mson;
  88.     build(nidx,l,mid);
  89.     build(nidx+1,mid+1,r);
  90.     tree[idx].fs=tree[nidx].fs+tree[nidx+1].fs;
  91.     tree[idx].sc=tree[nidx].sc+tree[nidx+1].sc;
  92.     return ;
  93. }
  94.  
  95. void update(int idx,int l,int r,int k)
  96. {
  97.     if(l==r)
  98.     {
  99.         tree[idx].fs=0;
  100.         tree[idx].sc=0;
  101.         return ;
  102.     }
  103.     mson;
  104.     if(k<=mid) update(nidx,l,mid,k);
  105.     else update(nidx+1,mid+1,r,k);
  106.     tree[idx].fs=tree[nidx].fs+tree[nidx+1].fs;
  107.     tree[idx].sc=tree[nidx].sc+tree[nidx+1].sc;
  108.     return ;
  109. }
  110. int query1(int idx,int l,int r,ll d,ll p)
  111. {
  112.     if(l==r) return l;
  113.     mson;
  114.     if(tree[nidx].fs-(d*tree[nidx].sc)>=p)
  115.         return query1(nidx,l,mid,d,p);
  116.     else
  117.         return query1(nidx+1,mid+1,r,d,p-(tree[nidx].fs-(d*tree[nidx].sc)));
  118.     return 0;
  119. }
  120.  
  121. int query(int idx,int l,int r,ll d,ll p)
  122. {
  123.     if(tree[idx].fs-(d*tree[idx].sc)<p) return 0;
  124.     return query1(idx,l,r,d,p);
  125. }
  126.  
  127.  
  128. int main()
  129. {
  130.  
  131.  
  132.     int n,m;
  133.     sf("%d %d",&n,&m);
  134.     for(int i=1;i<=m;i++)
  135.     {
  136.         sf("%lld",&dur[i]);
  137.         idx[dur[i]].psb(i);
  138.     }
  139.  
  140.     for(int i=0;i<n;i++)
  141.     {
  142.         sf("%lld %lld",&Query[i].fs.fs,&Query[i].fs.sc);
  143.         Query[i].sc=i;
  144.     }
  145.  
  146.     sort(Query,Query+n);
  147.  
  148.     build(1,1,m);
  149.  
  150.     int cur=0;
  151.  
  152.     for(int i=0;i<MAXD;i++)
  153.     {
  154.         int k=SZ(idx[i]);
  155.         for(int j=0;j<k;j++)
  156.         {
  157.             update(1,1,m,idx[i][j]);
  158.         }
  159.         while(cur<n && Query[cur].fs.fs==(ll)i){
  160.             ans[Query[cur].sc]=query(1,1,m,i,Query[cur].fs.sc);
  161.             cur++;
  162.         }
  163.     }
  164.     for(int i=0;i<n;i++)
  165.     {
  166.         pf("%d%c",ans[i]," \n"[i==n-1]);
  167.     }
  168.  
  169.     return 0;
  170. }
  171.  
  172.  
  173. /**
  174. 5 6
  175. 9 1 3 4 6
  176. 2 16
  177. 3 7
  178. 1 15
  179. 4 10
  180. 9 1
  181. 0 23
  182. */
  183. /**
  184. 10 10
  185. 54 6 65 1 54 6 3 5 9 8
  186. 10 5
  187. 5 16
  188. 3 56
  189. 98 21
  190. 65 14
  191. 24 65
  192. 7 88
  193. 69 15
  194. 3 3
  195. */
  196. /**
  197. 1 62
  198. 82 86 17 55 37 35 29 98 75 21 14 60 58 43 71 22 94 74 39 63 50 48 95 93 59 42 51 37 89 12 56 93 25 69 51 13 53 21 80 16 80 61 74 73 59 47 41 40 62 79 21 71 32 46 37 13 44 10 94 18 93 63
  199. 10 1574
  200. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement