Mephistopheles_

Segment tree

Aug 5th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. //https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/C
  2.  
  3.  
  4. #pragma GCC optimize ("Ofast,unroll-loops")
  5. #pragma GCC target ("avx2,fma")
  6. #include<bits/stdc++.h>
  7. //#include <ext/pb_ds/assoc_container.hpp>
  8. //using namespace __gnu_pbds;
  9.  
  10. using namespace std;
  11. /*typedef tree<long long,null_type,greater<>, rb_tree_tag,\
  12.         tree_order_statistics_node_update> indexed_set;*/
  13. //rope
  14. #define forx(i,a,b) for (long long i = a; i < b; i++)
  15. #define INF 1e10
  16. #define all(x2) begin(x2),end(x2)
  17. #define isz(t) ((ull)(t.size()))
  18. #define last(x) (*(x.end()-1))
  19. #define mod ll(1e9+7)
  20.  
  21. using ll= long long;
  22. using ld= long double;
  23. using ull=unsigned long long;
  24.  
  25. struct point{
  26.     double x,y;
  27.     point(const double & x,const double & y){
  28.         this->x=x;
  29.         this->y=y;
  30.     }
  31.     point operator +(point& b) const{
  32.         return {this->x+b.x,this->y+b.y};
  33.     }
  34.     point operator -(const point& b) const{
  35.         return {this->x-b.x,this->y-b.y};
  36.     }
  37.     point operator *(point& b) const{
  38.         return {this->x*b.x,this->y*b.y};
  39.     }
  40.     point operator /(point& b) const{
  41.         return {this->x/b.x,this->y/b.y};
  42.     }
  43.     double cross(point& b){
  44.         return this->x*b.y-this->y*b.x;
  45.     }
  46.     double cross(const point& a,const point& b){
  47.         return a.x*b.y-a.y*b.x;
  48.     }
  49.     double dott(const point& b) const {
  50.         return this->x*b.x+this->y*b.y;
  51.     }
  52.     double len() const {
  53.         return sqrt(this->dott(*this));
  54.     }
  55.     double len(const point& a){
  56.         return sqrt(a.dott(a));
  57.     }
  58.     double angle(const point& b){
  59.         return acos(dott(b)/len()/b.len());
  60.     }
  61.     double dist_to_line(point p1,point p2,point A){
  62.         return abs(cross(p2-A,p1-A)/len(p1-p2));
  63.     }
  64. };
  65.  
  66. /*cout.setf(ios::fixed);
  67.     cout.precision(6);*/
  68. #define declare ull mid=(left+right)/2,v_left=v*2,v_right=v*2+1
  69. class seg_tree{
  70.     ull start,end,pos,x;
  71.     vector<ull>arr;
  72.     vector<ull>tree;
  73.     vector<ull>num;
  74.     void clear(ull v){
  75.         tree[v]=INF;
  76.         num[v]=0;
  77.     }
  78.     void accum(ull v_left,ull v_right){
  79.  
  80.        if(tree[v_right]<tree[v_left])
  81.             num[v_left]=num[v_right];
  82.        if(tree[v_right]==tree[v_left])
  83.             num[v_left]+=num[v_right];
  84.        tree[v_left]=min(tree[v_right], tree[v_left]);
  85.     }
  86.     void update(ull v,ull v_left,ull v_right){
  87.         clear(v);
  88.         accum(v,v_left);
  89.         accum(v,v_right);
  90.     }
  91.     void build(ull v, ull left, ull right){
  92.         if(left+1==right) {
  93.             tree[v] = arr[left];
  94.             num[v] = 1;
  95.         }
  96.         else{
  97.             declare;
  98.             build(v_left,left,mid);
  99.             build(v_right,mid,right);
  100.             update(v,v_left,v_right);
  101.         }
  102.     }
  103.     void get_tr(ull v, ull left, ull right){
  104.         if(start<=left && right<=end)
  105.             accum(0,v);
  106.         else{
  107.             declare;
  108.             if(start<mid)
  109.                 get_tr(v_left,left,mid);
  110.             if(mid<end)
  111.                 get_tr(v_right,mid,right);
  112.         }
  113.     }
  114.     void change_tr(ull v, ull left, ull right){
  115.         if(left+1==right)
  116.             tree[v]=x;
  117.         else{
  118.             declare;
  119.             if(pos<mid)
  120.                 change_tr(v_left,left,mid);
  121.             else
  122.                 change_tr(v_right,mid,right);
  123.             update(v,v_left,v_right);
  124.         }
  125.     }
  126.  
  127. public:
  128.     seg_tree(vector<ull>&v):tree(isz(v)<<2,0),arr(v),num(isz(v)<<2,0){
  129.         build(1,0,isz(v));
  130.     }
  131.     void change(ull _pos,ull _x){
  132.         pos=_pos;
  133.         x=_x;
  134.         change_tr(1,0,isz(arr));
  135.     }
  136.     pair<ull,ull> get(ull _start, ull _end){
  137.         clear(0);
  138.         start=_start;
  139.         end=_end+1;
  140.         get_tr(1,0,isz(arr));
  141.         return {tree[0],num[0]};
  142.     }
  143. };
  144. int main() {
  145.     ios::sync_with_stdio(false);
  146.     cin.tie(nullptr);
  147.     cout.tie(nullptr);
  148.     ull n,m;
  149.     cin>>n>>m;
  150.     vector<ull>v(n);
  151.     forx(i,0,n)
  152.         cin>>v[i];
  153.     seg_tree tr(v);
  154.     forx(i,0,m){
  155.         ull a,b,c;
  156.         cin>>a>>b>>c;
  157.         if(a==1)
  158.             tr.change(b,c);
  159.         else {
  160.             auto res=tr.get(b, c - 1);
  161.             cout << res.first << ' '<<res.second<<'\n';
  162.         }
  163.     }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment