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\n",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\n",res);
}

CF 689D - Friends and Subsequences的更多相关文章

  1. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

  2. CF 314C Sereja and Subsequences(树状数组)

    题目链接:http://codeforces.com/problemset/problem/314/C 题意:给定一个数列a.(1)写出a的不同的所有非下降子列:(2)定义某个子列的f值为数列中各个数 ...

  3. CodeForces 689D Friends and Subsequences

    枚举,二分,$RMQ$. 对于一个序列来说,如果固定区间左端点,随着右端点的增大,最大值肯定是非递减的,最小值肯定是非递增的. 因此,根据这种单调性,我们可以枚举区间左端点$L$,二分找到第一个位置$ ...

  4. 689D Friends and Subsequences RMQ+二分

    题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...

  5. Solution -「CF 1132G」Greedy Subsequences

    \(\mathcal{Description}\)   Link.   定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列: \(\{b\}\) ...

  6. Magolor的数据结构作业

    \(CodeForces 706E ~Working routine\) 给出一个矩阵,每次操作交换两个子矩阵,求最后状态. 使用链表存储,每次交换后,影响到的之后矩阵边缘的指针,暴力修改. \(~~ ...

  7. codeforces 689D D. Friends and Subsequences(RMQ+二分)

    题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...

  8. CF 843 A. Sorting by Subsequences

    A. Sorting by Subsequences You are given a sequence a1, a2, ..., an consisting of different integers ...

  9. cf 843 A Sorting by Subsequences [建图]

    题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边 ...

随机推荐

  1. 网络是通的 yum用不了

    Loaded plugins: fastestmirror, langpacks One of the configured repositories failed (Unknown),and yum ...

  2. POJ 2395 Out of Hay( 最小生成树 )

    链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* & ...

  3. IntelliJ IDEA 2017.1.6 x64 的破解

    方式一 现在用这个 http://idea.imsxm.com/好使 步骤如下,点击help按钮,选择Register 点击license server   修改下面的服务器激活地址 方式二 由于Je ...

  4. Python 绘制2016世界GDP地图

    2016世界GDP地图 从https://datahub.io/core/gdp#data下载得到json文件. # country_code.py 获取国家二字代码 # 从pygal.maps.wo ...

  5. FFMPEG 音频转换命令

    音频转换: .转换amr到mp3: ffmpeg -i shenhuxi.amr amr2mp3.mp3 .转换amr到wav: ffmpeg -acodec libamr_nb -i shenhux ...

  6. linux_ubuntu 连接xftp

    一.修改静态ip 打开网络配置文件 :vim /etc/network/interfaces 1.添加以下配置: 注意:ubuntu 网卡名为 ens33 .多数liunx系统的网卡名为eth0. 可 ...

  7. HDU5130 Signal Interference

    /* HDU5130 Signal Interference http://acm.hdu.edu.cn/showproblem.php?pid=5130 计算几何 圆与多边形面积交 * */ #in ...

  8. 日志log配置理解

    实际生产中,每天都有大量的日志生成,单个文件(FileAppender)已经不能满足要求,RollingFileAppender继承了FileAppender,并提供了更多的功能: 每天生成一个日志文 ...

  9. [using_microsoft_infopath_2010]Chapter 14高级话题

    本章提要: 1.剖析表单逻辑 2.从多个表单中合并数据 3.重新连接XML表单到XSN模板文件 4.在repeating table中设置默认值 5.离线填写表单的权衡

  10. [Tailwind] Style Elements on hover and focus with Tailwind’s State Variants

    In this lesson, we learn how to target specific states of elements and apply styles only when those ...