Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. #define int long long
  6. #define f first
  7. #define s second
  8. #define sz(x) (int)(x.size())
  9. #define pb push_back
  10. #define vec vector
  11. #define all(x) (x).begin(), (x).end()
  12. #define ld long double
  13. #define rall(x) (x).rbegin(), (x).rend()
  14. #define pii pair< int, int >
  15. #define psi pair< string, int >
  16. #define pss pair< string, string >
  17. #define eb emplace_back
  18. #define lb lower_bound
  19. #define ub upper_bound
  20. #define rz resize
  21. #define ins insert
  22. #define endl "\n"
  23.  
  24.  
  25. using namespace std;
  26.  
  27. //using namespace __gnu_pbds;
  28. //
  29. //template <typename T> using ind_set = tree<T, null_type, less<T>,
  30. //rb_tree_tag, tree_order_statistics_node_update>;
  31.  
  32. template <class T>
  33. istream& operator >> ( istream & in, vec< T > & a )
  34. {
  35. for ( auto & i : a )
  36. in >> i;
  37. return in;
  38. }
  39.  
  40. template <class T>
  41. ostream& operator << ( ostream & out, vec< T > & a )
  42. {
  43. for ( auto & i : a )
  44. out << i << ' ';
  45. return out;
  46. }
  47.  
  48. template < class T, class U >
  49. istream& operator >> (istream & in, vec< pair < T, U > > & a )
  50. {
  51. for ( auto & i : a )
  52. in >> i.first >> i.second;
  53. return in;
  54. }
  55.  
  56. template <class T, class U>
  57. ostream& operator << ( ostream & out, vec< pair < T, U > > & a )
  58. {
  59. for ( auto & i : a )
  60. out << i.f << ' ' << i.s << endl;
  61. return out;
  62. }
  63.  
  64. vec< int > t (1e5 * 4), a;
  65.  
  66. void tree( int v, int vl, int vr )
  67. {
  68. if( vl == vr )
  69. {
  70. t[v] = a[vl];
  71. return;
  72. }
  73.  
  74. int vm = ( vl + vr ) / 2;
  75.  
  76. tree( v * 2 + 1, vl, vm );
  77. tree( v * 2 + 2, vm + 1, vr );
  78.  
  79. t[v] = t[v * 2 + 1] + t[v * 2 + 2];
  80. }
  81.  
  82. int query( int v, int vl, int vr, int l, int r )
  83. {
  84. if( vr < l or r < vl )
  85. {
  86. return 0;
  87. }
  88. if( vr <= r and l <= vl )
  89. {
  90. return t[v];
  91. }
  92.  
  93. int vm = ( vl + vr ) / 2;
  94.  
  95. int q1 = query( v * 2 + 1, vl, vm, l, r );
  96. int q2 = query( v * 2 + 2, vm + 1, vr, l, r );
  97.  
  98. return q1 + q2;
  99. }
  100.  
  101. int kth( int v, int vl, int vr, int k )
  102. {
  103. if( t[v] < k )
  104. {
  105. return -1;
  106. }
  107.  
  108. if( vl == vr )
  109. {
  110. return vr;
  111. }
  112.  
  113. int vm = ( vl + vr ) / 2;
  114.  
  115. if( k <= t[v * 2 + 1] )
  116. {
  117. return kth( v * 2 + 1, vl, vm, k );
  118. }
  119. else
  120. return kth( v * 2 + 2, vm + 1, vr, k - t[v * 2 + 1] );
  121. }
  122.  
  123. void update( int v, int vl, int vr, int pos, int val )
  124. {
  125. if( vl == vr )
  126. {
  127. if( val == 0 )
  128. {
  129. t[v] = 1;
  130. }
  131. else
  132. t[v] = 0;
  133.  
  134. return;
  135. }
  136.  
  137. int vm = ( vl + vr ) / 2;
  138.  
  139. if( pos <= vm )
  140. {
  141. update( v * 2 + 1, vl, vm, pos, val );
  142. }
  143. else
  144. update( v * 2 + 2, vm + 1, vr, pos, val );
  145.  
  146. t[v] = t[v * 2 + 1] + t[v * 2 + 2];
  147. }
  148.  
  149. void solve()
  150. {
  151. int n;
  152. cin >> n;
  153. a.rz( n );
  154. cin >> a;
  155.  
  156. tree( 0, 0, n - 1 );
  157.  
  158. int q;
  159. cin >> q;
  160.  
  161. while( q-- )
  162. {
  163. char c;
  164. cin >> c;
  165.  
  166. if( c == 'u' )
  167. {
  168. int l, r;
  169. cin >> l >> r;
  170. update( 0, 0, n - 1, l - 1, r - 1 );
  171. }
  172. else
  173. {
  174. int l, r, k;
  175. cin >> l >> r >> k;
  176.  
  177. k += query( 0, 0, n - 1, 0, l - 2 );
  178.  
  179. int ans = kth( 0, 0, n - 1, k );
  180.  
  181. if( ans == -1 )
  182. {
  183. cout << -1 << ' ';
  184. }
  185. else if( ans > r - 1 )
  186. {
  187. cout << -1 << ' ';
  188. }
  189. else
  190. cout << ans + 1 << ' ';
  191. }
  192. }
  193. }
  194.  
  195. signed main()
  196. {
  197. ios_base::sync_with_stdio( 0 );
  198. cin.tie( 0 );
  199. cout.tie( 0 );
  200.  
  201. int q = 1;
  202. //cin >> q;
  203.  
  204. while( q-- )
  205. {
  206. solve();
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement