POJ 2481 Cows tree-like array

Source: Internet
Author: User
Tags printf ranges

Cows

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15386 Accepted: 5128

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

Farmer John have N cows (we number The cows from 1 to N). Each of Farmer John's N cows have a range of clover that she particularly likes (these ranges might overlap). The ranges is defined by a closed interval [s,e]. 

But some cows is strong and some are weak. Given 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. 

Fo R each cow, how many cows is stronger than her? Farmer John needs your help!

Input the input contains multiple test cases.
For each test case, the first line was an integer n (1 <= n <= 5), and which is the number of cows. Then come N lines, the i-th of which contains both Integers:s and E (0 <= S < E <= 5) specifying the start end Location respectively of a range preferred by some cow. Locations is 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 is stronger than cow I.

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.


Test instructions: Given n sub-range, ask how many intervals contain the interval of I. (Only one endpoint of the interval is allowed to overlap)


Num[i].s record left endpoint, num[i].t record right Endpoint

Idea: Consider fixing an endpoint. Assuming a fixed s, then in ascending order of S, we will guarantee the NUM[I].S <= Num[i+1].s, then we find that only when num[i].t > num[i+1].t the section I contains the i+1 interval, then we can press T in descending order.

This may be the case of i+1 and I intervals (because the interval coincidence can be directly determined, it is not listed)

First, Num[i].s = = Num[i+1].s, num[i+1].t < num[i].t

II, NUM[I].S < NUM[I+1].S, num[i+1].t <= num[i].t

Three, Num[i].s < Num[i+1].s, num[i+1].t > num[i].t

Obviously, only the >=num[i+1].t, is the legal interval. This makes it possible to >= the interval number of the right endpoint and then map the right endpoint.


There is also a question of how many of the (true) sub-ranges of the interval of I are in question.

Solution: s Descending and T Ascending, statistics <= the right end of the interval number, and then mapping the right end point is good.


AC Code:


#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib&
Gt #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> # Include <vector> #include <string> #define INF 0x3f3f3f3f #define EPS 1e-8 #define MAXN (100000+10) #define MA XM (200000+10) #define RI (a) scanf ("%d", &a) #define RL (a) scanf ("%lld", &a) #define RF (a) scanf ("%lf", &a) #d Efine Rs (a) scanf ("%s", a) #define PI (a) printf ("%d\n", (a)) #define PF (a) printf ("%.2lf\n", (a)) #define PL (a) printf ("%l Ld\n ", (a)) #define PS (a) printf ("%s\n ", (a)) #define W (a) while (a--) #define CLR (A, B) memset (A, (b), sizeof (a)) #define MOD 1000000007 #define LL Long long #define Lson o<<1, L, Mid #define Rson o<<1|1, mid+1, R #define LL O&LT;&L
T;1 #define RR o<<1|1 #define PI ACOs ( -1.0) #define First fi #define Second se using namespace std;
struct node{int s, T, id;}; Node Num[maXN];
    BOOL CMP (Node A, Node B) {if (a.s! = B.S) return A.S < B.S;
else return A.T > b.t;
} int N = 100001;
int C1[MAXN], C2[MAXN];
        int lowbit (int x) {return x & (-X);} void Update (int *c, int x) {while (x <= N) {c[x] + = 1;
    x + = Lowbit (x);
    }} int sum (int *c, int x) {int s = 0;
        while (x > 0) {s + = c[x];
    X-= Lowbit (x);
} return s;
} int ANS[MAXN];
    int main () {int n; while (Ri (n), N) {CLR (C1, 0);
        CLR (C2, 0);
            for (int i = 0; i < n; i++) {ri (num[i].s), RI (num[i].t); num[i].s++;
            num[i].t++;
        Num[i].id = i;
        } sort (num, num+n, CMP); for (int i = 0; i < n; i++) {if (i && num[i].s = = Num[i-1].s && num[i].t = = Num[i-1]
            . t) ans[num[i].id] = ans[num[i-1].id]; else ans[num[i].id] = SUM (C1, N)-sum (C1, num[i].t-1);
        Update (C1, num[i].t);
            } for (int i = 0; i < n; i++) {if (i) printf ("");
        printf ("%d", ans[i]);
    } printf ("\ n");
} return 0;
 }


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.