Advertisement
a53

QMunte

a53
Jun 14th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. // Popa Bogdan Ioan, clasa a 10-a, Colegiul National Aurel Vlaicu Orastie
  2. #include <bits/stdc++.h>
  3. #define nmax 100002
  4. using namespace std;
  5. ifstream fin("qmunte.in");
  6. ofstream fout("qmunte.out");
  7. vector <int>arb[4 * nmax + 55], aux;
  8. int num[4 * nmax + 55];
  9. int a[nmax];
  10. int cnt;
  11. int i;
  12. int n,Q;
  13. int t,x,y;
  14. int updateStack(vector<int>&st, vector<int>&a, vector <int> &b, bool ok)
  15. {
  16. if(!ok)
  17. st = a;
  18. int ret = 0;
  19. for(int i = 0; i < (int)b.size(); i++)
  20. {
  21. while(st.size() >= 2 && st[st.size() - 1] > b[i] && st[st.size() - 1] > st[st.size() - 2])
  22. st.pop_back(), ++ret;
  23. st.push_back(b[i]);
  24. }
  25. return ret;
  26. }
  27. void Build(int nod, int st, int dr)
  28. {
  29. if(st == dr)
  30. {
  31. num[nod] = 0;
  32. arb[nod].push_back(a[st]);
  33. return;
  34. }
  35. int mij = (st + dr) / 2;
  36. Build(2 * nod, st, mij);
  37. Build(2 * nod + 1, mij + 1, dr);
  38. num[nod] = num[2 * nod] + num[2 * nod + 1];
  39. num[nod] += updateStack(arb[nod], arb[2 * nod], arb[2 * nod + 1], false);
  40. }
  41. void Update(int nod, int st, int dr, int poz, int val)
  42. {
  43. if(st == dr)
  44. {
  45. num[nod] = 0;
  46. a[st] = val;
  47. arb[nod][0] = val;
  48. return;
  49. }
  50. int mij = (st + dr) / 2;
  51. if(poz <= mij)
  52. Update(2 * nod, st, mij, poz, val);
  53. else Update(2 * nod + 1, mij+1, dr, poz, val);
  54. num[nod] = num[2 * nod] + num[2 * nod + 1];
  55. num[nod] += updateStack(arb[nod], arb[2 * nod], arb[2 * nod + 1], false);
  56. }
  57. void Query(int nod, int st, int dr, int a, int b)
  58. {
  59. if(a <= st && dr <= b)
  60. {
  61. cnt += num[nod];
  62. cnt += updateStack(aux, aux, arb[nod], true);
  63. return;
  64. }
  65. int mij = (st + dr) / 2;
  66. if(a <= mij)
  67. Query(2 * nod, st, mij, a, b);
  68. if(b > mij)
  69. Query(2 * nod + 1, mij+1, dr, a, b);
  70. }
  71. int main()
  72. {
  73. fin >> n >> Q;
  74. for(i = 1; i <= n; i++)
  75. fin >> a[i];
  76. Build(1, 1, n);
  77. while(Q--)
  78. {
  79. fin >> t >> x >> y;
  80. if(t == 1)
  81. Update(1, 1, n, x, y);
  82. else
  83. {
  84. cnt = 0;
  85. aux.clear();
  86. Query(1, 1, n, x, y);
  87. fout << cnt << "\n";
  88. }
  89. }
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement