hdu 1806 Frequent values
2018-06-17 21:46:26来源:未知 阅读 ()
Frequent values
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 25 Accepted Submission(s) : 11
You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100010
using namespace std;
int a[100010];
int ans;
struct node
{
int l,r;
int lcnt,ln,rcnt,rn;
int cnt,num;
}t[100010<<2];
void build(int l,int r,int n)
{
t[n].l=l;
t[n].r=r;
if(l==r)
{
t[n].cnt=t[n].lcnt=t[n].rcnt=1;
t[n].num=t[n].ln=t[n].rn=a[l];
return;
}
int m=(l+r)>>1;
build(l,m,n<<1);
build(m+1,r,n<<1|1);
if(t[n<<1].cnt>=t[n<<1|1].cnt){
t[n].cnt=t[n<<1].cnt;
t[n].num=t[n<<1].num;
}
else{
t[n].cnt=t[n<<1|1].cnt;
t[n].num=t[n<<1|1].num;
}
t[n].lcnt=t[n<<1].lcnt;
t[n].rcnt=t[n<<1|1].rcnt;
t[n].ln=t[n<<1].ln;
t[n].rn=t[n<<1|1].rn;
if(t[n<<1].rn==t[n<<1|1].ln)
{
if(t[n<<1].rcnt+t[n<<1|1].lcnt>t[n].cnt)
{
t[n].cnt=t[n<<1].rcnt+t[n<<1|1].lcnt;
t[n].num=t[n<<1|1].ln;
}
if(t[n<<1].rn==t[n<<1|1].rn){
t[n].rcnt+=t[n<<1].rcnt;
}
if(t[n<<1|1].ln==t[n<<1].ln){
t[n].lcnt+=t[n<<1|1].lcnt;
}
}
}
void query(int l,int r,int n)
{
if(t[n].l==l&&t[n].r==r){
ans=max(t[n].cnt,ans);
return;
}
if(l>=t[n<<1|1].l){
query(l,r,n<<1|1);
}
else if(r<=t[n<<1].r){
query(l,r,n<<1);
}
else{
query(l,t[n<<1].r,n<<1);
query(t[n<<1|1].l,r,n<<1|1);
if(t[n<<1].rn == t[n<<1|1].ln)
{
int ans1=0,ans2=0;
if(a[l]!=t[n<<1].rn)
ans1=t[n<<1].rcnt;
else ans1=t[n<<1].r-l+1;
if(a[r]!=t[n<<1|1].ln)
ans2=t[n<<1|1].lcnt;
else ans2=r-t[n<<1].r;
ans=max(ans,ans1+ans2);
return ;
}
}
}
int main()
{
int n,q;
while(scanf("%d",&n),n)
{
scanf("%d",&q);
for(int i =1;i<=n;i++){
scanf("%d",&a[i]);
}
build(1,n,1);
for(int i = 0;i<q;i++){
int x,y;
scanf("%d%d",&x,&y);
if(x==y){
puts("1");
continue;
}
ans=0;
query(x,y,1);
cout<<ans<<endl;
}
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
