Korotkodul

ДО Е OK

Apr 1st, 2022 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17.     for (auto x: v) cout<<x<<' ';
  18.     cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22.     for (auto x: v) cout<<x<<' ';
  23.     cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28.     for (auto x: v) cv(x);
  29.     cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33.     for (bool x: v) cout<<x<<' ';
  34.     cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string>  v){
  38.     for (auto a: v){
  39.         cout<<a<<"\n";
  40.     }
  41. }
  42.  
  43. int N,L,R,ans,cnt,k;
  44.  
  45. bool fnd; //found k-й zero
  46.  
  47. struct tree{
  48.     vector <int> Z;
  49.  
  50.     void bld(){
  51.         cin>>N;
  52.         int rawN=N;
  53.         N = pow(2, ceil(log2(N)) ) ;
  54.         //cout<<"N= "<<N<<"\n";
  55.         Z.assign(2*N-1,0);
  56.         for (int i = N-1; i < N-1 + rawN;++i){
  57.             int d; cin>>d;
  58.             if (d == 0) Z[i]=1;
  59.         }
  60.         for (int i = N-2; i >=0;--i) Z[i] = Z[2*i+1] + Z[2*i+2];
  61.         //cv(Z);
  62.     }
  63.  
  64.     void sh(){
  65.         int t = log2(N);
  66.         int id=-1;
  67.         for (int i=0;i<=t;++i){
  68.             for (int j=0;j<pow(2,i);++j){
  69.                 id++; cout<<Z[id]<<" ";
  70.             }cout<<"\n";
  71.         }
  72.     }
  73.  
  74.     void getZ(int l, int r, int id){
  75.         int m;
  76.         //cout<<"l r = "<<l<<' '<<r<<"\n";
  77.         //cout<<"id = "<<id<<"\n";
  78.         //+раасмотреть 3 случая пересечения
  79.         if (l > R || r < L){
  80.             //cout<<"A\n";
  81.             return;
  82.         }
  83.         else if (l >= L && r <= R){
  84.             //cout<<"cnt Z[id] k = "<<cnt<<' '<<Z[id]<<' '<<k<<"\n";
  85.             if (cnt == k-1 && l == r && Z[id] == 1){
  86.                 //cout<<"BINGO\n";
  87.                 ans = id - (N-2);
  88.                 fnd=1;
  89.                 return;
  90.             }
  91.             else if (cnt + Z[id] < k){
  92.                 //cout<<"C\n";
  93.                 cnt += Z[id];
  94.                 return;
  95.             }
  96.             else{
  97.                 //cout<<"D\n";
  98.                 m = (l+r)/2;
  99.                 getZ(l,m, 2*id+1);
  100.                 if (fnd) return;
  101.                 getZ(m+1,r, 2*id+2);
  102.             }
  103.         }
  104.  
  105.         else{
  106.             //cout<<"E\n";
  107.             m = (l+r)/2;
  108.             getZ(l,m, 2*id+1);
  109.             if (fnd) return;
  110.             getZ(m+1,r, 2*id+2);
  111.         }
  112.     }
  113.  
  114.     void chg(int id, int nw){//new
  115.         id = N - 2 + id;
  116.         //если ничего не поменялось
  117.         if (nw == 0 && Z[id] == 1){
  118.             return;
  119.         }
  120.         if (nw != 0 && Z[id] == 0){
  121.             return;
  122.         }
  123.         //если поменялось
  124.         if (nw == 0){
  125.             Z[id] = 1;
  126.         }
  127.         else Z[id] = 0;
  128.         while (id > 0 ){
  129.             id = (id-1)/2;
  130.             Z[id] = Z[id*2+1] + Z[id*2+2];
  131.         }
  132.     }
  133. };
  134.  
  135.  
  136. int main()
  137. {
  138.     ios::sync_with_stdio(0);
  139.     cin.tie(0);
  140.     cout.tie(0);
  141.     tree T;
  142.     T.bld();
  143.     //T.sh();
  144.     int M; cin>>M;
  145.     for (int i = 0; i <M;++i){
  146.         char cmd; cin>>cmd;
  147.         if (cmd == 's'){
  148.             cin>>L>>R>>k;
  149.             L--; R--;
  150.             ans=-1;
  151.             fnd=0;
  152.             cnt=0;
  153.             T.getZ(0,N-1,0);
  154.             cout<<ans<<' ';
  155.         }
  156.         else{
  157.             int id,nw;//new
  158.             cin>>id>>nw;
  159.             //id--;
  160.             T.chg(id, nw);
  161.             //T.sh();
  162.         }
  163.  
  164.     }
  165. }
Add Comment
Please, Sign In to add comment