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\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的更多相关文章
- CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...
- CF 314C Sereja and Subsequences(树状数组)
题目链接:http://codeforces.com/problemset/problem/314/C 题意:给定一个数列a.(1)写出a的不同的所有非下降子列:(2)定义某个子列的f值为数列中各个数 ...
- CodeForces 689D Friends and Subsequences
枚举,二分,$RMQ$. 对于一个序列来说,如果固定区间左端点,随着右端点的增大,最大值肯定是非递减的,最小值肯定是非递增的. 因此,根据这种单调性,我们可以枚举区间左端点$L$,二分找到第一个位置$ ...
- 689D Friends and Subsequences RMQ+二分
题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...
- Solution -「CF 1132G」Greedy Subsequences
\(\mathcal{Description}\) Link. 定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列: \(\{b\}\) ...
- Magolor的数据结构作业
\(CodeForces 706E ~Working routine\) 给出一个矩阵,每次操作交换两个子矩阵,求最后状态. 使用链表存储,每次交换后,影响到的之后矩阵边缘的指针,暴力修改. \(~~ ...
- codeforces 689D D. Friends and Subsequences(RMQ+二分)
题目链接: D. Friends and Subsequences time limit per test 2 seconds memory limit per test 512 megabytes ...
- CF 843 A. Sorting by Subsequences
A. Sorting by Subsequences You are given a sequence a1, a2, ..., an consisting of different integers ...
- cf 843 A Sorting by Subsequences [建图]
题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边 ...
随机推荐
- 3.2、使用Flask-Bootstrap集成Twitter Bootstrap
Bootstrap(http://getbootstrap.com/)是 Twitter 开发的一个开源框架,它提供的用户界面组件可用于创建整洁且具有吸引力的网页,而且这些网页还能兼容所有现代 Web ...
- Git clone时出现Please make sure you have the correct access rights and the repository exists.问题已解决。
看了好多资料终于搞定了git 中clone命令报错这个问题,废话不多说直接上步骤希望对大家有帮助. 1 删除.ssh文件夹(直接搜索该文件夹)下的known_hosts(手动删除即可,不需要git ...
- 【hdu 6396】Swordsman
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 开k个优先队列.每个队列都满足从小到大那种.. 首先将所有的怪物加入到第一个队列中. 然后对于v[i]>=pq[i].top( ...
- PHP学习总结(8)——PHP入门篇之WAMPServer集成环境安装和配置
WampServer就是Windows Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件.WampServer是一款由法国人开发的Apac ...
- spring data JPA使用quartz定时器的具体实现
第一步.在pom.xml中的配置 <!--quartz--> <dependency> <groupId>org.quartz-scheduler</grou ...
- 洛谷——P1802 5倍经验日
https://www.luogu.org/problem/show?pid=1802#sub 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却无奈的看着那一些比他等级高的好 ...
- HDU 1575 EASY
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- [Web Worker] Introduce to Web Worker
What is web worker for? OK, read it docs to get full details idea. Or just a quick intro to web work ...
- 【iOS开发-68】APP下载案例:利用tableView自带的cell布局+缓存池cell复用时注意button状态的检查
(1)效果 (2)源码与资源下载 http://pan.baidu.com/s/1pJLo2PP (3)总结 --核心是利用UITableView里面自带的cell来制作样式同样的cell. 与之对应 ...
- angularjs1--动画
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...