码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #361 (Div. 2) D

时间:2016-07-12 23:03:32      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

                                                                             D - Friends and Subsequences

Description

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 and b 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 numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the sequence a.

The third line contains n integer numbers b1, 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.

Sample Input

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

Hint

The occasions in the first sample case are:

1.l = 4,r = 4 since max{2} = min{2}.

2.l = 4,r = 5 since max{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.

题意:

        给出两个长为n的序列a和b,问有多少个区间[L,R]满足max<a>[L,R] == min<b>[L,R]。

分析:

        枚举左端点,二分找到右端点可行区间的左右边界;二分右端点需要用RMQ(RQM预处理和查询的相关知识点需要另外了解)。

   左边界:要是a>=b,左移;否则右移,找到第一个a=b的点;右边界:要是a>b,左移,否则右移,找到最后一个a=b的点;最后累加右端点可

   行区间长度;

AC的代码:

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
int rmq[2][20][200200];
void RMQ(int n)
{
    for(int k = 1; (1<<k) <= n; ++k)
        for(int i = 0; i+(1<<k) <= n; ++i)
          {
              rmq[0][k][i] = max(rmq[0][k-1][i],rmq[0][k-1][i+(1<<(k-1))]);
            rmq[1][k][i] = min(rmq[1][k-1][i],rmq[1][k-1][i+(1<<(k-1))]);
          }
}
int Search(int pos,int l,int r)
{
    int k = log((r-l+1)*1.0)/log(2.0);
    if(pos) return min(rmq[pos][k][l],rmq[pos][k][r-(1<<k)+1]);
    else return max(rmq[pos][k][l],rmq[pos][k][r-(1<<k)+1]);
}

int main()
{
   int n;
   scanf("%d",&n);
   for(int i=0;i<n;i++)
     scanf("%d",&rmq[0][0][i]);
    for(int i=0;i<n;i++)
     scanf("%d",&rmq[1][0][i]);
   RMQ(n);
     LL ans=0;
       int l,r,a,b,s,e;
       for(int i=0;i<n;i++)
       {
           l=i;
           r=n-1;
           s=-1;
           while(l<=r)
           {
               int mid=(l+r)/2;
                a=Search(0,i,mid);
                b=Search(1,i,mid);
               if(a>=b)
               {
                   if(a==b)   s=mid;
                   r=mid-1;
                }
               else
                 l=mid+1;
           }
           if(s==-1) continue;
           l=i;
           r=n-1;
           e=-1;
           while(l<=r)
           {
               int mid=(l+r)/2;
                a=Search(0,i,mid);
                b=Search(1,i,mid);

                if(a>b)  r=mid-1;
                else
                    {
                      e=mid;
                       l = mid+1;
                    }
           }
           ans+=(e-s+1);
   }
   printf("%lld\n",ans);
    return 0;
}

另一种方法:

技术分享
#include<bits/stdc++.h>
using namespace std;
int n,a[200005],b[200005];
long long ans;
deque<int>x,y;
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    for(int i=1; i<=n; i++) scanf("%d",&b[i]);
    for(int i=1,j=1; i<=n; i++)
    {
        while(!x.empty()&&a[x.back()]<=a[i])  x.pop_back();
        while(!y.empty()&&b[y.back()]>=b[i])  y.pop_back();
        x.push_back(i);
        y.push_back(i);
        while(j<=i&&a[x.front()]-b[y.front()]>0)
        {
            j++;
            while(!x.empty()&&x.front()<j) x.pop_front();
            while(!y.empty()&&y.front()<j) y.pop_front();
        }
        if(!x.empty()&&!y.empty()&&a[x.front()]==b[y.front()])
        {
            ans+=min(x.front(),y.front())-j+1;
        }
    }
    printf("%lld",ans);
}
View Code

 

 

Codeforces Round #361 (Div. 2) D

标签:

原文地址:http://www.cnblogs.com/fenhong/p/5665025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!