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

codeforces 652C Foe Pairs 水题

时间:2016-03-27 19:20:23      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

题意:给你若干个数对,给你一个序列,保证数对中的数都在序列中

        对于这个序列,询问有多少个区间,不包含这些数对

分析:然后把这些数对转化成区间,然后对于这些区间排序,然后扫一遍,记录最靠右的左端点就好

这是一场cf edu 然后当时做的时候想都没想就树状数组了,SB了,其实不需要

技术分享
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=3e5+5;
const int INF=0x3f3f3f3f;
int a[N],n,m;
struct pair{
  int x,y;
  bool operator<(const pair &e)const{
     return y<e.y;
  }
}p[N];
int mp[N];
int c[N];
void change(int x){
   for(int i=x;i<=n;i+=i&(-i))
     c[i]=max(c[i],x);
}
int query(int x){
  int ans=0;
  for(int i=x;i>0;i-=i&(-i))
    ans=max(ans,c[i]);
  return ans;
}
int main(){ 
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
      scanf("%d",&a[i]),mp[a[i]]=i;
    for(int i=1;i<=m;++i){
      scanf("%d%d",&p[i].x,&p[i].y);
      p[i].x=mp[p[i].x],p[i].y=mp[p[i].y];
      if(p[i].x>p[i].y)swap(p[i].x,p[i].y); 
    }
    sort(p+1,p+1+m);
    LL ans=0;
    int cnt=1;
    for(int i=1;i<=n;++i){
      for(;cnt<=m&&p[cnt].y<=i;++cnt)
        change(p[cnt].x);
      LL l=query(i);
      ans+=i-l;
    }
    printf("%I64d\n",ans);
    return 0;
}
View Code

 

codeforces 652C Foe Pairs 水题

标签:

原文地址:http://www.cnblogs.com/shuguangzw/p/5326451.html

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