zoukankan      html  css  js  c++  java
  • [Violet]蒲公英

    description

    在线询问区间众数。

    data range

    [nle 40000,mle 50000,a_ile 10^9 ]

    solution

    自己分块不行于是(\%)(yyb)一发

    神仙题。

    发现众数只可能为块的众数或者剩下的数

    于是离散化后维护块([l,r])的众树和每个树的权值分布
    对于剩下的数直接在(vector)上二分查询即可

    Code

    #include<bits/stdc++.h>
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>
    #include<cstring>
    #include<complex>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<bitset>
    #include<ctime>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #define Cpy(x,y) memcpy(x,y,sizeof(x))
    #define Set(x,y) memset(x,y,sizeof(x))
    #define FILE "2724"
    #define mp make_pair
    #define pb push_back
    #define RG register
    #define il inline
    using namespace std;
    typedef unsigned long long ull;
    typedef vector<int>VI;
    typedef long long ll;
    typedef double dd;
    const int N=40010;
    const int M=10000010;
    const dd eps=1e-5;
    const int inf=2147483647;
    const ll INF=1ll<<60;
    const ll P=100000;
    il ll read(){
      RG ll data=0,w=1;RG char ch=getchar();
      while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
      if(ch=='-')w=-1,ch=getchar();
      while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
      return data*w;
    }
    
    il void file(){
      srand(time(NULL)+rand());
      freopen(FILE".in","r",stdin);
      freopen(FILE".out","w",stdout);
    }
    
    int n,m,blk,Q,a[N],b[N],o[N],len,s[302][302],ans,t[N];
    vector<int>p[N];
    il int query(int l,int r,int x){
      if(!p[x].size())return 0;
      return upper_bound(p[x].begin(),p[x].end(),r)-lower_bound(p[x].begin(),p[x].end(),l);
    }
    int main()
    {
      n=read();Q=read();m=150;blk=(n-1)/m+1;
      for(RG int i=1;i<=n;i++){o[i]=a[i]=read();b[i]=(i-1)/m+1;}
      sort(o+1,o+n+1);len=unique(o+1,o+n+1)-o-1;
      for(RG int i=1;i<=n;i++){
        a[i]=lower_bound(o+1,o+len+1,a[i])-o;
        p[a[i]].push_back(i);
      }  
      for(RG int i=1,ret;i<=blk;i++){
        memset(t,0,sizeof(t));ret=0;
        for(RG int j=i;j<=blk;j++){
          for(RG int k=(j-1)*m+1;k<=n&&k<=j*m;k++){
    	t[a[k]]++;
    	if(t[ret]<t[a[k]]||(t[ret]==t[a[k]]&&a[k]<ret))
    	  ret=a[k];
          }
          s[i][j]=ret;
        }
      }
      
      while(Q--){
        RG int l=(read()+ans-1)%n+1,r=(read()+ans-1)%n+1,cnt;
        if(l>r)swap(l,r);
        if(b[l]==b[r]){
          ans=0;
          for(RG int i=l;i<=r;i++)t[a[i]]=0;
          for(RG int i=l;i<=r;i++){
    	t[a[i]]++;
    	if(t[ans]<t[a[i]]||(t[ans]==t[a[i]]&&a[i]<ans))
    	  ans=a[i];
          }
        }
        else{
          ans=s[b[l]+1][b[r]-1];if(ans)cnt=query(l,r,ans);else cnt=0;
          for(RG int i=l,ret;i==l||i%m!=1;i++){
    	ret=query(l,r,a[i]);
    	if(ret>cnt||(ret==cnt&&a[i]<ans))
    	  ans=a[i],cnt=ret;
          }
          for(RG int i=r,ret;i==r||i%m!=0;i--){
    	ret=query(l,r,a[i]);
    	if(ret>cnt||(ret==cnt&&a[i]<ans))
    	  ans=a[i],cnt=ret;
          }
        }
        ans=o[ans];printf("%d
    ",ans);
      }
      return 0;
    } 
    
    
  • 相关阅读:
    高中信息技术《算法与程序设计VB(选修)》知识要点
    信息技术选修一总结
    文学给人以相爱的力量
    雾霾
    杯子
    递归
    死锁
    高精度计算练习1
    高精度加法的优化
    字符串函数与过程
  • 原文地址:https://www.cnblogs.com/cjfdf/p/9705696.html
Copyright © 2011-2022 走看看