Poj 3067 Japan (line segment tree | tree array)

Source: Internet
Author: User

Link:

Http://poj.org/problem? Id = 3067


Question:

On both sides of the east and west sides of Japan, the coastline is north-south. There are N and M cities on both sides, and their numbers are 1... n, 1... M.

To build highways between cities on the east and west coast, find the number of intersections (one intersection must have only two roads ).


Analysis and Summary:

This question is not hard to think about. All the roads are changed to the starting point U on the East Coast, and the West Coast is the ending point V on the West Coast. Then sort by u in ascending order (V Order is not important ). After sorting, enumeration is equivalent to establishing connections from City 1 on the west coast until n.

When I reach the target city of V, how many intersections will this edge link add in the past? You only need to draw a sketch to find out that it is the sum of all the edges of the city that have been changed to be greater than v.

After knowing this, you can directly use a tree array or line segment tree to cut it out.


There are two notes:

1. The question is not directly given to the data range of K. The array should be open

2. Use long


Code:

1. Tree Array

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long int64;const int MAXN = 1000000;int n,m,k;int64 c[MAXN];struct node{    int u,v;    friend bool operator<(const node&a,const node&b){        if(a.u!=b.u) return a.u<b.u;        return a.v<b.v;    }}arr[MAXN];inline int lowbit(int x){return x&(-x);}int64 sum(int x){    int64 ret=0;    while(x>0){        ret += c[x];        x -= lowbit(x);    }    return ret;}void add(int x){    while(x<=m){        ++c[x];        x += lowbit(x);    }}int main(){    int T,cas=1;    scanf("%d",&T);    while(T--){        printf("Test case %d: ",cas++);        memset(c, 0, sizeof(c));        scanf("%d%d%d",&n,&m,&k);        for(int i=0; i<k; ++i)             scanf("%d%d",&arr[i].u,&arr[i].v) ;        sort(arr,arr+k);        int64 ans=0;        for(int i=0; i<k; ++i){            ans += sum(m)-sum(arr[i].v) ;            add(arr[i].v);        }        printf("%lld\n",ans);    }    return 0;}

2. Line Segment tree

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define mid ((left+right)>>1)#define lson rt<<1,left,mid#define rson rt<<1|1,mid+1,rightusing namespace std;typedef long long int64;const int MAXN = 1000000;int n,m,k;int64 c[MAXN];struct node{    int u,v;    friend bool operator<(const node&a,const node&b){        if(a.u!=b.u) return a.u<b.u;        return a.v<b.v;    }}arr[MAXN];void update(int rt,int left,int right,int data){    ++c[rt];    if(left==right)return;    if(data <= mid) update(lson,data);    else update(rson,data);}int query(int rt,int left,int right,int l,int r){    if(left==l && right==r) return c[rt];    int m = mid;    if(r <= m) return query(lson,l,r);    else if(l > m) return query(rson,l,r);    else return query(lson,l,m)+query(rson,m+1,r);}int main(){    int T,cas=1;    scanf("%d",&T);    while(T--){        printf("Test case %d: ",cas++);        memset(c, 0, sizeof(c));        scanf("%d%d%d",&n,&m,&k);        for(int i=0; i<k; ++i) {            scanf("%d%d",&arr[i].u,&arr[i].v) ;        }        sort(arr,arr+k);        int64 ans=0;        for(int i=0; i<k; ++i){            ans += query(1,1,m+1,arr[i].v+1,m+1);            update(1,1,m+1,arr[i].v);        }        printf("%lld\n",ans);    }    return 0;}


 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.