题目大意:给你两个长度为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. jsp 基本原理

    jsp 的本质是 servlet,当用户请求 servlet 的时候,servlet 利用输出流动态输出 HTML 内容. 由于包括了大量的 HTML 标签.大量的静态文本等,导致 servlet 开 ...

  2. js浏览器调试方法

    chrome浏览器可在需要断点的地方写一个关键字 "debugger",这样在 js 运行到这里的时候会停止继续运行,并可以查看当前状态

  3. Stat2—主成分分析(Principal components analysis)

    最近在猛撸<R in nutshell>这本课,统计部分涉及的第一个分析数据的方法便是PCA!因此,今天打算好好梳理一下,涉及主城分析法的理论以及R实现!come on…gogogo… 首 ...

  4. 【Codeforces752D】Santa Claus and a Palindrome [STL]

    Santa Claus and a Palindrome Time Limit: 20 Sec  Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个a ...

  5. 【BZOJ】2301: [HAOI2011]Problem b

    [题意]于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数.n,a,b,c,d,k<=50000. ...

  6. 牛客网习题剑指offer之数值的整数次方

    分析: 要考虑到exponent为0和负数的情况. 如果base是0并且exponent是负数的时候呢?那就发生除0的情况了. AC代码: public class Solution { public ...

  7. Masquerade strikes back Gym - 101911D(补题) 数学

    https://vjudge.net/problem/Gym-101911D 具体思路: 对于每一个数,假设当前的数是10 分解 4次,首先 1 10 这是一对,然后下一次就记录 10 1,这样的话直 ...

  8. perl6正则 2: 字母,数字,空格,下划线, 字符集

    数字, 字母, 下划线 在perl6中, 如果是 数字, 字母, 下划线, 在正则里可以正接写上. > so / True > so 'perl6_' ~~ /_/ True > 非 ...

  9. discuz 积分按日重新计算,(摒弃以前24小时计算)

    修改\source\module\forum\forum_misc.php将 foreach(C::t('forum_ratelog')->fetch_all_sum_score($_G['ui ...

  10. C++之 extern C的作用详解

    extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...