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. 3.3、Ansible命令参数详解

    0.ansible 命令参数详解: [root@localhost ~]# ansible Usage: ansible <host-pattern> [options] Options: ...

  2. JS三角形

    1.直角三角形 <script> ; ; i++) { //外层循环代表的是要循环的行数 ; j < i; j++) { //内层循环代表的是要打印的列数 document.writ ...

  3. css 超出宽度出现省略号

    display: block; overflow: hidden; width: 260px; white-space: nowrap; text-overflow: ellipsis;

  4. [Tailwind] Control What Variations are Generated for Each Utility Class Module in Tailwind

    In this lesson, we learn how to control what utility classes are generated for each utility class mo ...

  5. Android Touch事件分发过程

    虽然网络上已经有非常多关于这个话题的优秀文章了,但还是写了这篇文章,主要还是为了加强自己的记忆吧,自己过一遍总比看别人的分析要深刻得多.那就走起吧. 简单演示样例 先看一个演示样例 : 布局文件 : ...

  6. c++中字符输入函数getline、cin.getline区分

    1.cin>>s; s能够是:string  s.char s[]; 这个是ostream中的函数.遇到' '(空格) , '\n'(换行),就会自己主动结束,因此假设用cin读取字符串, ...

  7. unity3d Pathfinding插件使用

    Overview The central script of the A* Pathfinding Project is the script 'astarpath.cs', it acts as a ...

  8. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...

  9. POJ 2796 / UVA 1619 Feel Good 扫描法

    Feel Good   Description Bill is developing a new mathematical theory for human emotions. His recent ...

  10. ROS-VIM

    在终端输入:vimtutor,进入学习教程. 常用快捷键: a 编辑模式; 编辑状态下 回车 换行; Ctrl+n 补全命令; x剪切; p复制; u撤销; :q!不保存退出; :wq保存并退出; 以 ...