• poj3368 Frequent values(线段树)


    Description

    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.

    Input

    The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the 
    query.

    The last test case is followed by a line containing a single 0.

    Output

    For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

    Sample Input

    10 3
    -1 -1 1 1 1 1 3 10 10 10
    2 3
    1 10
    5 10
    0

    Sample Output

    1
    4
    3

    Source

    Ulm Local 2007
     
     
    无脑的线段树。写着玩玩。
     1 program rrr(input,output);
     2 type
     3   treetype=record
     4      l,r,m,lm,rm:longint;
     5   end;
     6 var
     7   a:array[0..400040]of treetype;
     8   c:array[0..100010]of longint;
     9   n,i,q,x,y:longint;
    10 function max(a,b:longint):longint;
    11 begin
    12    if a>b then exit(a) else exit(b);
    13 end;
    14 function min(a,b:longint):longint;
    15 begin
    16    if a<b then exit(a) else exit(b);
    17 end;
    18 procedure build(k,l,r:longint);
    19 var
    20   mid,i:longint;
    21 begin
    22    a[k].l:=l;a[k].r:=r;
    23    if l=r then begin a[k].m:=1;a[k].lm:=1;a[k].rm:=1;exit; end;
    24    mid:=(l+r)>>1;i:=k+k;
    25    build(i,l,mid);build(i+1,mid+1,r);
    26    a[k].m:=max(a[i].m,a[i+1].m);if c[mid]=c[mid+1] then a[k].m:=max(a[k].m,a[i].rm+a[i+1].lm);
    27    if c[l]=c[mid+1] then a[k].lm:=a[i].lm+a[i+1].lm else a[k].lm:=a[i].lm;
    28    if c[mid]=c[r] then a[k].rm:=a[i].rm+a[i+1].rm else a[k].rm:=a[i+1].rm;
    29 end;
    30 function query(k:longint):longint;
    31 var
    32   mid,i,ans:longint;
    33 begin
    34     if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].m);
    35     mid:=(a[k].l+a[k].r)>>1;i:=k+k;
    36     ans:=0;
    37     if x<=mid then ans:=query(i);
    38     if mid<y then ans:=max(ans,query(i+1));
    39     if (x<=mid) and (mid<y) and (c[mid]=c[mid+1]) then ans:=max(ans,min(mid-x+1,a[i].rm)+min(y-mid,a[i+1].lm));
    40     exit(ans);
    41 end;
    42 begin
    43    assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
    44    while true do
    45       begin
    46          read(n);if n=0 then break;
    47          readln(q);
    48          for i:=1 to n do read(c[i]);
    49          build(1,1,n);
    50          for i:=1 to q do begin readln(x,y);writeln(query(1)); end;
    51       end;
    52    close(input);close(output);
    53 end.
  • 相关阅读:
    “ODBC驱动程序不支持动态记录集”错误的解决
    Pro *C/C++学习笔记(一)
    探讨全局变量的析构顺序
    指针和数组关系初探
    (转)Visual C++开发工具与调试技巧整理
    对利用Session纪录datagrid模板列中CheckBox的状态的一点改进
    大学老师列传
    重读保尔的意义
    Rich Edit控件的使用
    C++程序员常用工具集
  • 原文地址:https://www.cnblogs.com/Currier/p/6701581.html
Copyright © 2020-2023  润新知