Mephistopheles_

Tree

Oct 17th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #pragma GCC optimize ("Ofast,unroll-loops")
  2. #pragma GCC target ("avx2,fma")
  3. #include<bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. //rope
  9. #define forx(i,a,b) for (int i = a; i < b; i++)
  10. #define INF 1e9
  11. #define all(x2) begin(x2),end(x2)
  12. #define isz(t) ((int)(t.size()))
  13. #define last(x) (*(x.end()-1))
  14. #define mod ll(1e9+7)
  15. #define ft first
  16. #define sd second
  17. #define pr pair
  18. //#define _GLIBCXX_DEBUG 1
  19. //#define _GLIBCXX_DEBUG_PEDANTIC 1
  20. //#define _FORTIFY_SOURCE 2
  21. //#define sh(x) cerr<<#x<<" = "<<x<<'\n'
  22. //#define debug
  23.  
  24. using ll= long long;//
  25. using ld= long double;
  26. using ull=unsigned long long;
  27.  
  28.  
  29. template<typename T>
  30. ostream& print_range(ostream& os,T begin,T end){
  31.     for(auto it=begin;it!=end;++it)
  32.         os<<*it<<' ';
  33.     os<<'\n';
  34.     return os;
  35. }
  36. template<typename ...T>
  37. ostream& operator<<(ostream& os,const vector<T...>&v){
  38.     return print_range(os,v.begin(),v.end());
  39. }
  40. string to_string(string s){
  41.     return s;
  42. }
  43. template<typename ...Args>
  44. void printer(Args&&... args) {
  45.     (std::cerr << ... <<( to_string(args)+" ")) << '\n';
  46. }
  47.  
  48. template<typename T, typename... Args>
  49. void push_back_vec(std::vector<T>& v, Args&&... args)
  50. {
  51.     (v.push_back(std::forward<Args>(args)), ...);
  52. }
  53.  
  54. template<class ...Args>
  55. void input(Args &... args) {
  56.     (cin>>...>>args);
  57. }
  58.  
  59. template<typename T>
  60. T minimum(T x) {
  61.     return x;
  62. }
  63.  
  64. template<typename T, typename... Pack>
  65. auto minimum(T x, Pack... p) {
  66.     using common = typename std::common_type<T, Pack...>::type;
  67.     return std::min((common) x, (common) minimum(p...));
  68. }
  69.  
  70. template<typename T>
  71. T maximum(T x) {
  72.     return x;
  73. }
  74.  
  75. template<typename T, typename... Pack>
  76. auto maximum(T x, Pack... p) {
  77.     using common = typename std::common_type<T, Pack...>::type;
  78.     return std::max((common) x, (common) maximum(p...));
  79. }
  80.  
  81. string operator*(const string &h, int k) {
  82.     string u;
  83.     forx(i, 0, k)u += h;
  84.     return u;
  85. }
  86.  
  87. class seg{
  88.         struct v{
  89.             int inf=-1;
  90.             v* l=nullptr;
  91.             v* r=nullptr;
  92.         };
  93.         v* n;
  94.  public:
  95.         vector<int> arr;
  96.         seg(seg const &d){
  97.             arr=d.arr;
  98.             n=build(0,isz(arr));
  99.         }
  100.         v* get_n(){
  101.             return n;
  102.         }
  103.         seg(const vector<int>&arr){
  104.             this->arr=arr;
  105.             n=build(0,isz(arr));
  106.         }
  107.         v* build(int l,int r){
  108.             if(l>=r)
  109.                 return nullptr;
  110.             v* x=new v;
  111.             x->inf=arr[(l+r)/2];
  112.             x->l=build(l,(l+r)/2);
  113.             x->r=build((l+r)/2+1,r);
  114.             return x;
  115.         }
  116.         void pris(){
  117.             vector<vector<int>>f(4*isz(arr),vector<int>(isz(arr)*4,INF));
  118.             pe(0,isz(arr)*2,n,f);
  119.             for(auto& a:f){
  120.                 for(int& i:a){
  121.                     if(i==INF)
  122.                         cout<<' ';
  123.                     else
  124.                         cout<<i;
  125.                 }
  126.                 cout<<'\n';
  127.             }
  128.         }
  129.         void pe(int x,int y,v* r,vector<vector<int>>& f){
  130.             f[y][x]=r->inf;
  131.             if(r==n)
  132.                 ++y;
  133.             if(r->l!=nullptr){
  134.                 pe(x+2,y+1,r->l,f);
  135.             }
  136.             if(r==n)
  137.                 y-=2;
  138.             if(r->r!=nullptr){
  139.                 pe(x+2,y-1,r->r,f);
  140.             }
  141.         }
  142.         int ct(v* x){
  143.             if(x==nullptr)
  144.                 return 0;
  145.             if((x->inf)%2==0)
  146.                 return 1+ct(x->l)+ct(x->r);
  147.             return 0+ct(x->l)+ct(x->r);
  148.         }
  149. };
  150. void solution() {
  151.     seg a({0,1,2,3,4,5,6,7,8,9,10});
  152.     a.pris();
  153.     cout<<"Number of even elements: "<<a.ct(a.get_n());
  154.     seg b(a);
  155. }
  156.  
  157. int32_t main() {
  158.     ios::sync_with_stdio(false);
  159.     cin.tie(nullptr);
  160.     cout.tie(nullptr);
  161.     //    cout.setf(ios::fixed);
  162.     //    cout.precision(15);
  163.     //    freopen("input.txt", "r", stdin);
  164.     //    freopen("output.txt", "w", stdout);
  165.     solution();
  166. }
Advertisement
Add Comment
Please, Sign In to add comment