YEZAELP

CUBE-225: Humanity Has Declined

Jul 13th, 2021 (edited)
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int INF = 2e9;
  5. const int N = 2e5 + 10;
  6. const int K = 2e5 + 10;
  7.  
  8. int num[K], ar[N], ed[N];
  9. int n, k, q, s = 1, e, cnt;
  10.  
  11. void Move(){
  12.     while(cnt == k and s <= e){
  13.         ed[s] = e;
  14.         num[ ar[s] ] --;
  15.         if(num[ ar[s] ] == 0 and ar[s] <= k)
  16.             cnt --;
  17.         s ++;
  18.     }
  19. }
  20.  
  21. int main(){
  22.  
  23.     scanf("%d%d%d", &n, &k, &q);
  24.  
  25.     for(int i=1;i<=n;i++){
  26.         scanf("%d", &ar[i]);
  27.         ed[i] = INF;
  28.     }
  29.  
  30.     for(e=1;e<=n;e++){
  31.         if(num[ ar[e] ] == 0 and ar[e] <= k)
  32.             cnt ++;
  33.         num[ ar[e] ] ++;
  34.         Move();
  35.     }
  36.     Move();
  37.  
  38.     for(int i=1;i<=q;i++){
  39.         int l, r;
  40.         scanf("%d%d", &l, &r);
  41.         if(ed[l] <= r)
  42.             printf("YES");
  43.         else
  44.             printf("NO");
  45.         printf("\n");
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
  51. /**
  52.  
  53. num[x] คือ x มีกี่ตัว
  54. ed[i] คือ ตำแหน่งที่น้อยที่สุดหากเริ่มที่ i และมีตัวเลขในช่วง [1, k] ครบ
  55. cnt คือ จำนวนของตัวเลขในช่วง [1, k] ที่แตกต่างกันทั้งหมด
  56.  
  57. ใช้ two pointer คือ ตำแหน่งเริ่มต้น และตำแหน่งจบ
  58. ว่าหากเริ่มที่ i ควรจบที่ตำแหน่งไหน
  59.  
  60. */
Add Comment
Please, Sign In to add comment