欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[POJ3263]Tallest Cow

程序员文章站 2022-07-12 17:40:07
...

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

Description

FJ’s 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(1H1,000,000) of the tallest cow along with the index I of that cow.

FJ has made a list of 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 (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>
#define LiangJiaJun main
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 LiangJiaJun(){
    while(scanf("%d%d%d%d",&n,&I,&H,&R)!=EOF)w33ha();
    return 0;
}