Codeforces Round #361 (Div. 2) D Friends and Subsequences
来源:互联网 发布:女装淘宝店铺简介 编辑:程序博客网 时间:2024/05/22 08:17
原题网址:http://codeforces.com/contest/689/problem/D
对于每一个确定的l,随着r的增大,max ai 单调增,min bi 单调减,对于每一个l可以二分,对于区间最值可以RMQ,总复杂度nlogn。(一开始区间最值用线段树平添log)
var pr1,pr2:array[0..200050,0..18] of longint; n,i,j,l,r,m:longint; ans,t1,t2:int64;function max(a,b:longint):longint; begin if a>b then exit(a) else exit(b); end;function min(a,b:longint):longint; begin if a<b then exit(a) else exit(b); end;function f(x:longint):longint; var s:longint; begin s:=0; while x>1 do begin x:=x>>1; inc(s); end; exit(s); end;function qmax(l,r:longint):longint; var t:longint; begin t:=f(r-l+1); exit(max(pr1[l][t],pr1[r-1<<t+1][t])); end;function qmin(l,r:longint):longint; var t:longint; begin t:=f(r-l+1); exit(min(pr2[l][t],pr2[r-1<<t+1][t])); end;begin read(n); for i:=1 to n do read(pr1[i][0]); for i:=1 to n do read(pr2[i][0]); for j:=1 to 18 do for i:=1 to n do begin if i+1<<j-1>n then break; pr1[i][j]:=max(pr1[i][j-1],pr1[i+1<<(j-1)][j-1]); pr2[i][j]:=min(pr2[i][j-1],pr2[i+1<<(j-1)][j-1]); end; ans:=0; for i:=1 to n do begin l:=i;r:=n; while l<r do begin m:=(l+r+1)>>1; if qmax(i,m)>qmin(i,m) then r:=m-1 else l:=m; end; t1:=l; l:=i;r:=n; while l<r do begin m:=(l+r)>>1; if qmax(i,m)<qmin(i,m) then l:=m+1 else r:=m; end; t2:=l; if (t1>=t2)and(qmax(i,t1)=qmin(i,t1))and(qmax(i,t2)=qmin(i,t2)) then inc(ans,t1-t2+1); end; writeln(ans);end.
0 0
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences
- Codeforces Round #361 (Div. 2)D. Friends and Subsequences
- Codeforces Round #361 (Div. 2) D Friends and Subsequences
- Codeforces Round #361 (Div. 2) D Friends and Subsequences
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
- Cf Round #361 (Div. 2) 689D. Friends and Subsequences
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences (二分+RMQ)
- codeforces Round #361 D. Friends and Subsequences (ST表,二分)
- Codeforces #361D. Friends and Subsequences 数学 尺取法
- CodeForces 689D-Friends and Subsequences
- CodeForces 689D-Friends and Subsequences
- Codeforces 689D Friends and Subsequences
- 【22.48%】【codeforces 689D】Friends and Subsequences
- Codeforces Round #275 (Div. 2) B Friends and Presents
- Codeforces Round #275 (Div. 2) --B Friends and Presents
- Codeforces Round #275 (Div. 2) --B Friends and Presents
- Codeforces Round #275 (Div. 2) B. Friends and Presents
- Codeforces Round #394 (Div. 2)Dasha and friends
- 外键约束
- 20160707
- Leetcode Delete Node in a Linked List
- MySql基本命令
- hdu5361(最短路+并查集)
- Codeforces Round #361 (Div. 2) D Friends and Subsequences
- Spring+Log4j+ActiveMQ实现远程记录日志
- 深度解决 SecurityException: User has not given permission to device UsbDevice
- linux挂载windows文件夹
- Leetcode Majority Element
- 常用查看Linux系统信息命令
- session入redis
- 面向对象原则
- 我的酷派大神1S 9976t的刷机过程