Tallest Cow POJ - 3263(线段树区间修改,单点查询)

Tallest Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2388 Accepted: 1094

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

有n头牛,第I个位置上的牛高度最高H,然后有m个情况,每个情况x,y位置表示x能够看到y,也就是x到y之间的牛的个头肯定是低于xy,现在要输出符合条件的每个牛的高度

解题思路:初始化每头牛高度都是H,然后每次给区间的时候对区间的每牛都减1,然后单点查询即可,值得注意的是,会有重边,所以最好的操作就是把更新排序去重之后重新更新

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

const int MAXN=1e4;
struct tree
{
	int flag,cnt;
}a[MAXN*4+5];
int n,I,H,m;
struct point 
{
	int x,y;
}b[10005];
void build(int l,int r,int deep)
{
	a[deep].flag=0;
	if(l==r)
	{
		a[deep].cnt=H;
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,deep*2);
	build(mid+1,r,deep*2+1);
}
void pushdown(int deep)
{
	if(a[deep].flag)
	{
		a[deep*2].flag+=a[deep].flag;
		a[deep*2+1].flag+=a[deep].flag;
		a[deep].flag=0;
	}
}
void update(int L,int R,int l,int r,int deep)
{
	if(L>R)
		return;
	if(L<=l&&r<=R)
	{
		a[deep].flag--;
		return;
	}
	pushdown(deep);
	int mid=(l+r)/2;
	if(L<=mid) update(L,R,l,mid,deep*2);
	if(mid<R) update(L,R,mid+1,r,deep*2+1);
}
int query(int L,int R,int l,int r,int deep)
{
	if(L<=l&&r<=R)
		return a[deep].cnt+a[deep].flag;
	pushdown(deep);
	int sum=0;
	int mid=(l+r)/2;
	if(L<=mid) sum+=query(L,R,l,mid,deep*2);
	if(mid<R) sum+=query(L,R,mid+1,r,deep*2+1);
	return sum;
}
bool cmp(point x,point y)
{
	if(x.x==y.x)
		return x.y<y.y;
	else
		return x.x<y.x;
}
int main()
{
	int i,x,y;
	scanf("%d%d%d%d",&n,&I,&H,&m);
	build(1,n,1);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&b[i].x,&b[i].y);
		if(b[i].x>b[i].y)
			swap(b[i].x,b[i].y);
	}
	sort(b+1,b+1+m,cmp);
	for(i=1;i<=m;i++)
	{
		if(b[i].x==b[i-1].x&&b[i].y==b[i-1].y)
			continue;
		update(b[i].x+1,b[i].y-1,1,n,1);
	}
	for(i=1;i<=n;i++)
		printf("%d\n",query(i,i,1,n,1));
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值