题目大意:给你两个长度为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的更多相关文章

  1. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

  2. Codeforces Round #361 (Div. 2) C.NP-Hard Problem

    题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  7. 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 ...

  8. Codeforces Round #361 (Div. 2) D

    D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...

  9. Codeforces Round #361 (Div. 2) C

    C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...

随机推荐

  1. 多线程中join方法的含义

    1.作用:调用这个方法的时候,主进程会在这里停住,等待该线程进行完毕再继续往下执行. 如:不使用join的情况: <?php class Join extends Thread { public ...

  2. isnotblank与isnotempty的区别

  3. php 三元运算符简洁用法

    <?php header('Content-type:text;charset=utf8'); $a = 'aaaa'; $b = $a ?:'; $c = $a ? $a : '; //这个和 ...

  4. Python基础之面向对象(进阶篇)

    面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...

  5. Spring 源码学习(2) —— FactoryBean 的使用

    一般情况下, Spring是通过反射机制利用bean的class属性指定实现类来完成实例化bean的.在某些特定的情况下, 想做一些定制,Spring为此提供了一个org.springframewor ...

  6. Knockout双向绑定

    knockout双工绑定基于 observe 模式,性能高.核心就是observable对象的定义.这个函数最后返回了一个也叫做 observable 的函数,也就是用户定义值的读写器(accesso ...

  7. TypeScript在react项目中的实践

    前段时间有写过一个TypeScript在node项目中的实践. 在里边有解释了为什么要使用TS,以及在Node中的一个项目结构是怎样的. 但是那仅仅是一个纯接口项目,碰巧赶上近期的另一个项目重构也由我 ...

  8. GCD HDU - 1695 莫比乌斯反演入门

    题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/ ...

  9. linux 进程优先级 之设置实时进程 (另一种方式是设置nice值)【转】

    转自:https://www.cnblogs.com/jkred369/p/6731353.html Linux内核的三种调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIF ...

  10. MAC和PHY的区别 (转自http://www.cnblogs.com/feitian629/archive/2013/01/25/2876857.html)

    一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数据传送与接收所需要的电与光信号.线路状态.时钟基准.数据编码和电路等,并向数据链路层设备提供标准接口.数据链路层 ...