程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ 2481 Cows 簡單樹狀數組區間覆蓋

POJ 2481 Cows 簡單樹狀數組區間覆蓋

編輯:關於C++
點擊打開鏈接
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13334 Accepted: 4413

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: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.

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 <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) 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 cowi.

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU
n頭牛,每頭牛都有一個范圍[s,e],讓比它強壯的牛的個數。第i頭牛比第j頭牛強壯的定義:si<=sj&&ei>=ej&&ei-si>ej-si。 將n頭牛按照e從大到小排序,跟據s的值,用樹狀數組求得答案。
//2156K	1079MS
#include
#include
#include
#define M 100007
using namespace std;
int c[M],ans[M],max_e;
struct Cow
{
    int s,e,id;
}p[M];
int cmp(Cow a,Cow b)
{
    if(a.e==b.e)return a.sb.e;
}
int lowbit(int x)
{
    return x&(-x);
}
void add(int pos,int val)
{
    while(pos<=max_e+1)
    {
        c[pos]+=val;
        pos+=lowbit(pos);
    }
}
int getsum(int pos)
{
    int res=0;
    while(pos>0)
    {
        res+=c[pos];
        pos-=lowbit(pos);
    }
    return res;
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        max_e=0;
        for(int i=0;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved