Light OJ 1339 Strongest Community(分块暴力)

In a strange city, houses are built in a straight line one after another. There are several communities in the city. Each community consists of some consecutivehouses such that every house belongs to exactly one community.
The houses are numbered from 1 to n, and the communities are numbered from 1 to c.

Now some inspectors want to find the strongest community considering all houses from i to j. A community is strongest if maximum houses in the range belong to this community. So, there can be more than one strongest community
in the range. So, they want to know the number of houses that belong to the strongest community. That‘s why they are seeking your help.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing three integers n (1 ≤ n ≤ 105), c (1 ≤ c ≤ n) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers (each between 1 and c)
denoting the communities the houses belong to. You can assume that the input follows the restrictions given above, and there are exactly c communities.

Each of the next q lines contains two integers i and j (1 ≤ i ≤ j ≤ n) denoting that the inspectors are asking for the result considering houses from i to j(inclusive).

Output

For each case, print the case number in a single line. Each of the next q lines should contain the number of houses that belong to the strongest community considering houses from i to j. The result should
be listed in the same order as they are given in input.

Sample Input

Output for Sample Input


2

10 3 4

1 1 1 3 3 3 3 2 2 2

1 5

1 6

1 7

7 9

3 3 1

3 2 1

1 1


Case 1:

3

3

4

2

Case 2:

1

Note

Dataset is huge, use faster

题意:询问区间出现次数最多的数字出现次数

分析:线段树不会做,分块暴力吧,我们统计区间中数子出现次数

此时要统计两个信息:Add操作:增加一个数字会使出现频率变化,相应要修改最值

Sub操作:删除一个数字,如果删除数字后还有其他数达到相同水平,才改变最值

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=1e5+100;
int pos[maxn],a[maxn];
int h[maxn],num[maxn];
int ans[maxn];
struct node{
    int l,r;
    int id;
}q[maxn];
int t,n,c,m;
int sum;
int cmp(node l1,node l2)
{
    if(pos[l1.l]==pos[l2.l])
        return l1.r<l2.r;
    return pos[l1.l]>pos[l2.l];
}
void Add(int x)
{
    int val=a[x];
    h[num[val]]--;
    num[val]++;
    h[num[val]]++;
    if(num[val]>sum)
        sum=num[val];
}
void Sub(int x)
{
    int val=a[x];
    h[num[val]]--;
    num[val]--;
    h[num[val]]++;
    if(sum==num[val]+1&&!h[num[val]+1])
        sum=num[val];
}
int main()
{
    int x,y;
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&c,&m);
        CLEAR(h,0);
        CLEAR(num,0);
        int block=sqrt(n*1.0+0.5);
        REPF(i,1,n)
        {
            scanf("%d",&a[i]);
            pos[i]=(i-1)/block+1;
        }
        REP(i,m)
        {
            scanf("%d%d",&x,&y);
            q[i].l=x;q[i].r=y;
            q[i].id=i;
        }
        sort(q,q+m,cmp);
        sum=0;int L=1,R=0;
        printf("Case %d:\n",cas++);
        for(int i=0;i<m;i++)
        {
            int l=q[i].l;
            int r=q[i].r;
            while(R>r) Sub(R--);
            while(R<r) Add(++R);
            while(L>l) Add(--L);
            while(L<l) Sub(L++);
            ans[q[i].id]=sum;
        }
        for(int i=0;i<m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 18:00:31

Light OJ 1339 Strongest Community(分块暴力)的相关文章

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

Light OJ 1114 Easily Readable 字典树

题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 只要满足每个单词的首尾字符一样 中间顺序可以变化 思路:每个单词除了首尾 中间的字符排序 然后插入字典树 记录每个单词的数量 输入一个句子 每个单词也排序之后查找 根据乘法原理 答案就是每个单词的数量之积 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm>

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都