Advertisement
Saleh127

Light OJ 1085 / Segment Tree

Feb 22nd, 2023
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. /***
  2.  created: 2023-01-05-02.09.28
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define all(we) we.begin(),we.end()
  13. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  14. #define nl '\n'
  15. const ll mod=1e9+7;
  16.  
  17. ll tr[500005];
  18.  
  19. bool cmp(pair<ll,ll>a,pair<ll,ll>b)
  20. {
  21.     if(a.first==b.first) return a.second>b.second;
  22.     return a.first<b.first;
  23. }
  24.  
  25. void treeconstruct(ll node,ll b,ll e)
  26. {
  27.     if(b==e)
  28.     {
  29.         tr[node]=0;
  30.         return;
  31.     }
  32.     ll left = node*2;
  33.     ll right = left|1ll;
  34.     ll mid=(b+e)/2;
  35.     treeconstruct(left,b,mid);
  36.     treeconstruct(right,mid+1,e);
  37.     tr[node]=tr[left]+tr[right];
  38. }
  39.  
  40.  
  41. void update(ll node,ll b,ll e,ll i,ll newv)
  42. {
  43.     if(i>e || i<b) return;
  44.     if(b==e)
  45.     {
  46.         tr[node]=newv;
  47.         return;
  48.     }
  49.     ll left = node*2;
  50.     ll right = left+1;
  51.     ll mid=(b+e)/2;
  52.     update(left,b,mid,i,newv);
  53.     update(right,mid+1,e,i,newv);
  54.     tr[node]=(tr[left]+tr[right])%mod;
  55. }
  56.  
  57. ll queries(ll node,ll b,ll e,ll i,ll j)
  58. {
  59.     if(i>e || j<b) return 0;
  60.     if(b>=i && e<=j) return tr[node];
  61.     ll left = node*2;
  62.     ll right = left+1;
  63.     ll mid=(b+e)/2;
  64.     ll x=queries(left,b,mid,i,j);
  65.     ll y=queries(right,mid+1,e,i,j);
  66.     return (x+y)%mod;
  67. }
  68.  
  69. int main()
  70. {
  71.     ios_base::sync_with_stdio(0);
  72.     cin.tie(0);
  73.     cout.tie(0);
  74.  
  75.  
  76.     test
  77.     {
  78.         ll n,m,i,j,k,l;
  79.  
  80.         cin>>n;
  81.  
  82.         vector<pair<ll,ll>>x;
  83.  
  84.         for(i=1;i<=n;i++)
  85.         {
  86.             cin>>m;
  87.             x.push_back({m,i});
  88.         }
  89.        
  90.         treeconstruct(1,1,n);
  91.  
  92.         sort(all(x),cmp);
  93.  
  94.         for(i=0;i<n;i++)
  95.         {
  96.             l=queries(1,1,n,1,x[i].second);
  97.             update(1,1,n,x[i].second,l+1);
  98.         }
  99.        
  100.         cout<<"Case "<<cs<<": "<<queries(1,1,n,1,n)<<nl;
  101.     }
  102.  
  103.     return 0;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement