Tallest Cow(线段树较易)

题目描述

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 R (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.

输入

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), indicating that cow A can see cow B.

输出

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

样例输入

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

样例输出

5
4
5
3
4
4
5
5
5

区间覆盖并且不会有相交的区间。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int M=10100;
int le[M],ri[M];
ll N,H,I;
ll s[M<<2],ans[M];
struct node
{
    int l,r;
    int sum;
} t[M<<2];
void pushdown(int rs)
{
    if(s[rs])
    {
        s[rs*2]+=s[rs];
        s[rs*2+1]+=s[rs];
        s[rs]=0;
    }
}
void build(int l,int r,int rs)
{
    t[rs].l=l;
    t[rs].r=r;
    s[rs]=0;
    if(l==r)
    {
        t[rs].sum=H;
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,rs*2);
    build(mid+1,r,rs*2+1);
}
void update(int l,int r,int rs)
{
    if(t[rs].l>=l&&t[rs].r<=r)
    {
        s[rs]+=1;
        return ;
    }
    pushdown(rs);
    int mid=(t[rs].l+t[rs].r)/2;
    if(l<=mid)
        update(l,r,rs*2);
    if(r>mid)
        update(l,r,rs*2+1);
}
int query(int k,int rs)
{
    if(t[rs].l==t[rs].r)
        return t[rs].sum-=s[rs];
    pushdown(rs);
    int mid=(t[rs].l+t[rs].r)/2;
    if(k<=mid)
        return query(k,rs*2);
    else
        return query(k,rs*2+1);
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int i,j,k=0,m,n;
    int x,y,T,flag;
    cin>>N>>I>>H>>T;
    build(1,N,1);
    while(T--)
    {
        cin>>x>>y;
        if(x>y)
            swap(x,y);
        flag=1;
        for(i=0; i<k; i++)
            if(x==le[i]&&y==ri[i])
            {
                flag=0;
                break;
            }
        if(flag==0)
            continue;
        le[k]=x,ri[k++]=y;
        if(y-x==1)
            continue;
        update(x+1,y-1,1);
    }
    for(i=1; i<=N; i++)
        ans[i]=query(i,1);
    for(i=1; i<=N; i++)
        cout<<ans[i]<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/nublity/p/9296618.html

时间: 2024-11-05 12:24:25

Tallest Cow(线段树较易)的相关文章

poj 3263 Tallest Cow(线段树)

Language: Default 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 i

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

Codeforces 283E Cow Tennis Tournament 线段树 (看题解)

Cow Tennis Tournament 感觉这题的难点在于想到求违反条件的三元组.. 为什么在自己想的时候没有想到求反面呢!!!! 违反的三元组肯定存在一个人能打败其他两个人, 扫描的过程中用线段树维护一下就好了. 反思: 计数问题: 正难则反 正难则反 正难则反 !!!! #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi

【BZOJ3939】Cow Hopscotch(动态开点线段树)

题意: 就像人类喜欢跳格子游戏一样,FJ的奶牛们发明了一种新的跳格子游戏.虽然这种接近一吨的笨拙的动物玩跳格子游戏几乎总是不愉快地结束,但是这并没有阻止奶牛们在每天下午参加跳格子游戏 游戏在一个R*C的网格上进行,每个格子有一个取值在1-k之间的整数标号,奶牛开始在左上角的格子,目的是通过若干次跳跃后到达右下角的格子,当且仅当格子A和格子B满足如下条件时能从格子A跳到格子B: 1.B格子在A格子的严格右方(B的列号严格大于A的列号) 2.B格子在A格子的严格下方(B的行号严格大于A的行号) 3.

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

poj 3264(RMQ或者线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42929   Accepted: 20184 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John de

POJ3264_Balanced Lineup(线段树/单点更新)

解题报告 题意: 求区间内最大值和最小值的差值. 思路: 裸线段树,我的线段树第一发.区间最值. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 #define LL long long using namespace std; LL minn[201000],maxx[201000]; void update(LL root,LL l,LL r,LL p,LL

POJ-3264 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47042   Accepted: 22071 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh