AlexNeagu11

arbin

Mar 19th, 2022 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream in("arbint.in");
  4. ofstream out("arbint.out");
  5. const int nmax = 100006;
  6. int block[nmax], blockSize, a[nmax], N, M, maxByBlock[nmax];
  7. int getMax(int L, int R) {
  8. int ans = -2e9;
  9. while(L < R && block[L] == block[L + 1]) {
  10. ans = max(ans, a[L]);
  11. L++;
  12. }
  13. while(R > L && block[R] == block[R - 1]) {
  14. ans = max(ans, a[R]);
  15. R--;
  16. }
  17. if(L == R) {
  18. ans = max(ans, a[L]);
  19. L++;
  20. } else {
  21. ans = max(ans, a[L]);
  22. ans = max(ans, a[R]);
  23. L++;
  24. R--;
  25. }
  26. if(L <= R) {
  27. for(int j = block[L]; j <= block[R]; ++j) {
  28. ans = max(ans, maxByBlock[j]);
  29. }
  30. }
  31. return ans;
  32. }
  33.  
  34. void updatePos(int pos, int x) {
  35. int L = block[pos] * blockSize;
  36. int R = min( (block[pos] + 1) * blockSize - 1, N - 1);
  37. a[pos] = x;
  38. maxByBlock[ block[pos] ] = -2e9;
  39. for(int j = L; j <= R; ++j) {
  40. maxByBlock[ block[j] ] = max(maxByBlock[ block[j] ], a[j]);
  41. }
  42. }
  43.  
  44. int main() {
  45. in >> N >> M;
  46. for(int i = 0; i < N; ++i) {
  47. in >> a[i];
  48. }
  49. blockSize = (int) sqrt(N);
  50. for(int i = 0; i < N; ++i) {
  51. block[i] = i / blockSize;
  52. }
  53. for(int i = 0; i < N; ++i) {
  54. int whatBlock = block[i];
  55. maxByBlock[whatBlock] = max(maxByBlock[whatBlock], a[i]);
  56. }
  57. for(int i = 0; i < M; ++i) {
  58. int op;
  59. in >> op;
  60. if(op == 0) {
  61. int L, R;
  62. in >> L >> R;
  63. out << getMax(L - 1, R - 1) << '\n';
  64.  
  65. } else {
  66. int pos, x;
  67. in >> pos >> x;
  68. updatePos(pos - 1, x);
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment