CodeForces 689D Friends and Subsequences (RMQ+二分)

本文介绍了一种解决特定RMQ(Range Maximum Query)与RMI(Range Minimum Query)问题的方法,通过对两个序列进行预处理,实现O(nlogn)的时间复杂度,并在O(1)时间内回答每个查询。通过二分查找确定区间左右边界,最终计算出满足条件的区间数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面:

 

D. Friends and Subsequences

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows?

Every one of them has an integer sequences a andb of length n. Being given a query of the form of pair of integers(l, r), Mike can instantly tell the value of while !Mike can instantly tell the value of.

Now suppose a robot (you!) asks them all possible different queries of pairs of integers(l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactlyn(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs is satisfied.

How many occasions will the robot count?

Input

The first line contains only integer n (1 ≤ n ≤ 200 000).

The second line contains n integer numbersa1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.

The third line contains n integer numbersb1, b2, ..., bn ( - 109 ≤ bi ≤ 109) — the sequence b.

Output

Print the only integer number — the number of occasions the robot will count, thus for how many pairs is satisfied.

Examples

Input

6
1 2 3 2 1 4
6 7 1 2 3 2

Output

2

Input

3
3 3 3
1 1 1

Output

0

Note

The occasions in the first sample case are:

1.l = 4,r = 4 sincemax{2} = min{2}.

2.l = 4,r = 5 sincemax{2, 1} = min{2, 3}.

There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.

 

题意:

    给定两个等长区间,求相同l,r情况下,有多少个区间,第一个序列的最大值和第二个序列的最小值相同。

 

解题:

    RMQ,O(nlog(n))的复杂度,随后可以做到O(1)询问。但若枚举区间l,r,复杂度仍为O(n^2)。这时可以利用区间特性进行二分,固定区间左端点(注意区间左端点和二分左端点不一样),若二分右端点所在位置形成的最小值不等于最大值,若最小值大于最大值,说明仍可以右移,还有希望相等。若最小值小于最大值,则说明区间过长,左移区间左端点。若相等,则根据当前是求右边界最左端还是最右端进行相应移动。

    此题比较特殊的是,枚举左区间端点,但左区间端点和左二分端点是不一样的,二分的是区间右端点。每次通过两次二分,求出每一区间左端点对应的区间右端点的两个位置极值,作差,求和,求得结果。

 

/*
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>

using namespace std;

int n;
int a[200005];
int b[200005];

int ma[200005][25];
int mi[200005][25];

void cal()
{
    for(int i=0;i<n;i++)
    {
        ma[i][0]=a[i];
        mi[i][0]=b[i];
    }


    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        {
            ma[i][j]=max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
            mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
        }
    }
}

int gma(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return max(ma[l][k],ma[r-(1<<k)+1][k]);
}

int gmi(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return min(mi[l][k],mi[r-(1<<k)+1][k]);
}

int bins(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    while(l<r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            r=mid;
        }
    }
    if(l==r && gma(sta,l)==gmi(sta,r))
        return l;
    else
        return -1;

}

int bine(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    while(l<r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            l=mid;
        }

        if((l+1)==r)
        {
            if(gma(sta,r)==gmi(sta,r))
                return r;
            if(gma(sta,l)==gmi(sta,l))
                return l;

            return -1;
        }
    }
    if(l==r && gma(sta,l)==gmi(sta,r))
        return l;
    else
        return -1;

}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);

        cal();

        long long ans=0;
        for(int i=0;i<n;i++)
        {
            int s=bins(i);
            int e=bine(i);

            //printf("%d : %d %d\n",i,s,e);
            if(s!=-1 && e!=-1)
            {
                ans+=(e-s+1);
            }
        }
        printf("%lld\n",ans);
    }

}
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>

using namespace std;

int n;
int a[200005];
int b[200005];

int ma[200005][25];
int mi[200005][25];

void cal()
{
    for(int i=0;i<n;i++)
    {
        ma[i][0]=a[i];
        mi[i][0]=b[i];
    }


    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        {
            ma[i][j]=max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
            mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
        }
    }
}

int gma(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return max(ma[l][k],ma[r-(1<<k)+1][k]);
}

int gmi(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return min(mi[l][k],mi[r-(1<<k)+1][k]);
}

int bins(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    int ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            r=mid-1;
            ans=mid;
        }
    }
    return ans;

}

int bine(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    int ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            l=mid+1;
            ans=mid;
        }
    }
    return ans;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);

        cal();

        long long ans=0;
        for(int i=0;i<n;i++)
        {
            int s=bins(i);
            int e=bine(i);

            //printf("%d : %d %d\n",i,s,e);
            if(s!=-1 && e!=-1)
            {
                ans+=(e-s+1);
            }
        }
        printf("%lld\n",ans);
    }

}

 

当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值