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 ...
随机推荐
- 009.C++ const使用
1.引例 class complex { public: complex(, ) : re (r), im (i) {} complex& operator += (const complex ...
- Exponential Distribution指数分布
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- Java中的Class.forName
在做JAVA EE开发的过程中,更多的是使用框架来提高开发效率.越来越发现,之前很基础的一些东西,都忘记的差不多了.从今天开始慢慢的复习一下基础.今天在看JDBC的时候,就有一个有趣的地方,之前学的时 ...
- Sql2008 全文索引创建
在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查 ...
- 使用tqdm组件构造程序进度条
使用tqdm组件构造程序进度条 觉得有用的话,欢迎一起讨论相互学习~Follow Me 主要代码 import tqdm # 引用tqdm组件 TRAIN_STEPS = N for i in tqd ...
- AbstractTransactionalJUnit4SpringContextTests事务回滚
在单元测试中继承AbstractTransactionalJUnit4SpringContextTests类的 时候,会默认事务回滚. 需要照常执行的话,在执行的测试方法上添加@Rollback(fa ...
- How to Evaluate Machine Learning Models, Part 4: Hyperparameter Tuning
How to Evaluate Machine Learning Models, Part 4: Hyperparameter Tuning In the realm of machine learn ...
- 详细说说如何生成验证码—ASP.NET细枝末节(4)
前言 今天小编详细的说一下,ASP.NET网站开发过程中生成验证码的全部问题. 本文的目标,是让读者了解,生成验证码涉及的全部基础知识问题. 当然这里说的是比较简单的验证码. 真正符合要求的验证码,涉 ...
- datagrid导出数据
//导出excel public function touzi_doExport() { $search=$_POST['search']; //接受前端传过来的数据 $this->succes ...
- 【洛谷P2015】二叉苹果树
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...