CF 689D - Friends and Subsequences
Posted fridayfang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 689D - Friends and Subsequences相关的知识,希望对你有一定的参考价值。
689D - Friends and Subsequences
题意:
- 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb(l,r)
- 切题的感觉很爽唉
- 同样而二分查找,找最小和最大下标满足条件
cf中%I64d, 一般是%lld
代码:
#include<bits/stdc++.h>
#define ll long long
const int maxn=200010;
int sta[maxn][18];
int stb[maxn][18];
int a[maxn];
int b[maxn];
ll res;
int n;
void build(){
int maxl=floor(log2(n));
for(int i=1;i<=n;i++){
sta[i][0]=a[i];
stb[i][0]=b[i];
}
int mul=1;
for(int j=1;j<=maxl;j++){
for(int i=1;i<=n&&(i+mul)<=n;i++){
sta[i][j]=std::max(sta[i][j-1],sta[i+mul][j-1]);
stb[i][j]=std::min(stb[i][j-1],stb[i+mul][j-1]);
}
mul=mul*2;
}
}
int check(int x,int y){
int len=floor(log2(y-x+1));
int maxa=std::max(sta[x][len],sta[y-(1<<len)+1][len]);
int minb=std::min(stb[x][len],stb[y-(1<<len)+1][len]);
//printf("db x:%d y:%d maxa: %d minb %d
",x,y,maxa,minb);
return maxa-minb;
}
// find min() key=0
//find max() key=0
int b1(int begin,int end){
int l=begin,r=end,m;
while(l<r){
m=l+((r-l)>>1);
if(check(begin,m)<0) l=m+1;
else r=m;
}
if(check(begin,l)==0) return l;
return -1;
}
int b2(int begin,int end){
int l=begin,r=end,m;
while(l<r){
m=l+((r-l+1)>>1);
if(check(begin,m)<=0) l=m;
else r=m-1;
}
if(check(begin,l)==0) return l;
return -1;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
build();
res=0;
for(int i=1;i<=n;i++){
if(b1(i,n)==-1) continue;
int r1=b1(i,n);
int r2=b2(i,n);
res+=(r2-r1+1);
}
printf("%I64d
",res);
}
以上是关于CF 689D - Friends and Subsequences的主要内容,如果未能解决你的问题,请参考以下文章
CF1080C Masha and two friends (矩阵的重叠面积)
CF with friends and user's influence considered on NYC data(updated Aug,11st)