Advertisement
Korotkodul

CF D 1

Aug 15th, 2022 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 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. void cvp(vector <pii> a){
  44.     for (auto p: a){
  45.         cout<<p.first<<' '<<p.second<<"\n";
  46.     }
  47.     cout<<"\n";
  48. }
  49.  
  50. bool sh=1;
  51.  
  52.  
  53. int n,N,k;
  54.  
  55. vector <int> a;
  56.  
  57. const int inf = 1e9;
  58.  
  59. int up2(int x){
  60.     int y = log2(x);
  61.     if (pow(2, y) < x){
  62.         y++;
  63.     }
  64.     y = pow(2, y);
  65.     return y;
  66. }
  67.  
  68. void seep(pii p){
  69.     cout<<"("<<p.first<<","<<p.second<<") ";
  70. }
  71.  
  72. struct tree{
  73.     vector <pii> MN; //{mn, id}
  74.     vector <pii> fr0, fr1; //макс мин на отрезках длины 2 {max mn, id}
  75.     void bld(){
  76.         N = up2(n);
  77.         MN.assign(2 * N - 1, {inf,-1});
  78.         for (int i = N - 1; i < N - 1 + n; ++i){
  79.             int id = i - ( N - 1);
  80.             MN[i] = {a[id], id};
  81.         }
  82.         for (int i = N - 2;i >= 0; --i){
  83.             if (MN[2*i + 1] < MN[2 * i + 2]){
  84.                 MN[i] = MN[2*i + 1];
  85.             }
  86.             else{
  87.                 MN[i] = MN[2 * i + 2];
  88.             }
  89.         }
  90.  
  91.         fr0.assign(N - 1, {0,-1});
  92.         fr1.assign(N - 1, {0,-1});
  93.         /*
  94.         УЧТИ?
  95.         fr0
  96.         * * *...
  97.         fr1
  98.         0 * * ...
  99.         здесь первая не учитывается и это надо учесть: как?
  100.  
  101.         вот как:
  102.         fr0
  103.         левый нижний отрезок (0,1)
  104.         fr1
  105.         левый ниждний отрезок (1,2)
  106.         просто это надо учитывать и все
  107.         */
  108.         //fr0
  109.         for (int i = 0; i + 1 < n; i += 2){
  110.             int mn = a[i], id = i;
  111.             if (a[i] > a[i+1]){
  112.                 mn = a[i+1];
  113.                 id++;
  114.             }
  115.             fr0[i/2 + N/2 - 1] = {mn, id};
  116.         }
  117.  
  118.         for (int i = 1; i + 1 < n; i += 2){
  119.             int mn = a[i], id = i;
  120.             if (a[i] > a[i + 1]){
  121.                 mn = a[i + 1];
  122.                 id++;
  123.             }
  124.             fr1[i/2 + N/2 - 1] = {mn, id};
  125.         }
  126.  
  127.         for (int i = N/2 - 2; i >= 0; --i){
  128.  
  129.             /*fr0[i] = max(fr0[i*2 + 1], fr0[i * 2 + 2]);
  130.             fr1[i] = max(fr1[i*2 + 1], fr1[i * 2 + 2]);*/
  131.             pii p = fr0[i*2 + 1];
  132.             if (fr0[i*2 + 2].first > fr0[i*2 + 1].first){
  133.                 p = fr0[i*2 + 2];
  134.             }
  135.             fr0[i] = p;
  136.         }
  137.  
  138.         for (int i = N/2 - 2; i >= 0; --i){
  139.             pii p = fr1[i*2 + 1];
  140.             if (fr1[i*2 + 2].first > fr1[i*2 + 1].first){
  141.                 p = fr1[i*2 + 2];
  142.             }
  143.             fr1[i] = p;
  144.         }
  145.  
  146.  
  147.         if (sh){
  148.             cout<<"SHOW\n";
  149.             cout<<"MN\n";
  150.             int id=-1;
  151.             for (int lg = 0; lg <= log2(N); ++lg){
  152.                 for (int i = 0; i < pow(2, lg); ++i){
  153.                     id++;
  154.                     seep(MN[id]);
  155.                 }cout<<"\n";
  156.             }
  157.         }
  158.     }
  159.  
  160.  
  161.     void upd(int id){
  162.         id += N - 1;
  163.         MN[id].first = inf;
  164.         while (id > 0){
  165.             id--;
  166.             id /= 2;
  167.             pii p = MN[2 * id + 1];
  168.             if (MN[2 * id + 1].first > MN[2 * id + 2].first){
  169.                 p = MN[2 * id + 2];
  170.             }
  171.             MN[id] = p;
  172.         }
  173.     }
  174. };
  175.  
  176. void slv(){
  177.     tree T;
  178.     T.bld();
  179. }
  180.  
  181.  
  182. int main()
  183. {
  184.     ios::sync_with_stdio(0);
  185.     cin.tie(0);
  186.     cout.tie(0);
  187.     int t=1; if (!sh) cin>>t;
  188.     for (int go = 0; go < t; ++go){
  189.         cin>>n>>k; a.resize(n); for (int &i: a) cin>>i;
  190.         slv();
  191.     }
  192. }
  193. /*
  194. 20 20
  195. 1 4 3 6 3
  196. 1 4 7 4 7
  197. 1 4 3 6 4
  198. 1 4 3 2 3
  199. */
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement