[POJ3263]Tallest Cow

本文介绍了一个利用线段树数据结构解决特定高度限制问题的算法。该问题涉及确定N头奶牛的最大可能高度,同时确保满足一系列可见性约束条件。通过线段树实现单点查询和区间更新,有效地解决了这一挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tallest Cow
Time Limit: 2000MS
Memory Limit: 65536K

####Description
FJ’s N ( 1 ≤ N ≤ 10 , 000 ) N (1 ≤ N ≤ 10,000) N(1N10,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 ) H (1 ≤ H ≤ 1,000,000) H(1H1,000,000) of the tallest cow along with the index I of that cow.

FJ has made a list of R ( 0 ≤ R ≤ 10 , 000 ) R (0 ≤ R ≤ 10,000) R(0R10,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, H and R
Lines 2…R+1: Two distinct space-separated integers A and B ( 1 ≤ A , B ≤ N ) (1 ≤ A, B ≤ N) (1A,BN), 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

题意
N头奶牛站成一排,最高的奶牛身高为H,给出R条信息,身高最高的奶牛有I头(这信息貌似没啥卵用),每条信息A,B表示编号为A的可以看见编号为B的,说明编号为B的奶牛身高≥编号为A的奶牛身高,并且编号在他们之间的奶牛的身高严格小于他们呢的身高。求每只奶牛的身高(要求平均身高最高)

题解
线段树单点查询区间修改。
先将所有身高赋值成H,然后根据信息,将A,B中间的所有奶牛身高减一。注意重复信息。

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,I,R,H;
struct seg{
    int l,r,w,tag;
}tr[250004];
struct data{
    int a,b;
}p[10004];
inline bool dex(data A,data B){
    return A.a==B.a?A.b<B.b:A.a<B.a;
}
inline bool SAME(data A,data B){
     return (A.a==B.a&&A.b==B.b);
}
void build(int k,int l,int r){
     tr[k].l=l;tr[k].r=r;
     if(l==r){
        tr[k].l=tr[k].r=l;
        tr[k].w=H;tr[k].tag=0;
        return ;
     }
     int mid=(l+r)>>1;
     build(k<<1,l,mid);
     build(k<<1|1,mid+1,r);
}
void pushdown(int k){
     int x=tr[k].tag;
     tr[k].tag=0;
     tr[k<<1].w+=x;
     tr[k<<1|1].w+=x;
     tr[k<<1].tag+=x;
     tr[k<<1|1].tag+=x;
}
void change(int k,int a,int b){
     int l=tr[k].l,r=tr[k].r;
     if(l==a&&r==b){
        tr[k].tag--;
        tr[k].w--;
        return ;
     }
     if(tr[k].tag!=0)pushdown(k);
     int mid=(l+r)>>1;
     if(b<=mid)change(k<<1,a,b);
     else if(a>mid)change(k<<1|1,a,b);
     else{
        change(k<<1,a,mid);
        change(k<<1|1,mid+1,b);
     }
}
int ask(int k,int p){
    int l=tr[k].l,r=tr[k].r;
    if(l==r&&l==p)return tr[k].w;
    if(tr[k].tag!=0)pushdown(k);
    int mid=(l+r)>>1;
    if(p<=mid)return ask(k<<1,p);
    else return ask(k<<1|1,p);
}
int w33ha(){
    build(1,1,n);
    for(int i=1;i<=R;i++){
        scanf("%d%d",&p[i].a,&p[i].b);
        if(p[i].a>p[i].b)swap(p[i].a,p[i].b);
    }
    sort(p+1,p+R+1,dex);
    while(R){
        while(SAME(p[R],p[R-1]))R--;
        if(p[R].a+1==p[R].b){R--;continue;}
        change(1,p[R].a+1,p[R].b-1);
        R--;
    }
    for(int i=1;i<=n;i++)printf("%d\n",ask(1,i));
    return 0;
}
int main(){
    while(scanf("%d%d%d%d",&n,&I,&H,&R)!=EOF)w33ha();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值