zoukankan      html  css  js  c++  java
  • D-query(莫队)

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

    Input

    • Line 1: n (1 ≤ n ≤ 30000).
    • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
    • Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
    • In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).

    Output

    • For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.

    Example

    Input
    5
    1 1 2 1 3
    3
    1 5
    2 4
    3 5
    
    Output
    3
    2
    3 

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <iomanip>
     6 #include <set>
     7 #include <map>
     8 #include <vector>
     9 #include <queue>
    10 #include <cmath>
    11 #define N 30005
    12 #define ll long long
    13 using namespace std;
    14 
    15 int n,m,k,L,R;
    16 int arr[N],cnt[1000005];
    17 int res;
    18 int sum[200005];
    19 
    20 struct Node{
    21     int l,r,b,ans;
    22     bool operator<(const Node&X)const{
    23         if(ans!=X.ans) return ans<X.ans;
    24         return r<X.r;
    25     }
    26 }A[200005];
    27 
    28 void add(int ee){
    29     if(cnt[arr[ee]]==0) res++;
    30     cnt[arr[ee]]++;
    31 }
    32 
    33 void del(int ee){
    34     cnt[arr[ee]]--;
    35     if(cnt[arr[ee]]==0) res--;
    36 }
    37 
    38 
    39 int main(){
    40     scanf("%d",&n);
    41     for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
    42     scanf("%d",&m);
    43     int big=(int)sqrt(n);
    44     for(int i=1;i<=m;i++){
    45         scanf("%d%d",&A[i].l,&A[i].r);
    46         A[i].b=i;A[i].ans=(A[i].l-1)/big+1;
    47     }
    48     sort(A+1,A+1+m);
    49     L=1,R=0;
    50     for(int i=1;i<=m;i++){
    51         while(L<A[i].l) del(L++);
    52         while(L>A[i].l) add(--L);
    53         while(R<A[i].r) add(++R);
    54         while(R>A[i].r) del(R--);
    55         sum[A[i].b]=res;
    56     }
    57     for(int i=1;i<=m;i++) printf("%d
    ",sum[i]);
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    c# NPOI读取xlsx
    内网搭建代理DNS使用内网域名代替ip地址
    Linux命令行快捷键
    Java基础数组
    Java基础语法
    python3发送邮件的脚本
    Java基础概述
    更多笔记
    Java基础面向对象(一)
    Linux awk抓取IP的两种方式
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11364539.html
Copyright © 2011-2022 走看看