Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fill(x,y) memset(x,y,sizeof(x))
  5. #define fi first
  6. #define se second
  7. #define sz(x) ((int) (x).size())
  8. #define all(x) (x).begin(), (x).end()
  9. #define sqr(x) ((x) * (x))
  10. #define sqrt(x) sqrt(abs(x))
  11. #define mp make_pair
  12. #define pb push_back
  13.  
  14. typedef long long ll;
  15. typedef string s;
  16. typedef pair<int,int> pii;
  17. typedef pair<int,string> pis;
  18. typedef pair<string,int> psi;
  19. typedef pair<string,string> pss;
  20. typedef vector<int> vi;
  21. typedef vector<vi> vvi;
  22. typedef vector<pii> vpii;
  23. typedef vector<vector<ll> > matrix;
  24.  
  25. const ll oo = 1e6;
  26.  
  27. const double EPS = (1e-7);
  28. int dcmp(double x, double y) { return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1; }
  29.  
  30.  
  31. int tarr[400001];
  32. int res[100001];
  33.  
  34. void build (int index ,int l , int r){
  35. if(l==r){
  36. tarr[index]= l;
  37. return ;
  38. }
  39. int mid = (l+r)/2;
  40. build(2*index,l,mid);
  41. build(2*index+1,mid+1,r);
  42. if(res[tarr[2*index]] >= res[tarr[2*index+1]]){
  43. tarr[index] = tarr[2*index];
  44. }
  45. else{
  46. tarr[index] = tarr[2*index+1];
  47. }
  48. }
  49.  
  50.  
  51. void update (int index , int start , int endd , int ind){
  52. if(start == ind && endd == ind ){
  53. return;
  54. }
  55. int mid=(start+endd)/2;
  56. if(ind <= mid) update(2*index,start,mid,ind);
  57. else update(2*index+1,mid+1,endd,ind);
  58. if(res[tarr[2*index]] >= res[tarr[2*index+1]]){
  59. tarr[index] = tarr[2*index];
  60. }
  61. else{
  62. tarr[index] = tarr[2*index+1];
  63. }
  64. }
  65.  
  66. int main(){
  67. ios::sync_with_stdio(0);
  68. int inputs;
  69. cin >> inputs ;
  70. for(int i=0 ; i<inputs ; i++){
  71. memset(res, 0 ,sizeof res);
  72. int maxy=0,maxin=0,n,q,moment,tries=0;
  73. cin >> n >> q ;
  74. build(1, 0, n-1);
  75. for(int j=0 ; j<q ; j++){
  76. int x,p ; cin >> x >> p;
  77. x--;
  78. res[x] += p;
  79. update(1, 0, n-1, x);
  80. int comp = tarr[1];
  81. int maxi2 = res[comp];
  82. if(maxi2 > maxy){
  83. if(comp != maxin){
  84. tries++;
  85. moment=j;
  86. }
  87. maxin=comp;
  88. maxy = maxi2;
  89. }
  90. else if(maxi2 == maxy){
  91. if(comp < maxin){
  92. tries++;
  93. moment=j;
  94. maxin=comp;
  95. maxy = maxi2;
  96. }
  97. }
  98. }
  99. if(tries==1){
  100. cout<<0<<endl;
  101. }
  102. else{
  103. cout<<(moment+1)<<endl;
  104. }
  105. }
  106. }
  107. /*
  108. 1
  109. 5 6
  110. 1 -2
  111. 2 -2
  112. 3 -1
  113. 2 -1
  114. 3 0
  115. 4 0*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement