• 洛谷P3901 数列找不同(莫队)


    传送门

    我不管我不管我就是要用莫队

    直接用莫队裸上

     1 //minamoto
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<cmath>
     6 #define ll long long
     7 using namespace std;
     8 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
     9 char buf[1<<21],*p1=buf,*p2=buf;
    10 inline int read(){
    11     #define num ch-'0'
    12     char ch;bool flag=0;int res;
    13     while(!isdigit(ch=getc()))
    14     (ch=='-')&&(flag=true);
    15     for(res=num;isdigit(ch=getc());res=res*10+num);
    16     (flag)&&(res=-res);
    17     #undef num
    18     return res;
    19 }
    20 const int N=100005;
    21 int cnt[N],a[N],rt[N],l,r,n,m,k,s,ans[N],ansn;
    22 struct node{
    23     int l,r,id;
    24 }q[N];
    25 inline bool cmp(node a,node b){
    26     return rt[a.l]==rt[b.l]?rt[a.l]&1?a.r<b.r:a.r>b.r:a.l<b.l;
    27 }
    28 inline void add(int x){
    29     ++cnt[x];
    30     if(cnt[x]==2) ++ansn;
    31 }
    32 inline void del(int x){
    33     --cnt[x];
    34     if(cnt[x]==1) --ansn;
    35 }
    36 int main(){
    37     //freopen("testdata.in","r",stdin);
    38     n=read(),m=read(),s=sqrt(n);
    39     for(int i=1;i<=n;++i) a[i]=read(),rt[i]=(i-1)/s+1;
    40     for(int i=1;i<=m;++i)
    41     q[i].l=read(),q[i].r=read(),q[i].id=i;
    42     sort(q+1,q+1+m,cmp);
    43     l=1,r=0;
    44     for(int i=1;i<=m;++i){
    45         while(l>q[i].l) add(a[--l]);
    46         while(r<q[i].r) add(a[++r]);
    47         while(l<q[i].l) del(a[l++]);
    48         while(r>q[i].r) del(a[r--]);
    49         ans[q[i].id]=ansn;
    50     }
    51     for(int i=1;i<=m;++i) puts(ans[i]?"No":"Yes");
    52     return 0;
    53 }
  • 相关阅读:
    socket的几大异常以及其出现的原因
    将文件打包成apk
    putty失活不挂起运行
    天气免费接口
    内存泄漏
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build
    009 预处理 #pragma once; typedef; #ifdef
    008 区别 : NULL nullptr
    007 头文件 : 作用/组织/包含
    006 整数/浮点数/cpu频率
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/9536930.html
Copyright © 2020-2023  润新知