(树状数组) poj 2481

Cows

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 13421   Accepted: 4442

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.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define N 100010
int c[N],val[N];
struct node
{
      int x,y,id;
}e[N];
bool cmp(node a,node b)
{
      if(a.y!=b.y) return a.y>b.y;
      return a.x<b.x;
}
int lowbit(int x)
{
      return x&(-x);
}
void update(int pos,int m)
{
      while(pos<N)
      {
           c[pos]+=m;
           pos+=lowbit(pos);
      }
}
int sum(int x)
{
      int sum=0;
      while(x>0)
      {
            sum+=c[x];
            x=x-lowbit(x);
      }
      return sum;
}
int main()
{
      int n;
      while(scanf("%d",&n)!=EOF)
      {
            if(n==0)
                  break;
            memset(c,0,sizeof(c));
            for(int i=1;i<=n;i++)
            {
                 scanf("%d%d",&e[i].x,&e[i].y);
                 e[i].x++,e[i].y++;
                 e[i].id=i;
            }
            sort(e+1,e+1+n,cmp);
            val[e[1].id]=sum(e[1].x);
            update(e[1].x,1);
            for(int i=2;i<=n;i++)
            {
                if(e[i].x==e[i-1].x&&e[i].y==e[i-1].y)
                        val[e[i].id]=val[e[i-1].id];
                else val[e[i].id]=sum(e[i].x);
                update(e[i].x,1);
            }
            printf("%d",val[1]);
            for(int i=2;i<=n;i++)
                  printf(" %d",val[i]);
            printf("\n");
      }
      return 0;
}

  

时间: 2024-11-07 00:40:11

(树状数组) poj 2481的相关文章

树状数组 POJ 2481 Cows

题目传送门 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX_N = 100000 + 10; 7 int cnt[MAX_N]; 8 int ans[MAX_N]; 9 int maxn = -1; 10 struct node 11 { 12 int s, e; 13 int id; 14 }cow[MAX_

线段树/树状数组 POJ 2182 Lost Cows

题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号:为1,倒数第二头牛的编号为除去最后一头牛的编号后的第a[i-1] + 1编号:为3,其他的类推,所以可以维护之前已经选掉的编号,求第k大的数字,sum[rt] 表示该区间已经被选掉的点的个数.另外树状数组也可以做,只不过用二分优化查找第k大的位置. 收获:逆向思维,求动态第K大 代码(线段树): /

LCA+树状数组 POJ 2763 Housewife Wind

题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储每条边的信息,这样第二种操作也能用BIT快速解决 利用RMQ的写法不知哪里写挫了,改用倍增法 /************************************************ * Author :Running_Time * Created Time :2015/10/6 星期二

( 树状数组) poj 2029

Get Many Persimmon Trees Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3700   Accepted: 2405 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In

(树状数组) poj 3067

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21769   Accepted: 5885 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 coas

[qbzt寒假]线段树和树状数组

树状数组 \(lowbit(x)=x\&(-x)\) 二维树状数组 修改某个点,查询(1,1)到(n,m)的前缀和(树状数组要从1开始) HDU2642 Stars \(YFF\)是个浪漫的人,他喜欢数天上的星星. 为了解决这个问题,我们考虑到天空是一个二维平面,有时星星会很亮,有时星星会很暗.首先,天空中没有明亮的星星,然后一些信息会被给出为"\(B\) \(x\) \(y\)",其中"\(B\)"表示明亮,\(x\)表示\(x\)坐标,\(y\)表示在\

每次输出有几条线段能完全覆盖大于自己和hdu5372相反 树状数组或线段树 poj 2481 Cows

http://poj.org/problem?id=2481 Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14762   Accepted: 4886 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

POJ 2481 Cows(树状数组)

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). Ea

POJ 2481 Cows &amp;&amp; POJ 2352 Stars(树状数组妙用)

题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 "小" 的有多少点. Cows需要自己排序, Stars题目已经给排好序. POJ 2352 Stars 题目大意为在二维坐标上给出一些星星的坐标,求某一个星星左方,下方,左下方的星星个数.题目已经把星星按照Y坐标从小到大,X从小到大排序.因此,在每次对一个星星进行统计时,之前出现过的星星,只要X