hdu简单的线段树

hdu1754,地址:http://acm.hdu.edu.cn/showproblem.php?pid=1754 

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26556    Accepted Submission(s): 10542


Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
 

Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
 

Output
对于每一次询问操作,在一行里面输出最高成绩。
 

Sample Input
  
  
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
 

Sample Output
  
  
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
 

先更新叶子节点的数值,再逐层往上更新区间最大值

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define M 200000
struct node{
	int l,r,max;
}tree[3*M];
int max(int i,int j) {return i>j?i:j;}
void ori_tree(int n,int l,int r)   //建树
{
	int m=(l+r)/2;
	tree[n].max=0;
	tree[n].l=l;
	tree[n].r=r;
	if(l!=r)
	{
		ori_tree(n*2,l,m);
		ori_tree(n*2+1,m+1,r);
	}
}
void inseart(int n,int num,int s)   //更新
{
	int m=(tree[n].l+tree[n].r)/2;
	if(tree[n].l==tree[n].r) tree[n].max=s;
	else if(num<=m)
	{
		inseart(n*2,num,s);
		tree[n].max=max(tree[n*2].max,tree[n*2+1].max);
	}
	else
	{
		inseart(n*2+1,num,s);
		tree[n].max=max(tree[n*2].max,tree[n*2+1].max);
	}
}
int sum(int n,int l,int r)  //查找或者输出
{
	int m=(tree[n].l+tree[n].r)/2;
	if(tree[n].l==l&&tree[n].r==r) return tree[n].max;
	else if(r<=m) return sum(n*2,l,r);
	else if(l>m) return sum(n*2+1,l,r);
	else return max(sum(n*2,l,m),sum(n*2+1,m+1,r));
}
int main()
{
	int t,m,n,i,l,r;
	char str;
	while(scanf("%d%d",&m,&t)>0)
	{
		ori_tree(1,1,m);
		for(i=1;i<=m;i++)
		{
			scanf("%d",&n);
			inseart(1,i,n);
		}
		getchar();
		for(i=1;i<=t;i++)
		{
			scanf("%c%d%d",&str,&l,&r);getchar();
			if(str=='U')
				inseart(1,l,r);
			if(str=='Q')
				printf("%d\n",sum(1,l,r));
		}
	}
	return 0;
}

hdu2795,地址:http://acm.hdu.edu.cn/showproblem.php?pid=2795

Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7277    Accepted Submission(s): 3260


Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).

Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
 

Input
There are multiple cases (no more than 40 cases).

The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
 

Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
 

Sample Input
  
  
3 5 5 2 4 3 3 3
 

Sample Output
  
  
1 2 1 3 -1
 

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define M 200010
struct node{
	int l,r,max;
}tree[3*M];
int w;
void ori_tree(int i,int l,int r)
{
	int m=(l+r)/2;
	tree[i].max=w;
	tree[i].l=l;
	tree[i].r=r;
	if(l!=r)
	{
		ori_tree(i*2,l,m);
		ori_tree(i*2+1,m+1,r);
	}
}
void updata(int i,int t)
{
	if(tree[i].l==tree[i].r)
	{
		tree[i].max-=t;
		printf("%d\n",tree[i].l);
	}
	else
	{
		int m=(tree[i].l+tree[i].r)/2;
		if(tree[i*2].max>=t)
			updata(i*2,t);
		else if(tree[i*2+1].max>=t)
			updata(i*2+1,t);
		if(tree[i*2].max>tree[i*2+1].max)
			tree[i].max=tree[i*2].max;
		else tree[i].max=tree[i*2+1].max;
	}
}
int main()
{
	int h,n,i;
	while(scanf("%d%d%d",&h,&w,&n)>0)
	{
		if(h>n) h=n;    //如果h大于n时,则最多写入n条广告,所以极值由10^9变为200000
		ori_tree(1,1,h);
		while(n--)
		{
			scanf("%d",&i);
			if(tree[1].max>=i)
				updata(1,i);
			else printf("-1\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值