a53

Predictor Machine

a53
Jan 24th, 2021 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #define LDR 1
  3. #define LST 200000
  4. using namespace std;
  5. struct anode
  6. {
  7. int nr=0,val=0;
  8. };
  9.  
  10. anode findBest(anode a,anode b)
  11. {
  12. if(a.val>b.val)
  13. return a;
  14. else
  15. if(a.val<b.val)
  16. return b;
  17. else
  18. if(a.nr<b.nr)
  19. return a;
  20. return b;
  21. }
  22.  
  23. int n=0;
  24. int v[200001]={0};
  25. anode arb[600001];
  26. int lazy[600001]={0};
  27. void build(int nod,int a,int b)
  28. {
  29. if(a>=b)
  30. {
  31. arb[nod].nr=a;
  32. return;
  33. }
  34. int mij=(a+b)/2;
  35. build(nod*2,a,mij);
  36. build(nod*2+1,mij+1,b);
  37. }
  38.  
  39. void propag(int nod,int a,int b)
  40. {
  41. arb[nod].val+=lazy[nod];
  42. if(arb[nod].nr==0)
  43. arb[nod].nr=a;
  44. if(a!=b)
  45. {
  46. lazy[nod*2]+=lazy[nod];
  47. lazy[nod*2+1]+=lazy[nod];
  48. }
  49. lazy[nod]=0;
  50. }
  51.  
  52. void update(int nod,int st,int dr,int a,int b,int val)
  53. {
  54. if(a>=st&&b<=dr)
  55. lazy[nod]+=val;
  56. else
  57. {
  58. int mij=(a+b)/2;
  59. if(st<=mij)
  60. update(nod*2,st,dr,a,mij,val);
  61. propag(nod*2,a,mij);
  62. if(dr>mij)
  63. update(nod*2+1,st,dr,mij+1,b,val);
  64. propag(nod*2+1,mij+1,b);
  65. arb[nod]=findBest(arb[nod*2],arb[nod*2+1]);
  66. }
  67. }
  68.  
  69. int main()
  70. {
  71. int x=0,y=0,q=0;
  72. build(1,LDR,LST);
  73. cin>>n>>v[1];
  74. for(int i=2;i<=n;++i)
  75. {
  76. cin>>v[i];
  77. update(1,min(v[i-1],v[i]),max(v[i-1],v[i]),LDR,LST,1);
  78. if(i>1&&i<n)
  79. update(1,v[i],v[i],LDR,LST,-1);
  80. }
  81. propag(1,LDR,LST);
  82. cin>>q;
  83. for(int i=1;i<=q;++i)
  84. {
  85. cin>>x>>y;
  86. if(x<n)
  87. update(1,min(v[x],v[x+1]),max(v[x],v[x+1]),LDR,LST,-1);
  88. if(x>1)
  89. update(1,min(v[x],v[x-1]),max(v[x],v[x-1]),LDR,LST,-1);
  90. if(x>1&&x<n)
  91. update(1,v[x],v[x],LDR,LST,1);
  92. v[x]=y;
  93. if(x<n)
  94. update(1,min(v[x],v[x+1]),max(v[x],v[x+1]),LDR,LST,1);
  95. if(x>1)
  96. update(1,min(v[x],v[x-1]),max(v[x],v[x-1]),LDR,LST,1);
  97. if(x>1&&x<n)
  98. update(1,v[x],v[x],LDR,LST,-1);
  99. propag(1,LDR,LST);
  100. cout<<arb[1].nr<<' '<<arb[1].val<<'\n';
  101. }
  102. return 0;
  103. }
  104.  
Add Comment
Please, Sign In to add comment