Advertisement
Saleh127

Light OJ 1188/Spoj D-query - Segment Tree

Jul 7th, 2022
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. /***
  2.  created: 2022-07-08-00.09.35
  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 test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16.  
  17. ll a[200005];
  18. ll tree1[800005];
  19.  
  20. //void treeconstruct(ll node,ll b,ll e)
  21. //{
  22. //
  23. //    if(b==e)
  24. //    {
  25. //        tree1[node]=a[b];
  26. //        return;
  27. //    }
  28. //
  29. //    ll left = node*2;
  30. //    ll right = node*2 + 1ll;
  31. //
  32. //    ll mid=(b+e)/2;
  33. //
  34. //    treeconstruct(left,b,mid);
  35. //    treeconstruct(right,mid+1,e);
  36. //
  37. //    tree1[node]=tree1[left]+tree1[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.  
  45.     if(b==e)
  46.     {
  47.         tree1[node]=newv;
  48.         return;
  49.     }
  50.  
  51.     ll left = node*2;
  52.     ll right = node*2 + 1ll;
  53.  
  54.     ll mid=(b+e)/2;
  55.  
  56.     update(left,b,mid,i,newv);
  57.  
  58.     update(right,mid+1,e,i,newv);
  59.  
  60.     tree1[node]=tree1[left]+tree1[right];
  61.  
  62. }
  63.  
  64. ll queries(ll node,ll b,ll e,ll i,ll j)
  65. {
  66.     if(i>e || j<b) return 0;
  67.  
  68.     if(b>=i && e<=j) return tree1[node];
  69.  
  70.     ll left = node*2;
  71.     ll right = node*2 + 1ll;
  72.  
  73.     ll mid=(b+e)/2;
  74.  
  75.     ll x=queries(left,b,mid,i,j);
  76.  
  77.     ll y=queries(right,mid+1,e,i,j);
  78.  
  79.     return x+y;
  80.  
  81. }
  82.  
  83. ll ans[200005];
  84.  
  85. int main()
  86. {
  87.     ios_base::sync_with_stdio(0);
  88.     cin.tie(0);
  89.     cout.tie(0);
  90.  
  91.     test
  92.     {
  93.         ll n,m,i,j,k,l,q;
  94.  
  95.         cin>>n>>q;
  96.  
  97.         for(i=1; i<=n; i++)
  98.         {
  99.             cin>>a[i];
  100.         }
  101.  
  102.  
  103.  
  104.         vector<array<ll,3>>qr;
  105.  
  106.         for(i=0; i<q; i++)
  107.         {
  108.             cin>>j>>k;
  109.  
  110.             qr.push_back({k,j,i});
  111.         }
  112.  
  113.         sort(qr.begin(),qr.end());
  114.  
  115.         map<ll,ll>x;
  116.  
  117.         j=0;
  118.  
  119.         for(i=1; i<=n; i++)
  120.         {
  121.             if(x[a[i]]>0)
  122.             {
  123.                 update(1,1,n,x[a[i]],0ll);
  124.             }
  125.  
  126.             update(1,1,n,i,1ll);
  127.  
  128.             x[a[i]]=i;
  129.  
  130.             for(; j<q; j++)
  131.             {
  132.                 if(qr[j][0]==i)
  133.                 {
  134.                     ans[qr[j][2]]=queries(1,1,n,qr[j][1],qr[j][0]);
  135.                 }
  136.                 else break;
  137.             }
  138.         }
  139.  
  140.         cout<<"Case "<<cs<<":"<<nl;
  141.  
  142.         for(i=0; i<q; i++)
  143.         {
  144.             cout<<ans[i]<<nl;
  145.         }
  146.     }
  147.  
  148.  
  149.     get_lost_idiot;
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement