内部赛3 F Frequent values
2013-09-11 14:08
316 查看
Frequent values
Time Limit: 1000MS Memory limit: 65536K
题目描述
You are given asequence 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[/i]). For each query, determine the most frequent value
among the integers ai , ... ,
aj.
输入
The input consists of several testcases. Each test case starts with a line containing two
integers n and q (1
≤ n, q ≤ 100000[/i]). The next line contains n integers a1 , ... ,
an (-100000
≤ ai ≤
100000[/i], for each i
∈ {1, ..., n}[/i]) separated by spaces. You can assume that for
each i
∈ {1, ..., n-1}: ai ≤
ai+1[/i].
The following q lines
contain one query each, consisting of two integers i and j (1
≤ i ≤ j ≤ n[/i]), which indicate the boundary indices for the
query.
The last test case is followed by a line containing a
single 0[/i].
输出
For each query, print one line withone integer: The number of occurrences of the most frequent value
within the given range.
示例输入
10 3 -1 -1 1 1 1 1 3 10 10 10 2 3 1 10 5 10 0
示例输出
1 4 3
这个题题意简单我首先是想到的是线段树,在写代码时思路他一定要理清,不然很容易在递归中陷进去,代码有点长,不过听大宝说还有简单的方法,等学会了在贴一遍吧!
代码:
#include<stdio.h>
#define N 100005
struct node
{
int l,r,count,num;
int rnum,lnum;
int lcount,rcount;
}tree[N*4];
int a
,MAX;
void maketree(int t,int l,int r)
{
int mid,sum;
tree[t].l=l;
tree[t].r=r;
if(l==r)
{
tree[t].count=tree[t].lcount=tree[t].rcount=1;
tree[t].num=tree[t].lnum=tree[t].rnum=a[l];
return;
}
mid=(l+r)/2;
maketree(2*t,l,mid);
maketree(2*t+1,mid+1,r);
if(tree[2*t].count>tree[2*t+1].count)
{
tree[t].count=tree[2*t].count;
tree[t].num=tree[2*t].num;
}
else
{
tree[t].count=tree[2*t+1].count;
tree[t].num=tree[2*t+1].num;
}
tree[t].lnum=tree[2*t].lnum;
tree[t].lcount=tree[2*t].lcount;
tree[t].rnum=tree[2*t+1].rnum;
tree[t].rcount=tree[2*t+1].rcount;
if(tree[2*t].rnum==tree[2*t+1].lnum)
{
sum=tree[2*t].rcount+tree[2*t+1].lcount;
if(sum>tree[t].count)
{
tree[t].count=sum;
tree[t].num=tree[2*t].rnum;
}
if(tree[2*t].lnum==tree[2*t+1].lnum)
tree[t].lcount+=tree[2*t+1].lcount;
if(tree[2*t].rnum==tree[2*t+1].rnum)
tree[t].rcount+=tree[2*t].rcount;
}
}
void search(int t,int l,int r)
{
int mid;
int sum1,sum2;
if(tree[t].l==l&&tree[t].r==r)
{
if(tree[t].count>MAX)
MAX=tree[t].count;
return;
}
if(r<=tree[2*t].r)
search(2*t,l,r);
else if(l>=tree[2*t+1].l)
search(2*t+1,l,r);
else
{
mid=(tree[t].l+tree[t].r)/2;
search(2*t,l,mid);
search(2*t+1,mid+1,r);
if(tree[2*t].rnum ==
tree[2*t+1].lnum)
{
if(a[l]!=tree[2*t].rnum)
sum1=tree[2*t].rcount;
else
sum1=mid-l+1;
if(a[r]!=tree[2*t+1].lnum) sum2=tree[2*t+1].lcount;
else
sum2=r-mid;
if(sum1+sum2 > MAX) MAX=sum1+sum2;
}
}
}
int main()
{
int n,m;
int i,j;
int x,y;
while(scanf("%d%d",&n,&m),n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
maketree(1,1,n);
for(j=0;j<m;j++)
{
MAX=0;
scanf("%d%d",&x,&y);
search(1,x,y);
printf("%d\n",MAX);
}
}
return 0;
}
相关文章推荐
- poj&nbsp;3368&nbsp;Frequent&nbsp;values(线段树)
- poj&nbsp;3368&nbsp;Frequent&nbsp;values(非常变…
- 内部赛3 B Black and white painti…
- hdu 1806 Frequent values 线段…
- 内部赛 3 D Deli Deli
- Sublime&nbsp;Text&nbsp;3&nbsp;及插件
- Real Racing 3 v1.…
- sublime text 3 破解
- 编译原理笔记3 编译程序
- Php&nbsp;session内部执行流程的再…
- Bootstrap3&nbsp;兼容IE8&nbsp;初步
- 3&nbsp;Nginx的基本配置和优化
- CSS&nbsp;3&nbsp;的动画功能
- 【原創】python sqlite3&nbsp…
- Anonymous Inner Class (匿名内部…
- Php&nbsp;session内部执行流程的再…
- 【转载】mybatis3 添加ehcache支持
- Java学习笔记3 原生数据类型 Primi…
- 探索Lua5.2内部实现:虚拟机指令(3) Upvalues & Globals
- Kindle&nbsp;3&nbsp;原生系统拼音输入法安装…