Under Attack









Time Limit:  10 Seconds      Memory Limit:  65536 KB 









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

1 5 2

6 9 2

-1 -1 -1









Sample Output

1 9









从早上起 这都一上午了,这个大水题最终a了!

在不知道 最大覆盖次数求法之前,我先求出全长线段中最大值,也就是普通的区间更新加延迟标记。然后利用calculate函数从两边分别開始遍历找到左右最大值的位置。从多组測试数据上来看 。并没有什么差错,但就是wrong,后学会最大覆盖次数。1a。

见到的请帮我看看究竟是哪些 数据错了!!!!

。!



第二段代码是正确地。

wrong code:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF=15003;
struct Tree
{
int left;
int right;
int mark;
int Max;
} tree[INF<<2]; int create(int root,int left,int right)
{
tree[root].left=left;
tree[root].right=right;
if(left==right)
{
return tree[root].Max=0;
}
int a,b,middle=(left+right)>>1;
a=create(root<<1,left,middle);
b=create(root<<1|1,middle+1,right);
return tree[root].Max=max(a,b);
} void update_mark(int root)
{
if(tree[root].mark)
{
tree[root].Max+=tree[root].mark;
if(tree[root].left!=tree[root].right)
{
tree[root<<1].mark+=tree[root].mark;
tree[root<<1|1].mark+=tree[root].mark;
}
tree[root].mark=0;
}
} int calculate(int root,int left ,int right)
{
update_mark(root);
if(tree[root].left>right||tree[root].right<left)
return 0;
if(tree[root].left>=left&&tree[root].right<=right)
{
return tree[root].Max;
}
int a,b;
a=calculate(root<<1,left,right);
b=calculate(root<<1|1,left,right);
return max(a,b);
} int update(int root,int left,int right,int val)
{
update_mark(root);
if(tree[root].left>right||tree[root].right<left)
return tree[root].Max;
if(tree[root].left>=left&&tree[root].right<=right)
{
tree[root].mark+=val;
update_mark(root);
return tree[root].Max;
}
int a=update(root<<1,left,right,val);
int b=update(root<<1|1,left,right,val);
return tree[root].Max=max(a,b); } int main()
{
int L;
while(scanf("%d",&L)!=EOF)
{
create(1,0,L);
int x,y,z;
while(scanf("%d%d%d",&x,&y,&z)!=EOF)
{
if(x>y)
swap(x,y);
if(x!=-1)
{
update(1,x,y,z);
}
else break;
}
int k=calculate(1,0,L);
int locl,locr;
for(int i=0; i<=L;i++)
{
if(calculate(1,i,i)==k)
{ locl=i;
break;
}
}
for(int i=L;i>=0;i--)
{
if(calculate(1,i,i)==k)
{
locr=i;
break;
}
}
printf("%d,%d\n",locl,locr);
}
return 0;
}
/*
10
1 2 3
0 0 3
5 8 4
0 0 3
2 2 2
-1 1 3
*/
</pre><pre name="code" class="cpp">正确 代码:
<pre name="code" class="cpp">#include<stdio.h>
struct Tree
{
int left,right,cover;
} tree[15000<<2];
int covered=0;
void create(int root,int left,int right)
{
tree[root].left=left;
tree[root].right=right;
tree[root].cover=0;
if(right==left)
return ;
int mid=(left+right)>>1;
create(root<<1,left,mid);
create(root<<1|1,mid+1,right);
} void update(int root,int left,int right,int val)
{
if(left<=tree[root].left&&tree[root].right<=right)
{
tree[root].cover+=val;
return ;
}
int m=(tree[root].left+tree[root].right)>>1;
if(m>=left)update(root<<1,left,right,val);
if(m<right)update(root<<1|1,left,right,val);
} void calculate(int root,int x)
{
covered+=tree[root].cover;
if(tree[root].left==tree[root].right)
return ;
int m=(tree[root].left+tree[root].right)>>1;
if(m>=x)
calculate(root<<1,x);
else
calculate(root<<1|1,x); } int main()
{
int L;
while(scanf("%d",&L)!=EOF)
{
create(1,0,L);
int x,y,z;
while(scanf("%d%d%d",&x,&y,&z))
{
if(x==-1)
break;
update(1,x,y,z);
}
int loc1, loc2,Max=0;
for(int i=0; i<=L; i++)
{
covered=0;
calculate(1,i);
if(covered>Max)
{
Max=covered;
loc1=i;
}
}
for(int i=L,Max=0; i>=0; i--)
{
covered=0;
calculate(1,i);
if(covered>Max)
{
Max=covered;
loc2=i;
}
}
printf("%d %d\n",loc1,loc2);
}
return 0;
}

