Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. /**
  2.     In the name of Allah, the Most Gracious, the Most Merciful.
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. int block, ans;
  9. int fin[100005], cnt[55];
  10.  
  11. struct Query
  12. {
  13.     int L, R, F, I;
  14. };
  15.  
  16. void add(int x) {
  17.     cnt[x]++;
  18. }
  19.  
  20. void sub(int x) {
  21.     cnt[x]--;
  22. }
  23.  
  24. bool compare(Query x, Query y)
  25. {
  26.     if (x.L/block != y.L/block)
  27.         return x.L/block < y.L/block;
  28.  
  29.     return x.R < y.R;
  30. }
  31.  
  32. void fun(int ara[], int n, Query q[], int m)
  33. {
  34.     block = sqrt(n);
  35.  
  36.     sort(q, q + m, compare);
  37.  
  38.     int currL = 0, currR = -1;
  39.  
  40.     for (int i=0; i<m; i++)
  41.     {
  42.         int L = q[i].L, R = q[i].R, ff = q[i].F;
  43.         while (currR < R)
  44.         {
  45.             currR++;
  46.             add(ara[currR]);
  47.         }
  48.  
  49.         while (currR > R)
  50.         {
  51.             sub(ara[currR]);
  52.             currR--;
  53.         }
  54.         while (currL < L)
  55.         {
  56.             sub(ara[currL]);
  57.             currL++;
  58.         }
  59.         while (currL > L)
  60.         {
  61.             currL--;
  62.             add(ara[currL]);
  63.         }
  64.         int pp = 0;
  65.         for(int zz=1; zz<=50; zz++) {
  66.             if(cnt[zz] >= ff) {
  67.                 pp++;
  68.             }
  69.         }
  70.  
  71.         fin[q[i].I] = pp;
  72.     }
  73. }
  74.  
  75. int main() {
  76.     #ifdef _OFFLINE_
  77.         freopen("in.txt", "r", stdin);
  78.     #endif /// _OFFLINE_
  79.  
  80.     int n, m, r, l, f;
  81.     scanf("%d", &n);
  82.  
  83.     int ara[n+5];
  84.     for(int i=0; i<n; i++) {
  85.         scanf("%d", &ara[i]);
  86.     }
  87.  
  88.     scanf("%d", &m);
  89.     Query q[m+5];
  90.  
  91.     for(int i=0; i<m; i++) {
  92.         scanf("%d %d %d", &l, &r, &f);
  93.         q[i].L = l-1;
  94.         q[i].R = r-1;
  95.         q[i].F = f;
  96.         q[i].I = i;
  97.     }
  98.  
  99.     fun(ara, n, q, m);
  100.  
  101.     for(int i=0; i<m; i++) {
  102.         printf("%d\n", fin[i]);
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement