Poj 3263 tallest cow (line segment tree)

Source: Internet
Author: User

Language:DefaultTallest cow
Time limit:2000 ms   Memory limit:65536 K
Total submissions:1964   Accepted:906

Description

FJ'sN(1 ≤N≤ 10,000) cows conveniently indexed 1 ..NAre standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the heightH(1 ≤H≤ 1,000,000) of the tallest cow along with the indexIOf that cow.

FJ has made a listR(0 ≤R≤ 10,000) lines of the form "cow 17 sees cow 34 ". this means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.

For each cow from 1 ..N, Determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

Input

Line 1: four space-separated integers:N,I,HAndR 
Lines 2 ..R+ 1: two distinct space-separated IntegersAAndB(1 ≤A,B≤N), Indicating that cowACan see cowB.

Output

Lines 1 ..N: LineIContains the maximum possible height of cowI.

Sample Input

9 3 5 51 35 34 33 79 8

Sample output

545344555

Source

Usaco 2007 January silver

Question: the highest possible height of a cow is given. Then, input M group data a B to represent a and B. Finally, ask all cows to output the highest possible height.


Note: 1> duplicate edges may exist.

2> A, B can look at is to reduce the height of A + 1 to B-1 between the ox


Code for details:


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)using namespace std;#define INF 0x3f3f3f3f#define N  1000005int n,i,h,m;int lle[N],rri[N],k;struct stud{int le,ri;int va;}f[N];void pushdown(int pos){    if(f[pos].va==0) return ;  f[L(pos)].va+=f[pos].va;      f[R(pos)].va+=f[pos].va;      f[pos].va=0;}void build(int pos,int le,int ri){f[pos].le=le;f[pos].ri=ri;f[pos].va=0;if(le==ri) return;int mid=MID(le,ri);build(L(pos),le,mid);build(R(pos),mid+1,ri);}void update(int pos,int le,int ri){if(f[pos].le==le&&f[pos].ri==ri){f[pos].va++;return ;}    pushdown(pos);    int mid=MID(f[pos].le,f[pos].ri);    if(mid>=ri)update(L(pos),le,ri);elseif(mid<le)update(R(pos),le,ri);else{update(L(pos),le,mid);update(R(pos),mid+1,ri);}}int query(int pos,int le){if(f[pos].le==le&&f[pos].ri==le){return h-f[pos].va;}    pushdown(pos);    int mid=MID(f[pos].le,f[pos].ri);    if(mid>=le)return query(L(pos),le);elsereturn query(R(pos),le);}int main(){int i,j;while(~scanf("%d%d%d%d",&n,&i,&h,&m)){build(1,1,n);k=0;lle[0]=rri[0]=0;int le,ri;while(m--){scanf("%d%d",&le,&ri);if(le>ri) {i=le; le=ri;ri=i;}if(le+1==ri) continue;for(i=0;i<k;i++)if(lle[i]==le&&rri[i]==ri){i=-1;break;}           if(i==-1) continue;           lle[k]=le;           rri[k++]=ri;           update(1,le+1,ri-1);}    for(i=1;i<=n;i++){printf("%d\n",query(1,i));}}   return 0;}






Poj 3263 tallest cow (line segment tree)

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.