Codeforces Round #361 (Div. 2) D - Friends and Subsequences
题目大意:给你两个长度为n的数组a, b,问你有多少个问你有多少个区间满足
a中最大值等于b中最小值。
思路:我本来的想法是用单调栈求出每个点的管辖区间,然后问题就变成了巨麻烦的线段覆盖问题,就爆炸写了
一晚上假算法。正解就是枚举一个端点,然后二分找右端点的区间,因为满足一个很神奇的单调性,然后st表维护
一下区间最值就好了。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 2e5 + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ; int n, a[N], b[N], bin[], Log[N], mx[][N], mn[][N]; void calRmq() {
bin[] = ; Log[] = -;
for(int i = ; i < ; i++) bin[i] = bin[i - ] * ;
for(int i = ; i < N; i++) Log[i] = Log[i / ] + ; for(int i = ; i <= n; i++) mx[][i] = a[i];
for(int i = ; i <= n; i++) mn[][i] = b[i]; for(int i = ; i <= Log[n]; i++) {
for(int j = ; j <= n; j++) {
if(j + bin[i] - <= n) {
mn[i][j] = min(mn[i - ][j], mn[i - ][j + bin[i - ]]);
mx[i][j] = max(mx[i - ][j], mx[i - ][j + bin[i - ]]);
}
}
}
} int getVal(int x, int y, int op) {
int t = Log[y - x + ];
if(op) return max(mx[t][x], mx[t][y - bin[t] + ]);
return min(mn[t][x], mn[t][y - bin[t] + ]);
} int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
for(int i = ; i <= n; i++) {
scanf("%d", &b[i]);
} calRmq(); LL ans = ;
for(int i = ; i <= n; i++) {
int l = i, r = n, mid, ret1 = -, ret2 = -;
while(l <= r) {
mid = l + r >> ;
int valMin = getVal(i, mid, );
int valMx = getVal(i, mid, );
if(valMin > valMx) l = mid + ;
else if(valMin < valMx) r = mid - ;
else ret1 = mid, r = mid - ;
} l = i, r = n;
while(l <= r) {
mid = l + r >> ;
int valMin = getVal(i, mid, );
int valMx = getVal(i, mid, );
if(valMin > valMx) l = mid + ;
else if(valMin < valMx) r = mid - ;
else ret2 = mid, l = mid + ;
} if(ret1 != -) {
ans += ret2 - ret1 + ;
}
} printf("%lld\n", ans);
return ;
} /*
*/
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 1e5;
const int M = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int mm[N];
struct ST
{
int dp[N][],ty;
void build(int n,int b[],int _ty)
{
ty=_ty;
for(int i=;i<=n;i++)
dp[i][]=ty*b[i];
for(int j=;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int query(int x,int y)
{
int k=mm[y-x+];
return ty*max(dp[x][k],dp[y-(<<k)+][k]);
}
}rmq; int a[N];
int main() { for(int i=-(mm[]=-);i<N;i++)
mm[i]=mm[i-]+((i&(i-))==); int n; scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
rmq.build(n, a, ); while() {
int x, y; scanf("%d%d", &x, &y);
cout << rmq.query(x, y) << endl;
}
return ;
}
还有一个很神奇的st表
Codeforces Round #361 (Div. 2) D - Friends and Subsequences的更多相关文章
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs
B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- Codeforces Round #361 (Div. 2) D
D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...
- Codeforces Round #361 (Div. 2) C
C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...
随机推荐
- 进程间共享数据Manager
一.前言 进程间的通信Queue()和Pipe(),可以实现进程间的数据传递.但是要使python进程间共享数据,我们就要使用multiprocessing.Manager. Manager()返回的 ...
- proxy vue3.0
<html> <head> <meta charset="UTF-8" /> <meta name="viewport" ...
- jdbc:oracle:thin:@localhost:1521:orcl和jdbc:oracle:thin:@localhost:1521/orcl的区别
Oracle Thin JDBC Driver 驱动程序包名:ojdbc14.jar.ojdbc6.jar 驱动程序类名: oracle.jdbc.driver.OracleDriver JDBC ...
- HDU3068 最长回文 MANACHER+回文串
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- 重构改善既有代码设计--重构手法06:Split Temporary Variable (分解临时变量)
你的程序有某个临时变量被赋值超过一次,它既不是循环变量,也不被用于收集计算结果.针对每次赋值,创造一个独立.对应的临时变量 double temp = 2 * (_height + _width); ...
- GlusterFS + lagstash + elasticsearch + kibana 3 + redis日志收集存储系统部署 01
因公司数据安全和分析的需要,故调研了一下 GlusterFS + lagstash + elasticsearch + kibana 3 + redis 整合在一起的日志管理应用: 安装,配置过程,使 ...
- asp.net mvc 站点优化
基于上篇:IIS网站日志分析 现象 服务端:IIS 日志, #Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-u ...
- ASP.NET网站伪静态下使用中文URL
首先解释一下,什么是中文URL呢?它并不是我们常见的把汉字编码为 %CF%EC 这种形式,而是在URL中直接使用汉字 这种形式目前还不是很多见.因为不同的浏览器处理起来可能会有所不同,不过据我测试,I ...
- 【leetcode 简单】第三十七题 相交链表
编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...
- Linux命令之uptime
这是什么 uptime用来查看系统已经启动了多长时间了. 它显示的信息和w命令的头(第一行)是一样一样的. 举个栗子 举一个实际的应用场景: 比如发现服务器上的某些没有加入开机启动的服务挂了一片,这个 ...