ZOJ 3573 Under Attack (线段树版)

超级水的线段树,主要是题目时间太宽松了,不然这题应该还是比较有难度的。。

第一次写结构体版的线段树,呵呵

#include<stdio.h>
typedef struct
{
 int l,r,d;
}Node;
int SUM=0;
Node tree[15000*4];
void Build(int l,int r,int th){
	tree[th].l=l;
	tree[th].r=r;
	tree[th].d=0;
	if(l==r) return ;
	int m=(tree[th].l+tree[th].r)/2;
	Build(tree[th].l,m,th*2);
	Build(m+1,tree[th].r,th*2+1);
}
void UpDate(int L,int R,int v,int th){
	if(L<=tree[th].l&&tree[th].r<=R){
		tree[th].d+=v;
		return ;
	}
	int m=(tree[th].l+tree[th].r)/2;
	if(m>=L) UpDate(L,R,v,th*2);
	if(m<R) UpDate(L,R,v,th*2+1);
}
void Query(int i,int th){
	SUM+=tree[th].d;
	if(tree[th].l==i&&tree[th].r==i) return ;
	int m=(tree[th].l+tree[th].r)/2;
	if(m>=i) Query(i,th*2);
	else Query(i,th*2+1);
}
int main(){
	int T;
	while(scanf("%d",&T)!=EOF){
		Build(0,T,1);
		int a,b,c;
		while(scanf("%d%d%d",&a,&b,&c)!=EOF){
			
			if(a==-1) break;
			UpDate(a,b,c,1);
		}
		int posl,posr,max;
		for(int i=0,max=0;i<=T;i++){
			SUM=0;
			Query(i,1);
			if(SUM>max){
		 		max=SUM;
		 		posl=i;
			}
		}
		for(int i=T,max=0;i>=0;i--){
			SUM=0;
			Query(i,1);
			if(SUM>max){
				max=SUM;
				posr=i;
			}
		}
		printf("%d %d\n",posl,posr);
	}
}
			


阅读更多
换一批

Under Attack

08-25

Doctor serves at a military air force base. One day, the enemy launch a sudden attack and the base is under heavy fire. The fighters in the airport must take off to intercept enemy bombers. However, the enemies know this clearly and they now focus on destroying the runway. The situation is becoming worse rapidly!nnEvery part of the runway has a damage level. On each bombing run, the damage level is increased by the bomb's damage . Fortunately, the enemy bombers has to stop bombing the runway when they run out of ammo. Then the ground crew have time to evaluate the situation of runway so that they can come to repair the runway ASAP after enemy attacks. The most heavily-damaged part on fighters' taking off and landing path should first be repaired. Assume that runway start from north and head to south , and fighters will take off or land only from north to south or vice versa.nnNow that the task is clear, the ground crew need the cooridinates of two points: first that is the most damaged point from north to south, second is the most damaged point from south to north.The base's central mainframe is down under hacker attack. So Doctor could only use his poor little and shabby notebook to fulfill this task. Can you help him?nnInputnnThe input consists of multiple cases. nThe first line is the runway length L. L can be up to 15000.nNext lines will describe enemy bombing runs ,each line describes effect range start end of each bombing run and enemy bomb damage d.if start is -1, this case ends..nThere can be up to 3000 bombing run, each time the damage is up to 100. nNotice that the bombing range is from north to south, and runway range is [0,len].nOutputnnOutput the cooridinates of two points: first that is the most damaged point from north to south, second is the most damaged point from south to north.nnSample Inputnn10n1 5 2n6 9 2n-1 -1 -1nSample Outputnn1 9

没有更多推荐了,返回首页