ZOJ 3573 Under Attack (线段树版)
2013-11-11 21:29
357 查看
超级水的线段树,主要是题目时间太宽松了,不然这题应该还是比较有难度的。。
第一次写结构体版的线段树,呵呵
第一次写结构体版的线段树,呵呵
#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.l=l; tree .r=r; tree .d=0; if(l==r) return ; int m=(tree .l+tree .r)/2; Build(tree .l,m,th*2); Build(m+1,tree .r,th*2+1); } void UpDate(int L,int R,int v,int th){ if(L<=tree .l&&tree .r<=R){ tree .d+=v; return ; } int m=(tree .l+tree .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 .d; if(tree .l==i&&tree .r==i) return ; int m=(tree .l+tree .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); } } 相关文章推荐
- 比较两个文件是否相同(C/C++语言)
- Killing Defunct Operating System Process (Doc ID 298048.1)
- 黑马程序员-String常量池
- 第十二周项目1-用函数输出日期是本年的第几天
- 将文件夹下的所有图片存入数据库和下载到本地
- Selenium私房菜系列3 -- Selenium API参考手册【ZZ】
- C++ extern "C"
- C语言 - 判断键盘输入的类型 并且计数
- C++任务队列与多线程
- PHP你可能也会掉入的坑(二)
- Selenium私房菜系列2 -- XPath的使用【ZZ】
- linux进程间通信(IPC)--共享内存
- 和java谈恋爱:本色的她 (抽象类、接口) ---尚硅谷 -课程笔记
- 二叉堆小结
- c#练习
- 第六章的牛叉程序
- ORACLE in exists 比较
- UITableView 显示在statusbar 下面
- 23款免费web图标任你选择
- 新人的第一次