首页 > 其他分享 >POJ 3264(STRMQ)

POJ 3264(STRMQ)

时间:2022-10-25 12:33:40浏览次数:49  
标签:do STRMQ min ln begin longint POJ 3264 shl


for j:=1 to ln(n)/ln(2)

    for i:=1 to n-(1 shl j)+1 do

       f[i,j]:=min(f[i,j-1],f[i+(1 shl (j-1),j-1];


f[l,r]:=min(f[l,j],f[r-(1 shl j)+1,j]; j=ln(r-l+1)/ln(2);


ln(n)/ln(2)=log(2,n);



Program lineup;
const
maxn=50000;
maxq=200000;
maxh=1000000;
var
n,q,i,j,l,r,k:longint;
a:array[1..maxh] of longint;
f,h:array[1..maxh,0..16] of longint;//f mintall h hightall
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;

begin
read(n,q);
for i:=1 to n do
begin
read(a[i]);
f[i,0]:=a[i];
h[i,0]:=a[i];
end;
for j:=1 to trunc(ln(n)/ln(2)) do //f[i,j]:=min(f[i,j-1],f[i+2^(j-1),j-1] i<=n-2^j
for i:=1 to n-(1 shl j)+1 do
begin
f[i,j]:=min(f[i,j-1],f[i+1 shl (j-1),j-1]);
h[i,j]:=max(h[i,j-1],h[i+1 shl (j-1),j-1]);
end;

for i:=1 to q do
begin
read(l,r);
j:=(r-l+1);
j:=trunc(ln(j)/ln(2));
writeln(max(h[l,j],h[r-1 shl j +1,j])-min(f[l,j],f[r-1 shl j +1,j]));
end;
end.



标签:do,STRMQ,min,ln,begin,longint,POJ,3264,shl
From: https://blog.51cto.com/u_15724837/5794488

相关文章

  • POJ 3748(C++的16进制读法 %x)
    P党写几小时的程序C++才几行……首先P的位运算有上限2^30此时即便是int64也会因为补码坑死人的到1shl31时 int64是负数故这个时候不能shr为多出好多位造成以......
  • POJ 2185(最大平铺矩阵)
    DefaultMilkingGridDescription(1<=R<=10,000) *C (1<=C<=75) 的矩阵,求它的最大平铺矩阵,不够的地方可部分平铺,但不可重叠。Input......
  • POJ 3661(贝茜晨练)
    经典Dp,果断记忆化……#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<functional>usingnamespacestd;#defineMAXN10000+10#defineM......
  • POJ 3575(计算几何与二分-无尽的小数处理)
    这题写了将近半个月……总是在D各种Bug总的说来-这题最难应该是在精度处理上11001这组数据过了就说明精度处理差不多了……Programkingdom;constmaxn=100;maxm=10......
  • POJ 3049(输出字母)
    果断搜ProgramP3049;varn,i,j,m:longint;a:array[1..26]ofchar;b:array['a'..'z']ofboolean;c:char;procedureswap(vara,b:char);vart:char;begin......
  • POJ 3289(高精度乘法)
    高精度乘法ProgramP3289;constmaxn=40000;F=10;typearr=recordd:array[1..maxn]oflongint;len,doc:longint;end;varr,m:arr;y:long......
  • POJ 1222(Gauss消元xor版)
    EXTENDEDLIGHTSOUTDescriptionLightsOut就是下图的游戏,给你一个5*6的矩阵. 你的目标是把灯全关上. 0表示关,1表示开.Input第一行为数据......
  • POJ 2398(二分点集)
    DefaultToyStorageDescription在长方形(x1,y1)(x2,y2)中有n块板(保证与上下边相交),和m个点。现给出板和点的位置,求拥有相同点数的区域数、  Inpu......
  • POJ 2318(点集二分)
    DefaultTOYSDescription在长方形(x1,y1)(x2,y2)中有n块板(保证与上下边相交),和m个点。现给出板和点的位置,求各区域点数、  Input......
  • POJ 1825/2279(Young/Mr. Young's Picture Permutations-杨氏矩阵和钩子公式)
    给出一个n行的矩阵,每一行有a[i]个数,总共有sum个数,要求每一个位置的数必须比上面的数和左面的数大,求总方案数.杨氏矩阵又叫杨氏图表,它是这样一个矩阵,满足条件:(1)如果格子......