nrk009

MO's

Aug 9th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  5. #define ll long long
  6. #define ld long double
  7. #define pb push_back
  8. #define fe first
  9. #define se second
  10. #define nl "\n"
  11. #define pp pair < ll , ll >
  12. #define sz(x) (ll)x.size()
  13. #define st(x) sort(x.begin(),x.end())
  14. #define rst(x) sort(x.rbegin(), x.rend())
  15. #define all(x) x.begin(),x.end()
  16. long double pi = 3.14159265358979323;
  17.  
  18. const double EPS = 1e-12;
  19. const int N = 3e4 + 5;
  20. const int Q = 2e5 + 1;
  21. const int mod = 1e9 + 7;
  22.  
  23. ll bsize , a[N],cnt,ans[Q];
  24. ll q , n;
  25. ll mp[1000001];
  26. struct node{
  27.  
  28.    int l , r , idx;
  29. };
  30. node query[Q];
  31. void init(){
  32.    for(int i = 0 ; i < q ; i++ ){
  33.       ll l , r;
  34.       cin >> l >> r ;
  35.       query[i].l = l;
  36.       query[i].r = r;
  37.       query[i].idx = i;
  38.     }
  39. }
  40. bool cmp(node q1 , node q2){
  41.    if(q1.l/bsize == q2.l/bsize)return q1.r < q2.r;
  42.    return q1.l < q2.l;
  43. }
  44. /*bool cmp(node q1, node q2)
  45. {
  46.     int l1 = q1.l / n;
  47.     int l2 = q2.l / n;
  48.     if(l1 == l2)
  49.     return q1.r < q2.r;
  50.     else return l1 < l2;
  51. }
  52. */
  53. void add(ll val){
  54.    mp[val]++;
  55.    if(mp[val] == 1)
  56.       cnt++;
  57. }
  58. void remove(ll val){
  59.    mp[val]--;
  60.    if(mp[val] == 0)
  61.       cnt--;
  62. }  
  63. void Query(){
  64.     ll lcurr  = 1 , rcurr =  0;
  65.     for(int i = 0 ; i < q; i++ ){
  66.       ll l = query[i].l;
  67.       ll r = query[i].r;
  68.       while(l < lcurr){
  69.          lcurr--;
  70.          add(a[lcurr]);
  71.       }
  72.       while(r > rcurr){
  73.          rcurr++;
  74.          add(a[rcurr]);
  75.       }
  76.       while(l > lcurr){
  77.          remove(a[lcurr]);
  78.          lcurr++;
  79.       }
  80.       while(r < rcurr){
  81.          remove(a[rcurr]);
  82.          rcurr--;
  83.       }
  84.      
  85.       ans[query[i].idx] = cnt;
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91.     fast;
  92.  
  93.     cin >> n ;
  94.     bsize = sqrt(n);
  95.    
  96.     for(int i = 1 ;  i <= n; i++ ){
  97.       cin >> a[i];
  98.     }
  99.     cin >> q;
  100.     init();
  101.    
  102.     sort(query   , query + q,cmp);
  103.    Query();
  104.     for(int i = 0 ;  i < q ; i++ )
  105.       cout << ans[i] << "\n";
  106.  
  107.    
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.    
  130.     return 0;
  131.    
  132. }
Advertisement
Add Comment
Please, Sign In to add comment