码迷,mamicode.com
首页 > 编程语言 > 详细

poj 3067 poj 2481 树状数组变形+对区间排序

时间:2016-05-12 18:46:21      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:


这种问题先对区间和线段进行排序,排序方法见代码cmp


然后分析其中一个点,用sum求值


poj 3067


Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

Output

For each test case write one line on the standard output: 
Test case (case number): (number of crossings)

Sample Input

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
struct line
{
    int x,y;
}q[1000100];
int c[1010];
int cmp(line a,line b)
{
    if(a.x==b.x)
        return a.y>b.y;
    return a.x>b.x;
}
int n,m,k;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int val)//下标为x值增加val
{
    while(x<=m)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}
int sum(int x)//求下标1~x的数组之和
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int T;
    cin>>T;
    int kase=1;
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<=k-1;i++)
            scanf("%d%d",&q[i].x,&q[i].y);
        sort(q,q+k,cmp);
        memset(c,0,sizeof(c));
        LL cnt=0;
        for(int i=0;i<=k-1;i++)
        {
            cnt+=sum(q[i].y-1);
            add(q[i].y,1);
        }
        cout<<"Test case "<<kase++<<": "<<cnt<<endl;
    }
    return 0;
}


poj 2481


Description

Farmer John‘s cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. 

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John‘s N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E]. 

But some cows are strong and some are weak. Given two cows: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j

For each cow, how many cows are stronger than her? Farmer John needs your help!

Input

The input contains multiple test cases. 
For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge. 

The end of the input contains a single 0.

Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cow i

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
int c[100010];
int num[100010];
struct node
{
    int id;
    int l,r;
}q[100010];
int cmp(node a,node b)
{
    if(a.r==b.r)
        return a.l<b.l;
    return a.r>b.r;
}
int lowbit(int x)
{
    return x&(-x);
}

void add(int x,int val)//下标为x值增加val
{
    while(x<=100000)//n是数组的规模,容易出错
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}

int sum(int x)//求下标1~x的数组之和
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)
            return 0;
        for(int i=0;i<=n-1;i++)
        {
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q,q+n,cmp);
        memset(c,0,sizeof(c));
        for(int i=0;i<=n-1;i++)
        {
            if(q[i].l==q[i-1].l&&q[i].r==q[i-1].r&&i!=0)
                num[q[i].id]=num[q[i-1].id];
            else
                num[q[i].id]=sum(q[i].l+1);
            add(q[i].l+1,1);
        }
        for(int i=0;i<=n-2;i++)
            cout<<num[i]<<" ";
        cout<<num[n-1]<<endl;
    }
    return 0;
}


poj 3067 poj 2481 树状数组变形+对区间排序

标签:

原文地址:http://blog.csdn.net/winycg/article/details/51355467

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