zoukankan      html  css  js  c++  java
  • BZOJ-1878: [SDOI2009]HH的项链(莫队算法)

    1878: [SDOI2009]HH的项链

    Time Limit: 4 Sec  Memory Limit: 64 MB
    Submit: 4857  Solved: 2401
    [Submit][Status][Discuss]

    Description

    HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一
    段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此他的项链变得越来越长。有一天,他突然提出了一
    个问题:某一段贝壳中,包含了多少种不同的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只
    好求助睿智的你,来解决这个问题。

    Input

    第一行:一个整数N,表示项链的长度。 
    第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。 
    第三行:一个整数M,表示HH询问的个数。 
    接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。
    N ≤ 50000,M ≤ 200000。

    Output

    M行,每行一个整数,依次表示询问对应的答案。

    Sample Input

    6
    1 2 3 4 3 5
    3
    1 2
    3 5
    2 6

    Sample Output

    2
    2
    4

    HINT

     

    Source

    mmp其实这题简单至极,然而本辣鸡因为一个小于号写成大于号了而且眼神又不咋地,所以浪费了一个小时才搞好fuck
     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX1=5e4+5;
     5 const int MAX2=2e5+5;
     6 int n,m;
     7 int a[MAX1],b[1000005],pos[MAX1],bas,ans,an[MAX2];
     8 struct Node{
     9     int id;
    10     int l,r;
    11     bool operator < (const Node &tt) const {
    12         if (pos[l]!=pos[tt.l])
    13             return pos[l]<pos[tt.l];
    14         return r<tt.r;
    15     }
    16 }que[MAX2];
    17 inline int read(){
    18     int an=0,x=1;char c=getchar();
    19     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
    20     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
    21     return an*x;
    22 }
    23 void update(int x,int y){
    24     ans-=(b[a[x]]==0?0:1);
    25     b[a[x]]+=y;
    26     ans+=(b[a[x]]==0?0:1);
    27 }
    28 int main(){
    29     freopen ("neck.in","r",stdin);
    30     freopen ("neck.out","w",stdout);
    31     int i,j;
    32     n=read();bas=(int)sqrt(n*1.0);
    33     for (i=1;i<=n;i++) a[i]=read(),pos[i]=(i-1)/bas+1;
    34     memset(b,0,sizeof(b));
    35     m=read();
    36     for (i=1;i<=m;i++){
    37         que[i].id=i;
    38         que[i].l=read();que[i].r=read();
    39     }
    40     sort(que+1,que+m+1);
    41     int L=1,R=0;
    42     for (i=1;i<=m;i++){
    43         while (R<que[i].r){R++;update(R,1);}
    44         while (R>que[i].r){update(R,-1);R--;}
    45         while (L>que[i].l){L--;update(L,1);}
    46         while (L<que[i].l){update(L,-1);L++;}
    47         an[que[i].id]=ans;
    48     }
    49     for (i=1;i<=m;i++)
    50         printf("%d
    ",an[i]);
    51     return 0;
    52 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    《人月神话》读后感
    软件工程心得体会(十一)
    Arch + Win10 EFI 引导重装记录
    BurpSuite 的使用
    Wireshark 的使用
    Android 中的反调试技术
    IDA 对 so 的动态调试
    Smail 中的一些点
    IDA 对 SO 的逆向
    动态调试smali代码
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7679250.html
Copyright © 2011-2022 走看看