fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "AILIME"
  33.  
  34. const int MOD = 1e9 + 7;
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 2e4 + 5;
  38. const int BLOCK_SIZE = 188;
  39. mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
  40. int random(int l, int r) {
  41. return l + rd() % (r - l + 1);
  42. }
  43. int n, q, B[N];
  44. ll a[N], lazy[N / BLOCK_SIZE + 5];
  45. unordered_map<ll, int> sus[N / BLOCK_SIZE + 1];
  46.  
  47. void update(int l, int r, int v) {
  48. int blockL = B[l];
  49. int blockR = B[r];
  50. if (blockL + 1 >= blockR) {
  51. FOR(i, l, r) {
  52. sus[B[i]][a[i]]--;
  53. a[i] += v;
  54. sus[B[i]][a[i]]++;
  55. }
  56. return;
  57. }
  58. FOR(i, l, (blockL + 1) * BLOCK_SIZE - 1) {
  59. sus[blockL - 1][a[i]]--;
  60. a[i] += v;
  61. sus[blockL - 1][a[i]]++;
  62. }
  63. FOR(i, blockR * BLOCK_SIZE, r) {
  64. sus[blockR][a[i]]--;
  65. a[i] += v;
  66. sus[blockR][a[i]]++;
  67. }
  68. FOR(i, blockL + 1, blockR - 1) lazy[i] += v;
  69. }
  70.  
  71. int freq(int l, int r, ll x) {
  72. int blockL = B[l];
  73. int blockR = B[r];
  74. if (blockL + 1 >= blockR) {
  75. int cnt = 0;
  76. FOR(i, l, r) if (a[i] + lazy[B[i]] == x)
  77. cnt++;
  78. return cnt;
  79. }
  80. int cnt = 0;
  81. FOR(i, l, (blockL + 1) * BLOCK_SIZE - 1) if (a[i] + lazy[blockL - 1] == x)
  82. cnt++;
  83. FOR(i, blockR * BLOCK_SIZE, r) if (a[i] + lazy[blockR] == x)
  84. cnt++;
  85. FOR(i, blockL + 1, blockR - 1) if (sus[i].find(x - lazy[i]) != sus[i].end())
  86. cnt += sus[i][x - lazy[i]];
  87. return cnt;
  88. }
  89.  
  90. void init(void) {
  91. cin >> n >> q;
  92. REP(i, n) {
  93. cin >> a[i];
  94. B[i] = i / BLOCK_SIZE;
  95. sus[B[i]][a[i]]++;
  96. }
  97. }
  98.  
  99. void process(void) {
  100. int t, l, r, v;
  101. while(q--) {
  102. cin >> t >> l >> r;
  103. l--; r--;
  104. if (t == 1) {
  105. cin >> v;
  106. update(l, r, v);
  107. } else {
  108. bool ok = false;
  109. REP(tries, 20) {
  110. int i = random(l, r);
  111. int c = freq(l, r, a[i] + lazy[B[i]]);
  112. if (2 * c > (r - l + 1)) {
  113. ok = true;
  114. cout << a[i] + lazy[B[i]] << '\n';
  115. break;
  116. }
  117. }
  118. if (!ok) cout << "IMPOSSIBLE\n";
  119. }
  120. }
  121. }
  122.  
  123. int main() {
  124. ios_base::sync_with_stdio(0);
  125. cin.tie(0); cout.tie(0);
  126. if (fopen(task".inp", "r")) {
  127. freopen(task".inp", "r", stdin);
  128. freopen(task".out", "w", stdout);
  129. }
  130. int tc = 1;
  131. // cin >> tc;
  132. while(tc--) {
  133. init();
  134. process();
  135. }
  136. return 0;
  137. }
  138.  
  139.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty