DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
1.8 KiB

  1. /*
  2. #pragma GCC target ("avx2")
  3. #pragma GCC optimize ("Ofast")
  4. #pragma GCC optimize ("unroll-loops")
  5. */
  6. #include "bits/stdc++.h"
  7. #define iosb ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  8. #define BIT(x) __builtin_popcount(x)
  9. #define all(x) x.begin() , x.end()
  10. #define F first
  11. #define S second
  12. #define pb push_back
  13. using namespace std ;
  14. typedef unsigned long long UL ;
  15. typedef long long L ;
  16. typedef string T ;
  17. typedef int I ;
  18. const I MaxN = 1e5+1 ;
  19. const I MOD = 1e9+7 ;
  20. const I inf = 2e9+7 ;
  21. const L INF = 2e18+7 ;
  22. int t[4*MaxN], a[MaxN], n, m ;
  23. void build(int v, int tl, int tr)
  24. {
  25. if(tl == tr)
  26. {
  27. t[v] = a[tl] ;
  28. return ;
  29. }
  30. int tm = (tl + tr) >> 1 ;
  31. build(v*2, tl, tm) ;
  32. build(v*2+1, tm+1, tr) ;
  33. t[v] = t[v*2] + t[v*2+1] ;
  34. }
  35. void update(int v, int tl, int tr, int l)
  36. {
  37. if(tl == tr && tl == l)
  38. {
  39. t[v] ^= 1;
  40. return ;
  41. }
  42. if(tr < l || l < tl)
  43. return ;
  44. int tm = (tl + tr) >> 1 ;
  45. update(v*2, tl, tm, l) ;
  46. update(v*2+1, tm+1, tr, l) ;
  47. t[v] = t[v*2] + t[v*2+1] ;
  48. }
  49. int idx(int v, int tl, int tr, int k)
  50. {
  51. if(tl == tr)
  52. return tl ;
  53. int tm = (tl + tr) >> 1 ;
  54. if(t[v*2] >= k)
  55. return idx(v*2, tl, tm, k) ;
  56. else
  57. return idx(v*2+1, tm+1, tr, k-t[v*2]) ;
  58. }
  59. int32_t main(int argc, char *argv[])
  60. {
  61. if(argc > 1)
  62. freopen(argv[1], "r", stdin) ;
  63. //freopen(".in" , "r" , stdin) ;
  64. //freopen(".out" , "w" , stdout) ;
  65. cin >> n >> m ;
  66. for(int i = 0 ; i < n ; ++i)
  67. cin >> a[i] ;
  68. build(1, 0, n-1) ;
  69. while(m--)
  70. {
  71. int type, l ;
  72. cin >> type ;
  73. if(type == 1)
  74. {
  75. cin >> l ;
  76. update(1, 0, n-1, l) ;
  77. }
  78. else
  79. {
  80. cin >> l ;
  81. cout << idx(1, 0, n-1, l+1) << '\n' ;
  82. }
  83. }
  84. }