Guest User

Untitled

a guest
Dec 5th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1.           #include <bits/stdc++.h>
  2.           using namespace std;
  3.           #define tani_nachi_ke  ios_base::sync_with_stdio(false); cin.tie(NULL);
  4.           #define tani_nachi_ke  ios_base::sync_with_stdio(false); cin.tie(NULL);
  5.           #define M_PI 3.14159265358979323846
  6.           #define data data_
  7.           #define ff first
  8.           #define ss second
  9.  
  10.          
  11.           vector<int> make_data(int  val)
  12.           {
  13.            vector<int> temp;
  14.            if(val != -1)
  15.             temp.push_back(val);
  16.            return temp;
  17.           }
  18.  
  19.  
  20.           int const N = 600009;
  21.           vector<int> tree[4*N];
  22.           int arr[N];
  23.  
  24.           vector<int> combine(vector<int> const &left,vector<int> const &right)
  25.           {
  26.             vector<int> temp;
  27.             int n = left.size();
  28.             int m = right.size();
  29.             int l = 0,r = 0;
  30.             while(l < n && r < m)
  31.             {
  32.               if(left[l] < right[r])
  33.               {
  34.                 temp.push_back(left[l]);
  35.                 l++;
  36.               }
  37.               else
  38.               {
  39.                 temp.push_back(right[r]);
  40.                 r++;
  41.               }
  42.             }
  43.             while(l < n)
  44.             {
  45.               temp.push_back(left[l]);
  46.               l++;
  47.             }
  48.             while(r < m)
  49.             {
  50.               temp.push_back(right[r]);
  51.               r++;
  52.             }
  53.  
  54.             return temp;
  55.           }
  56.  
  57.  
  58.  
  59.          
  60.  
  61.  
  62.  
  63.  
  64.           void build(int id,int l,int r)
  65.           {
  66.             if(l==r)tree[id]=make_data(arr[l]);
  67.             else
  68.             {
  69.               int mid=(l+r)/2;
  70.               build(2*id,l,mid);
  71.               build(2*id+1,mid+1,r);
  72.               tree[id]=combine(tree[2*id],tree[2*id+1]);
  73.             }
  74.           }
  75.  
  76.  
  77.  
  78.  
  79.           void update(int id,int l,int r,int pos,int val)
  80.           {
  81.             if(l==r)
  82.             {
  83.               tree[id]=make_data(val);
  84.             }
  85.             else
  86.             {
  87.               int mid=(l+r)/2;
  88.               if(pos<=mid)update(2*id,l,mid,pos,val);
  89.               else update(2*id+1,mid+1,r,pos,val);
  90.               tree[id]=combine(tree[2*id],tree[2*id+1]);
  91.             }
  92.           }
  93.  
  94.  
  95.  
  96.           int ans = 0;
  97.           int query(int v, int tl, int tr, int l, int r, int x,int y) {
  98.                 if (l > r)
  99.                     return 0;
  100.                 if (l == tl && r == tr) {
  101.                     return upper_bound(tree[v].begin(),tree[v].end(),y) -
  102.                                 lower_bound(tree[v].begin(),tree[v].end(),x);
  103.                 }
  104.                 int tm = (tl + tr) / 2;
  105.                 return query(v*2, tl, tm, l, min(r, tm), x, y)+
  106.                            query(v*2+1, tm+1, tr, max(l, tm+1), r, x, y);
  107.             }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.           int main()
  114.           {
  115.  
  116.            clock_t begin = clock();
  117.            #ifndef ONLINE_JUDGE
  118.            freopen("input.txt", "r", stdin);
  119.            freopen("output.txt", "w", stdout);
  120.            #endif
  121.  
  122.            tani_nachi_ke
  123.  
  124.            // build(1,1,n);
  125.            //querry(1,1,n,aa,bb);
  126.            //update(1,1,n,pos,new_value); start array indexing from 1
  127.            
  128.            vector<vector<int>> bin;
  129.            int q;
  130.            cin >> q;
  131.            int n = 0;
  132.            while(q--)
  133.            {
  134.               int id;
  135.               cin >> id;
  136.               if(id == 1)
  137.               {
  138.                 cin >> arr[++n];
  139.               }
  140.               else
  141.               {
  142.                 vector<int> temp(4);
  143.                 temp[0] = id;
  144.                 cin >> temp[1] >> temp[2] >> temp[3];
  145.                 bin.push_back(temp);
  146.               }
  147.            }
  148.  
  149.    
  150.            build(1,1,n);
  151.  
  152.            for(auto u : bin)
  153.            {
  154.               ans = 0;
  155.               if(u[0] == 2)
  156.               {
  157.                 cout << query(1,1,n,u[1],u[2],-1,u[3]-1) << '\n';
  158.                 // cout << ans << '\n';
  159.               }
  160.               else if(u[0] == 3)
  161.               {
  162.                cout << query(1,1,n,u[1],u[2],u[3],u[3]) << '\n';
  163.                 // cout << ans << '\n';
  164.               }
  165.               else
  166.               {
  167.                
  168.                cout << query(1,1,n,u[1],u[2],u[3]+1,INT_MAX) << '\n';
  169.                 // cout << ans << '\n';
  170.               }
  171.            }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.           #ifndef ONLINE_JUDGE
  189.           clock_t end = clock();
  190.           cout<<endl<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
  191.           #endif
  192.           return 0;
  193.           }
Add Comment
Please, Sign In to add comment