Poj 3263 Tallest Cow (line segment tree), poj3263

Source: Internet
Author: User

Poj 3263 Tallest Cow (line segment tree), poj3263

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,BN), 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;}







Who has the correspondence between data structure algorithms and POJ questions?

An asterisk indicates a typical question or a better algorithm.

1195 Mobile phones tree Array
1455
1521 Entropy huffman
1703 Find them, Catch them and query the set
1785 Binary Search Heap Construction
1794 Castle Wils Reverse Order
1961 Period KMP recurrence factor
1984 * Navigation Nightmare and check the set + coordinate translation
1986 * Distance Queries LCA
1988 * Cube Stacking and query set applications
1990 * MooFest line segment tree
2010 * Moo University-Financial Aid Max heap-min heap
2182 Lost Cows line segment tree
2183 Bovine Math Geniuses hash
2188 Cow Laundry Reverse Order
2227 The Wedding Juicer heap + floodfill
2236 Wireless Network and query set
2266 * Quadtree Recursion
2269 * Friends expression
2270 Quadtree II or: Florida Jones strikes back will be 2266 vice versa
2299 Ultra-QuickSort Merge Sorting
2352 Stars tree Array
2395 Out of Hay and query set
2482 Stars in Your Window static 2 Cross Tree
2513 Colored Sticks and check the set
2524 Ubiquitous Religions and query set
2528 Mayor's posters line segment tree
2567 Code the Tree
2750 * Potted Flower line segment tree
2777 Count Color line segment tree
2796 Feel Good RMQ
2823 Sliding Window heap or double-end queue
2828 Buy Tickets line segment tree
2886 * Who Gets the Most Candies? Line Segment tree
2892 * Tunnel Warfare tree Array
3214 * Heap post-order traversal. Each node deducts the corresponding sub-guarantee attribute, and then calculates the longest non-descending sequence for the traversal result.
3253 Fence Repair huffman
3263 Tallest Cow line segment tree
3274 * Gold Balanced Lineup hash
3277 City Horizon line segment tree
3320 Jessica's Reading Problem queue operation or minimum heap
3321 * Apple Tree Array
3332 Parsing Real Numbers DFA
3344 Chessboard Dance queue Simulation
3349 Snowflake Snow Snowflakes hash (or brute force)
3437 Tree Grafting dfs Tree construction
3461 Oulipo KMP
3468 A Simple Problem with Integers segment tree Interval Update, lazy operation
3631 Cuckoo Hashing and query Sets
3667 Hotel line segment tree
3690 Constellations trie match
3695 Rectangles matrix cutting... remaining full text>

For the poj2777 question, I will think about the line segment tree and lazy, but I don't understand what it is like to use bits to record colors, or combine colors with values.

This is lazytag ....
Each line segment Tree node records the following information: sum, p, q, tag, pre, suf
There are sum different colors in p and q respectively. tag indicates lazy mark,-1 indicates no mark, and others indicate to dye this interval into tag color; pre indicates the color of the Left endpoint of the interval, and suf indicates the right endpoint.

When updating and querying, first upload a flag... This is the same as other lazy labels.
When the flag is passed down, clear the tag directly, set pre and suf to the tag color, sum = 1, and mark it to the Son (if any)

Then, update the node (note that this node is u, the left son is l, and the right son is r)
First, Mark l and r, and then
U. pre = l. pre;
U. suf = r. suf;
U. sum = l. sum + r. sum-(l. suf = r. pre );
Describe the last
First, set u. sum to l. sum + r. sum,
However, when the color of the left-son suffix is the same as that of the right-son prefix, the two colors are actually the same, so we calculate it once, and subtract
Nothing else.

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.