zoukankan      html  css  js  c++  java
  • poj 3263 Tallest Cow(线段树)

    Language:
    Tallest Cow
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 1964   Accepted: 906

    Description

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

    FJ has made a list of (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: NIH and R 
    Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ AB ≤ N), indicating that cow A can see cow B.

    Output

    Lines 1..N: Line i contains the maximum possible height of cow i.

    Sample Input

    9 3 5 5
    1 3
    5 3
    4 3
    3 7
    9 8

    Sample Output

    5
    4
    5
    3
    4
    4
    5
    5
    5

    Source



    题意:给出牛的可能最高身高。然后输入m组数据 a b,代表a,b能够相望,最后求全部牛的可能最高身高输出


    注意问题:1>可能有重边

                        2>  a,b能够相望就是要减少a+1到b-1之间的牛的身高


    详情看代码:


    #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  1000005
    
    int 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);
    	else
    		if(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);
    	else
    		return 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
    ",query(1,i));
    		}
    
    	}
       return 0;
    }
    
    






  • 相关阅读:
    Java中Runnable和Thread的区别
    JAVA Swing 事件监听
    java 监听机制模拟(JButton按钮监听机制)
    java事件处理机制(自定义事件)
    oracle之检查点(Checkpoint)
    linux内核值shmmax问题
    如何在VMware虚拟机间建立共享磁盘?
    Mysql 不同版本 说明
    mysql 概念和逻辑架构
    mysql 在大型应用中的架构演变
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6844939.html
Copyright © 2011-2022 走看看