Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- int N,L,R,ans,cnt,k;
- bool fnd; //found k-й zero
- struct tree{
- vector <int> Z;
- void bld(){
- cin>>N;
- int rawN=N;
- N = pow(2, ceil(log2(N)) ) ;
- //cout<<"N= "<<N<<"\n";
- Z.assign(2*N-1,0);
- for (int i = N-1; i < N-1 + rawN;++i){
- int d; cin>>d;
- if (d == 0) Z[i]=1;
- }
- for (int i = N-2; i >=0;--i) Z[i] = Z[2*i+1] + Z[2*i+2];
- //cv(Z);
- }
- void sh(){
- int t = log2(N);
- int id=-1;
- for (int i=0;i<=t;++i){
- for (int j=0;j<pow(2,i);++j){
- id++; cout<<Z[id]<<" ";
- }cout<<"\n";
- }
- }
- void getZ(int l, int r, int id){
- int m;
- //cout<<"l r = "<<l<<' '<<r<<"\n";
- //cout<<"id = "<<id<<"\n";
- //+раасмотреть 3 случая пересечения
- if (l > R || r < L){
- //cout<<"A\n";
- return;
- }
- else if (l >= L && r <= R){
- //cout<<"cnt Z[id] k = "<<cnt<<' '<<Z[id]<<' '<<k<<"\n";
- if (cnt == k-1 && l == r && Z[id] == 1){
- //cout<<"BINGO\n";
- ans = id - (N-2);
- fnd=1;
- return;
- }
- else if (cnt + Z[id] < k){
- //cout<<"C\n";
- cnt += Z[id];
- return;
- }
- else{
- //cout<<"D\n";
- m = (l+r)/2;
- getZ(l,m, 2*id+1);
- if (fnd) return;
- getZ(m+1,r, 2*id+2);
- }
- }
- else{
- //cout<<"E\n";
- m = (l+r)/2;
- getZ(l,m, 2*id+1);
- if (fnd) return;
- getZ(m+1,r, 2*id+2);
- }
- }
- void chg(int id, int nw){//new
- id = N - 2 + id;
- //если ничего не поменялось
- if (nw == 0 && Z[id] == 1){
- return;
- }
- if (nw != 0 && Z[id] == 0){
- return;
- }
- //если поменялось
- if (nw == 0){
- Z[id] = 1;
- }
- else Z[id] = 0;
- while (id > 0 ){
- id = (id-1)/2;
- Z[id] = Z[id*2+1] + Z[id*2+2];
- }
- }
- };
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- tree T;
- T.bld();
- //T.sh();
- int M; cin>>M;
- for (int i = 0; i <M;++i){
- char cmd; cin>>cmd;
- if (cmd == 's'){
- cin>>L>>R>>k;
- L--; R--;
- ans=-1;
- fnd=0;
- cnt=0;
- T.getZ(0,N-1,0);
- cout<<ans<<' ';
- }
- else{
- int id,nw;//new
- cin>>id>>nw;
- //id--;
- T.chg(id, nw);
- //T.sh();
- }
- }
- }
Add Comment
Please, Sign In to add comment