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

链接:http://codeforces.com/contest/689/problem/D

题意:给定两个长度为n的数组a,b。求有多少个区间[l,r]有max(a[l]~a[r])==min(b[l]~b[r])。

分析:对于每一个a[i]找到它为最大的值的区间最大边界l[i]和r[i],同样处理b数组。然后我们枚举以每一个a[i]为区间max来和b数组计数,注意下去重就行了。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=200010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
struct node {
    int x,w;
    node() {}
    node(int x,int w):x(x),w(w) {}
    bool operator < (const node a) const {
        if (x!=a.x) return x<a.x;
        return w<a.w;
    }
}f[N];
int a[2][N],l[2][N],r[2][N],d[2*N],RI[N];
int main()
{
    int i,j,g,n,k=0,L,R,mid;
    node kk;
    ll ans=0;
    scanf("%d", &n);
    for (i=0;i<2;i++)
        for (j=1;j<=n;j++) {
            scanf("%d", &a[i][j]);
            d[++k]=a[i][j];
        }
    sort(d+1,d+k+1);
    g=unique(d+1,d+k+1)-(d+1);
    for (i=0;i<2;i++)
        for (j=1;j<=n;j++) a[i][j]=lower_bound(d+1,d+g+1,a[i][j])-d;
    a[0][0]=a[0][n+1]=INF;
    a[1][0]=a[1][n+1]=0;
    k=1;d[k]=0;
    for (i=1;i<=n;i++) {
        while (a[0][i]>=a[0][d[k]]) k--;
        l[0][i]=d[k];d[++k]=i;
    }
    k=1;d[k]=n+1;
    for (i=n;i>=1;i--) {
        while (a[0][i]>a[0][d[k]]) k--;
        r[0][i]=d[k];d[++k]=i;
    }
    k=1;d[k]=0;
    for (i=1;i<=n;i++) {
        while (a[1][i]<=a[1][d[k]]) k--;
        l[1][i]=d[k];d[++k]=i;
    }
    k=1;d[k]=n+1;
    for (i=n;i>=1;i--) {
        while (a[1][i]<a[1][d[k]]) k--;
        r[1][i]=d[k];d[++k]=i;
    }
    k=1;d[k]=n+1;
    for (i=n;i>=1;i--) {
        while (a[1][i]<=a[1][d[k]]) k--;
        RI[i]=d[k];d[++k]=i;
    }
    for (i=1;i<=n;i++) f[i]=node(a[1][i],i);
    sort(f+1,f+n+1);
    for (i=1;i<=n;i++) {
        kk=node(a[0][i],i);
        L=0;R=n;mid=(L+R)>>1;
        while (L+1<R)
        if (f[mid]<kk) { L=mid;mid=(L+R)>>1; }
        else { R=mid;mid=(L+R)>>1; }
        if (f[L].x==kk.x&&r[1][f[L].w]>kk.w&&l[1][f[L].w]<kk.w&&l[0][i]<f[L].w) {
            if (f[L].w<=kk.w) ans+=(ll)(f[L].w-max(l[0][i],l[1][f[L].w]))*(min(r[0][i],r[1][f[L].w])-kk.w);
            else ans+=(ll)(kk.w-max(l[0][i],l[1][f[L].w]))*(min(r[0][i],RI[f[L].w])-f[L].w);
        }
        if (f[R].x==kk.x&&l[1][f[R].w]<kk.w&&r[1][f[R].w]>kk.w&&r[0][i]>f[R].w) {
            if (f[R].w>=kk.w) ans+=(ll)(kk.w-max(l[0][i],l[1][f[R].w]))*(min(r[0][i],RI[f[R].w])-f[R].w);
            else ans+=(ll)(f[R].w-max(l[0][i],l[1][f[R].w]))*(min(r[0][i],r[1][f[R].w])-kk.w);
        }
    }
    printf("%I64d\n", ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值