Under Attack(zoj)

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!

Every 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.

Now 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?

Input

The input consists of multiple cases.
The first line is the runway length L. L can be up to 15000.
Next 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..
There can be up to 3000 bombing run, each time the damage is up to 100.
Notice that the bombing range is from north to south, and runway range is [0,len].

Output

Output 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.

Sample Input
10            //长0到10的路
1 5 2    //从1到5被炸弹伤害为2
6 9 2   //从6到9的路段被炸弹伤害为2(伤害有累加效果)
-1 -1 -1  //-1代表一组测试数据结束

Sample Output
1 9     //分别输出从左边和从右边算起伤害最大的第一个点

View Code
 1 #include <iostream>
2 #include<stdio.h>
3 #define MAX 15010
4 using namespace std;
5 struct node
6 {
7 int left,right;
8 int num;
9 node()
10 {
11 num=0;
12 }
13 };
14 node tree[4*MAX];
15 void build(int left,int right,int i)
16 {
17 tree[i].left=left;tree[i].right=right;
18 if (left<right)
19 {
20 int mid=(left+right)>>1;
21 build(left,mid,2*i);
22 build(mid+1,right,2*i+1);
23 }
24 }
25 void insert(int i,int left,int right,int num)
26 {
27 if(left<=tree[i].left&&tree[i].right<=right)
28 {
29 tree[i].num+=num;
30 return;
31 }
32 if (tree[i].left<tree[i].right)
33 {
34 int mid=(tree[i].left+tree[i].right)>>1;
35 if(right<=mid)
36 insert(2*i,left,right,num);
37 else if (left>mid)
38 insert(2*i+1,left,right,num);
39 else
40 {
41 insert(2*i,left,mid,num);
42 insert(2*i+1,mid+1,right,num);
43 }
44 }
45 }
46 int sum;
47 void finds(int k,int i)
48 {
49 if (tree[i].left<=k&&k<=tree[i].right)
50 {
51 sum+=tree[i].num;
52 }
53 if(tree[i].left<tree[i].right)
54 {
55 int mid=(tree[i].left+tree[i].right)>>1;
56 if(k<=mid)
57 finds(k,2*i);
58 else
59 finds(k,2*i+1);
60 }
61 }
62 int main()
63 {
64 int n,i,num,left,right,max,north,south;
65 while(cin>>n)
66 {
67 build(1,n+1,1);
68 for(i=0;i<=4*n;i++)
69 tree[i].num=0;
70 while(scanf("%d%d%d",&left,&right,&num)&&left!=-1)
71 {
72 insert(1,left+1,right+1,num);
73 }
74 max=0;
75 for(i=1;i<=n+1;i++)
76 {
77 sum=0;
78 finds(i,1);
79 if(max<sum)
80 {
81 north=i;
82 max=sum;
83 }
84 if(max<=sum)
85 {
86 south=i;
87 }
88 }
89 printf("%d %d\n",north-1,south-1);
90 }
91 return 0;
92 }
原文地址:https://www.cnblogs.com/qijinbiao/p/2368066.html