Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define int long long
- #define f first
- #define s second
- #define sz(x) (int)(x.size())
- #define pb push_back
- #define vec vector
- #define all(x) (x).begin(), (x).end()
- #define ld long double
- #define rall(x) (x).rbegin(), (x).rend()
- #define pii pair< int, int >
- #define psi pair< string, int >
- #define pss pair< string, string >
- #define eb emplace_back
- #define lb lower_bound
- #define ub upper_bound
- #define rz resize
- #define ins insert
- #define endl "\n"
- using namespace std;
- //using namespace __gnu_pbds;
- //
- //template <typename T> using ind_set = tree<T, null_type, less<T>,
- //rb_tree_tag, tree_order_statistics_node_update>;
- template <class T>
- istream& operator >> ( istream & in, vec< T > & a )
- {
- for ( auto & i : a )
- in >> i;
- return in;
- }
- template <class T>
- ostream& operator << ( ostream & out, vec< T > & a )
- {
- for ( auto & i : a )
- out << i << ' ';
- return out;
- }
- template < class T, class U >
- istream& operator >> (istream & in, vec< pair < T, U > > & a )
- {
- for ( auto & i : a )
- in >> i.first >> i.second;
- return in;
- }
- template <class T, class U>
- ostream& operator << ( ostream & out, vec< pair < T, U > > & a )
- {
- for ( auto & i : a )
- out << i.f << ' ' << i.s << endl;
- return out;
- }
- vec< int > t (1e5 * 4), a;
- void tree( int v, int vl, int vr )
- {
- if( vl == vr )
- {
- t[v] = a[vl];
- return;
- }
- int vm = ( vl + vr ) / 2;
- tree( v * 2 + 1, vl, vm );
- tree( v * 2 + 2, vm + 1, vr );
- t[v] = t[v * 2 + 1] + t[v * 2 + 2];
- }
- int query( int v, int vl, int vr, int l, int r )
- {
- if( vr < l or r < vl )
- {
- return 0;
- }
- if( vr <= r and l <= vl )
- {
- return t[v];
- }
- int vm = ( vl + vr ) / 2;
- int q1 = query( v * 2 + 1, vl, vm, l, r );
- int q2 = query( v * 2 + 2, vm + 1, vr, l, r );
- return q1 + q2;
- }
- int kth( int v, int vl, int vr, int k )
- {
- if( t[v] < k )
- {
- return -1;
- }
- if( vl == vr )
- {
- return vr;
- }
- int vm = ( vl + vr ) / 2;
- if( k <= t[v * 2 + 1] )
- {
- return kth( v * 2 + 1, vl, vm, k );
- }
- else
- return kth( v * 2 + 2, vm + 1, vr, k - t[v * 2 + 1] );
- }
- void update( int v, int vl, int vr, int pos, int val )
- {
- if( vl == vr )
- {
- if( val == 0 )
- {
- t[v] = 1;
- }
- else
- t[v] = 0;
- return;
- }
- int vm = ( vl + vr ) / 2;
- if( pos <= vm )
- {
- update( v * 2 + 1, vl, vm, pos, val );
- }
- else
- update( v * 2 + 2, vm + 1, vr, pos, val );
- t[v] = t[v * 2 + 1] + t[v * 2 + 2];
- }
- void solve()
- {
- int n;
- cin >> n;
- a.rz( n );
- cin >> a;
- tree( 0, 0, n - 1 );
- int q;
- cin >> q;
- while( q-- )
- {
- char c;
- cin >> c;
- if( c == 'u' )
- {
- int l, r;
- cin >> l >> r;
- update( 0, 0, n - 1, l - 1, r - 1 );
- }
- else
- {
- int l, r, k;
- cin >> l >> r >> k;
- k += query( 0, 0, n - 1, 0, l - 2 );
- int ans = kth( 0, 0, n - 1, k );
- if( ans == -1 )
- {
- cout << -1 << ' ';
- }
- else if( ans > r - 1 )
- {
- cout << -1 << ' ';
- }
- else
- cout << ans + 1 << ' ';
- }
- }
- }
- signed main()
- {
- ios_base::sync_with_stdio( 0 );
- cin.tie( 0 );
- cout.tie( 0 );
- int q = 1;
- //cin >> q;
- while( q-- )
- {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement