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;
}


發佈了149 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章