Korotkodul

ДО Е 1

Apr 1st, 2022 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 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.         //+раасмотреть 3 случая пересечения
  78.         if (l > R || r < L){
  79.             return;
  80.         }
  81.         else if (l >= L && r <= R){
  82.             if (cnt == k-1 && Z[id] == 1){
  83.                 ans = id - (N-2);
  84.                 fnd=1;
  85.                 return;
  86.             }
  87.             else if (cnt + Z[id] < k){
  88.                 cnt += Z[id];
  89.                 return;
  90.             }
  91.             else{
  92.                 m = (l+r)/2;
  93.                 getZ(l,m, 2*id+1);
  94.                 if (fnd) return;
  95.                 getZ(m+1,r, 2*id+2);
  96.             }
  97.         }
  98.         else{
  99.             m = (l+r)/2;
  100.             getZ(l,m, 2*id+1);
  101.             if (fnd) return;
  102.             getZ(m+1,r, 2*id+2);
  103.         }
  104.     }
  105.  
  106.     void chg(int id, int nw){//new
  107.         id = N - 2 + id;
  108.         //если ничего не поменялось
  109.         if (nw == 0 && Z[id] == 1){
  110.             return;
  111.         }
  112.         if (nw != 0 && Z[id] == 0){
  113.             return;
  114.         }
  115.         //если поменялось
  116.         if (nw == 0){
  117.             Z[id] = 1;
  118.         }
  119.         else Z[id] = 0;
  120.         while (id > 0 ){
  121.             id = (id-1)/2;
  122.             Z[id] = Z[id*2+1] + Z[id*2+2];
  123.         }
  124.     }
  125. };
  126.  
  127.  
  128. int main()
  129. {
  130.     /*ios::sync_with_stdio(0);
  131.     cin.tie(0);
  132.     cout.tie(0);*/
  133.     tree T;
  134.     T.bld();
  135.     T.sh();
  136.     int M; cin>>M;
  137.     for (int i = 0; i <M;++i){
  138.         char cmd; cin>>cmd;
  139.         if (cmd == 's'){
  140.             cin>>L>>R;
  141.             L--; R--;
  142.             ans=-1;
  143.             fnd=0;
  144.             cnt=0;
  145.             T.getZ(0,N-1,0);
  146.             cout<<ans<<' ';
  147.         }
  148.         else{
  149.             int id,nw;//new
  150.             cin>>id>>nw;
  151.             //id--;
  152.             T.chg(id, nw);
  153.             T.sh();
  154.         }
  155.  
  156.     }
  157. }
  158. /*
  159. 5
  160. 0 0 3 0 2
  161. 3
  162. u 1 5
  163. u 1 0
  164. s 1 5 3
  165.  
  166.  
  167. 5
  168. 0 0 3 0 2
  169. 1
  170. s 1 5 3
  171. */
  172.  
Add Comment
Please, Sign In to add comment