zoj 3573 Under Attack(线段树 标记法 最大覆盖数)的更多相关文章

  1. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  2. hdu 4578 线段树(标记处理)

    Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others) ...

  3. hdu 3954 线段树 (标记)

    Level up Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. BZOJ4785 [Zjoi2017]树状数组 【二维线段树 + 标记永久化】

    题目链接 BZOJ4785 题解 肝了一个下午QAQ没写过二维线段树还是很难受 首先题目中的树状数组实际维护的是后缀和,这一点凭分析或经验或手模观察可以得出 在\(\mod 2\)意义下,我们实际求出 ...

  5. Codeforces 258E - Little Elephant and Tree(根号暴力/线段树+标记永久化/主席树+标记永久化/普通线段树/可撤销线段树,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 yyq:"hot tea 不常有,做过了就不能再错过了" 似乎这是半年前某场 hb 模拟赛的 T2?当时 ycx.ym ...

  6. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

  7. hdu 4031 Attack 线段树

    题目链接 Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total ...

  8. P3332 [ZJOI2013]K大数查询(线段树套线段树+标记永久化)

    P3332 [ZJOI2013]K大数查询 权值线段树套区间线段树 把插入的值离散化一下开个线段树 蓝后每个节点开个线段树,维护一下每个数出现的区间和次数 为了防止MLE动态开点就好辣 重点是标记永久 ...

  9. 洛谷P3437 [POI2006]TET-Tetris 3D(二维线段树 标记永久化)

    题意 题目链接 Sol 二维线段树空间复杂度是多少啊qwqqq 为啥这题全网空间都是\(n^2\)还有人硬要说是\(nlog^2n\)呀.. 对于这题来说,因为有修改操作,我们需要在外层线段树上也打标 ...

随机推荐

  1. bash文件名统配

    bash基础特性之globbing,即文件名通配:     文件名通配:使用元字符匹配字符         *:匹配任意长度的任意字符             假如文件名为paaaa,则pa*,*pa ...

  2. Python开发:模块

    在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法,把这些定义存放在文 ...

  3. luogu3629 [APIO2010]巡逻

    创造一个环出来,可以让环上的边都只访问一次. 对于 \(k=1\),答案就是树的直径两边连起来. 倘若 \(k=2\),那就先按照 \(k=1\) 的求一遍,然后我们发现,如果第二条加的边构成的环和第 ...

  4. [工具使用] visualvm 通过jmx不能连接

    远程服务器,通常配置下jmx,然后用visualvm连接然后监控. 但昨天自己的一台测试服务器上,正确配置了jmx还是不能连接上去. 后来参考了 https://bjddd192.github.io/ ...

  5. 【总集】C++ STL类库 vector 使用方法

    介绍: 1.vector 的中文名为向量,可以理解为一个序列容器,里面存放的是相同的数据结构类型,类似于数组但与数组又有微妙的不同. 2.vector 采用的是连续动态的空间来存储数据,它是动态的数组 ...

  6. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  7. scp命令(基于ssh上传文件等)

    (转:http://www.cnblogs.com/hitwtx/archive/2011/11/16/2251254.html) svn 删除所有的 .svn文件 find . -name .svn ...

  8. 【Luogu】P2015二叉苹果树(DP,DFS)

    题目链接 设f[i][j][k]表示给以i为根节点的子树分配j条可保留的树枝名额的时候,状态为k时能保留的最多苹果. k有三种情况. k=1:我只考虑子树的左叉,不考虑子树的右叉,此时子树能保留的最多 ...

  9. Oracle学习笔记整理手册

    文章目录(1)Oracle正则匹配使用(2)Oracle修改有数据的数据字段类型(3)Oracle表数据回滚语句(4)sql筛选出记录数大于2的记录(5)oracle同义词(6)oracle内外连接( ...

  10. POJ 1860: Currency Exchange 【SPFA】

    套汇问题,从源点做SPFA,如果有一个点入队次数大于v次(v表示点的个数)则图中存在负权回路,能够套汇,如果不存在负权回路,则判断下源点到自身的最长路是否大于自身,使用SPFA时松弛操作需要做调整 # ...