codeforces 652C Foe Pairs 水题

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

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

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

这是一场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

 

posted @ 2016-03-27 18:51  shuguangzw  阅读(356)  评论(0编辑  收藏  举报