Light OJ 1339 Strongest Community (sub-block violence)

Source: Internet
Author: User
Tags cas

In a strange city, houses is built in a straight line one after another. There is several communities in the city. Each community consists of some consecutivehouses such, every house belongs to exactly one community . The houses is numbered from 1 to N, and the communities is 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 is 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 is 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 Communiti Es the houses belong to. You can assume that the input follows the restrictions given above, and there is exactly C communities.

Each of the next Q lines contains II integers i and J (1≤i≤j≤n) denoting that the Inspec Tors is asking for the result of considering houses from I to J(inclusive).

Output

For each case, print the case number in a. Each of the next Q lines should contain the number of houses this belong to the strongest community considering H Ouses from i to J. The result should is listed in the same order as they is 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


Test instructions: Ask for the number of occurrences with the most number of interval occurrences

Analysis: Line tree does not do, block violence, we count the number of sub-occurrences in the interval

At this point to count two information: Add operation: Add a number will make the frequency changes, corresponding to modify the maximum value

Sub action: Delete a number, if the number is deleted after the other number reaches the same level, only change the maximum value

#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]&GT;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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Light OJ 1339 Strongest Community (sub-block violence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.