生活随笔
收集整理的這篇文章主要介紹了
                                
Bzoj 2724: [Violet 6]蒲公英(分块)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                            2724: [Violet 6]蒲公英 
 Time Limit: 40 Sec Memory Limit: 512 MB 
 Description 
 Input 
 修正一下 
l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n + 1 
 Output 
 Sample Input 
 6 3 
 1 2 3 2 1 2 
 1 5 
 3 6 
 1 5 
 Sample Output 
 1 
 2 
 1 
 HINT 
 修正下: 
 n <= 40000, m <= 50000 
 Source 
Vani原創
  
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<map>
#define MAXN 50001
using namespace std;
int n,m=
200,q,lastans,belong[MAXN],a[MAXN],tot,id[MAXN],f[
510][
510],cnt[MAXN];
vector<int>g[MAXN];
map<int,int>ma;
int read()
{
int x=
0,f=
1;
char ch=getchar();
while(ch<
'0'||ch>
'9'){
if(ch==
'-')f=-
1;ch=getchar();}
while(ch>=
'0'&&ch<=
'9') x=x*
10+ch-
48,ch=getchar();
return x*f;
}
void slove(
int x)
{
memset(cnt,
0,
sizeof cnt);
int max1=
0,ans=
0;
for(
int i=(x-
1)*m+
1;i<=n;i++){cnt[a[i]]++;
if(cnt[a[i]]>max1||(cnt[a[i]]==max1&&id[a[i]]<id[ans]))max1=cnt[a[i]],ans=a[i];f[x][belong[i]]=ans;}
return ;
}
int query(
int x,
int y,
int k)
{
return upper_bound(g[k].begin(),g[k].end(),y)-lower_bound(g[k].begin(),g[k].end(),x);
}
int slovequery(
int x,
int y)
{
int ans=f[belong[x]+
1][belong[y]-
1];
int max1=query(x,y,ans);
for(
int i=x;i<=min(y,belong[x]*m);i++){
int t=query(x,y,a[i]);
if(t>max1||(t==max1&&id[a[i]]<id[ans]))max1=t,ans=a[i];}
if(belong[x]!=belong[y]){
for(
int i=(belong[y]-
1)*m+
1;i<=y;i++){
int t=query(x,y,a[i]);
if(t>max1||(t==max1&&id[a[i]]<id[ans]))max1=t,ans=a[i];}}
return ans;
}
int main()
{
int x,y;n=read(),q=read();
for(
int i=
1;i<=n;i++){a[i]=read();
if(!ma[a[i]]) ma[a[i]]=++tot,id[tot]=a[i];a[i]=ma[a[i]];g[a[i]].push_back(i);}
for(
int i=
1;i<=n;i++) belong[i]=(i-
1)/m+
1;
for(
int i=
1;i<=belong[n];i++) slove(i);
while(q--){x=read(),y=read();x=(x+lastans-
1)%n+
1,y=(y+lastans-
1)%n+
1;
if(x>y) swap(x,y);lastans=id[slovequery(x,y)];
printf(
"%d\n",lastans);}
return 0;
} 
 
轉載于:https://www.cnblogs.com/nancheng58/p/10068059.html
                            總結
                            
                                以上是生活随笔為你收集整理的Bzoj 2724: [Violet 6]蒲公英(分块)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。