ZJNU 1903 Why Did the Cow Cross the Road III 树状数组

Why Did the Cow Cross the Road III

Time Limit: 3000MS Memory Limit: 3000K
Total Submissions: 23 Accepted: 5
Description

The layout of Farmer John’s farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn.

As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns N cows, conveniently identified with the integer IDs 1…N, so there are precisely 2N crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2N numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b) a “crossing” pair if cow a’s path from entry to exit must cross cow b’s path from entry to exit. Please help Farmer John count the total number of crossing pairs.

Input

The first line of input contains N (1≤N≤50,000), and the next 2N lines describe the cow IDs for the sequence of entry and exit points around the field.

Output

Please print the total number of crossing pairs.

Sample Input

4
3
2
4
4
1
3
2
1
Sample Output

3

题目链接

题意:给你n条线段,每个线段的起点为i第一次出现的位置,终点为i最后一次出现的位置,这些线段有多少个交点。不好读懂题目,所以画了个图…

这里写图片描述
恩…这是样例啊…能看懂意思就好…交点数就是答案。

解题思路:原来在比赛中能gank我的不只是难题,还有队友,因为队友的读错题,我的ak梦毁灭了(在此嘲笑队友x个操作数,(x>1e9)哈哈哈哈,虽然我的英语更渣2333),把每条边的left和right记录,然后按照left从小到大排序,那么对于排序后的第n条边,e[n].left肯定大于前面的所有边的left,那么我们只需要再考虑e[n].left小于前面边的right和e[n].right大于前面边的right即可,因此可以直接用前缀和解决,但是因为要更新就要更新一大块,所有我们可以用树状数组解决这个问题。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,a,ans;
int ss[100005];
struct node{
    int l,r;
}e[50005];
bool cmp(node a,node b){
    return a.l<b.l;
}
void update(int x){
    while(x<=2*n){
        ss[x]++;
        x+=x&(-x);
    }
}
int sum(int x){
    int ret=0;
    while(x>0){
        ret+=ss[x];
        x-=x&(-x);
    }
    return ret;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=2*n;i++){
        scanf("%d",&a);
        if(e[a].l==0)   e[a].l=i;
        else e[a].r=i;
    }
    sort(e+1,e+n+1,cmp);
    for(int i=1;i<=n;i++){
        update(e[i].r+1);
        ans+=sum(e[i].r)-sum(e[i].l);
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值