LA 3938 线段树(区间合并)
来源:互联网 发布:魔鬼数学 大数据 pdf 编辑:程序博客网 时间:2024/05/22 12:14
After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long ... , the dishes are represented by 1, 2, 3 ... ). ``You make me work hard and don't pay me! You refuse to teach me Latin Dance! Now it is time for you to serve me", Neal says to himself.
Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many questions about the values of dishes he would have.
For each question Neal asks, he will first write down an interval [a, b] (inclusive) to represent all the dishes a, a + 1,..., b , where a and b are positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.
Input
The input file contains multiple test cases. For each test case, there are two integers n and m in the first line (n, m < 500000) . n is the number of dishes and m is the number of questions Neal asks.
Then n numbers come in the second line, which are the values of the dishes from left to right. Next m lines are the questions and each line contains two numbers a , b as described above. Proceed to the end of the input file.
Output
For each test case, output m lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.
Sample Input
3 1 1 2 3 1 1
Sample Output
Case 1: 1 1
有bug。D了半天没搞出来、改天再来更改。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 500080#define lson id<<1,l,mid#define rson id<<1|1,mid+1,r#define lson id<<1,l,mid#define rson id<<1|1,mid+1,r#define LL long long intLL key[maxn];int s,t;struct ST{int l,r,a,b,left,right;LL maxsum,sum,lsum,rsum;}st[maxn<<2];LL max (LL a,LL b){return a>b?a:b;}void PushUp(int id){st[id].left = st[id<<1].left;st[id].right = st[id<<1|1].right;st[id].lsum = st[id<<1].lsum;st[id].rsum = st[id<<1|1].rsum;st[id].sum = st[id<<1].sum + st[id<<1|1].sum;LL ans1 = st[id<<1].maxsum,ans2 = st[id<<1|1].maxsum;if(ans1 >= ans2){st[id].maxsum = st[id<<1].maxsum;st[id].a = st[id<<1].a;st[id].b = st[id<<1].b;}else {st[id].maxsum = st[id<<1|1].maxsum;st[id].a = st[id<<1|1].a;st[id].b = st[id<<1|1].b;}//lsum,rsum,maxsum,a,b,left,right都有可能更改//lsumif(st[id].lsum == st[id<<1].sum && st[id<<1|1].lsum > 0){st[id].lsum = st[id].lsum + st[id<<1|1].lsum;st[id].left = st[id<<1|1].left;}//rsumif(st[id].rsum == st[id<<1|1].sum && st[id<<1].rsum >= 0){st[id].rsum = st[id].rsum + st[id<<1].rsum;st[id].right = st[id<<1].right;}//maxsumif((st[id<<1].rsum + st[id<<1|1].lsum > st[id].maxsum) || (st[id<<1].right < st[id].a && st[id<<1].rsum + st[id<<1|1].lsum == st[id].maxsum)){st[id].maxsum = st[id<<1].rsum + st[id<<1|1].lsum;st[id].a = st[id<<1].right;st[id].b = st[id<<1|1].left;}}void buildtree(int id,int l,int r){st[id].l = l,st[id].r = r;if(l == r){st[id].sum = st[id].lsum = st[id].rsum = st[id].maxsum = key[l];st[id].left = st[id].right = st[id].a = st[id].b = l;return;}int mid = (l + r) >> 1;buildtree(lson);buildtree(rson);PushUp(id);}LL query(int id,int l,int r){if(st[id].l == l && st[id].r == r){s = st[id].a;t = st[id].b;return st[id].maxsum;}if(st[id<<1].r >= r){return query(id<<1,l,r);}else if(st[id<<1|1].l <= l){return query(id<<1|1,l,r);}else {LL ans1 = query(id<<1,l,st[id<<1].r);LL ans2 = query(id<<1|1,st[id<<1|1].l,r);if(ans1 >= ans2)query(id<<1,l,st[id<<1].r);LL ans = max(ans1,ans2);if((st[id<<1].rsum + st[id<<1|1].lsum > ans) || (st[id<<1].rsum + st[id<<1|1].lsum == ans && st[id<<1].right < s)){s = st[id<<1].right;t = st[id<<1|1].left;}ans = max(ans,st[id<<1].rsum + st[id<<1|1].lsum);return ans;}}int main(){//freopen("in.txt","r",stdin);int n,m,cas = 0;while(scanf("%d%d",&n,&m)==2){cas++;for(int i = 1;i <= n;i++)scanf("%lld",&key[i]);buildtree(1,1,n);printf("Case %d:\n",cas);while(m--){int u,v;scanf("%d%d",&u,&v);query(1,u,v);printf("%d %d\n",s,t);}}return 0;}回过头来看。终于把它A了。。热泪盈眶。。。线段树维护的信息:a表示最小x。b表示最小y。left表示最大前缀和的终点,right表示最大后缀和的起点lsum,rsum,maxsum表示最大前缀和,最大后缀和,最大连续和。此题query函数得写三个~~返回类型用结构体~~跪烂了!!#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 500080#define lson id<<1,l,mid#define rson id<<1|1,mid+1,r#define lson id<<1,l,mid#define rson id<<1|1,mid+1,r#define LL long long intLL key[maxn],Sum[maxn];//前缀和数组struct ST{int l,r,a,b,left,right;LL maxsum,sum,lsum,rsum;}st[maxn<<2];struct Node{int l,r;LL sum;};LL max (LL a,LL b){return a>b?a:b;}void PushUp(int id){st[id].left = st[id<<1].left;st[id].right = st[id<<1|1].right;st[id].lsum = st[id<<1].lsum;st[id].rsum = st[id<<1|1].rsum;st[id].sum = st[id<<1].sum + st[id<<1|1].sum;LL ans1 = st[id<<1].maxsum,ans2 = st[id<<1|1].maxsum;if(ans1 >= ans2){st[id].maxsum = st[id<<1].maxsum;st[id].a = st[id<<1].a;st[id].b = st[id<<1].b;}else {st[id].maxsum = st[id<<1|1].maxsum;st[id].a = st[id<<1|1].a;st[id].b = st[id<<1|1].b;}//lsum,rsum,maxsum,a,b,left,right都有可能更改//lsumif(st[id<<1].sum + st[id<<1|1].lsum > st[id<<1].lsum){st[id].lsum = st[id<<1].sum + st[id<<1|1].lsum;st[id].left = st[id<<1|1].left;}//rsumif(st[id<<1|1].sum + st[id<<1].rsum >= st[id<<1|1].rsum){st[id].rsum = st[id<<1].rsum + st[id<<1|1].sum;st[id].right = st[id<<1].right;}//maxsumif((st[id<<1].rsum + st[id<<1|1].lsum > st[id].maxsum) || (st[id<<1].right < st[id].a && st[id<<1].rsum + st[id<<1|1].lsum == st[id].maxsum)){st[id].maxsum = st[id<<1].rsum + st[id<<1|1].lsum;st[id].a = st[id<<1].right;st[id].b = st[id<<1|1].left;}}void buildtree(int id,int l,int r){st[id].l = l,st[id].r = r;if(l == r){st[id].sum = st[id].lsum = st[id].rsum = st[id].maxsum = key[l];st[id].left = st[id].right = st[id].a = st[id].b = l;return;}int mid = (l + r) >> 1;buildtree(lson);buildtree(rson);PushUp(id);}Node query_suffix(int id,int l,int r){if(st[id].l == l && st[id].r == r){Node a;a.l = st[id].right;;a.r = r;a.sum = st[id].rsum;return a;}if(st[id<<1].r >= r){return query_suffix(id<<1,l,r);}else if(st[id<<1|1].l <= l){return query_suffix(id<<1|1,l,r);}Node a = query_suffix(id<<1,l,st[id<<1].r);Node b = query_suffix(id<<1|1,st[id<<1|1].l,r);Node ans;ans.r = r;if(a.sum + Sum[r] - Sum[st[id<<1].r] >= b.sum){ans.sum = a.sum + Sum[r] - Sum[st[id<<1].r];ans.l = a.l;}else {ans.sum = b.sum;ans.l = b.l;}return ans;}Node query_pre(int id,int l,int r){if(st[id].l == l && st[id].r == r){Node a;a.l = l;a.r = st[id].left;a.sum = st[id].lsum;return a;}if(st[id<<1].r >= r){return query_pre(id<<1,l,r);}else if(st[id<<1|1].l <= l){return query_pre(id<<1|1,l,r);}Node a = query_pre(id<<1,l,st[id<<1].r);Node b = query_pre(id<<1|1,st[id<<1|1].l,r);Node ans;ans.l = l;if(st[id<<1].sum + b.sum > a.sum){ans.sum = st[id<<1].sum + b.sum;ans.r = b.r;}else {ans.sum = a.sum;ans.r = a.r;}return ans;}Node query(int id,int l,int r){if(st[id].l == l && st[id].r == r){Node a;a.l = st[id].a;a.r = st[id].b;a.sum = st[id].maxsum;return a;}if(st[id<<1].r >= r){return query(id<<1,l,r);}else if(st[id<<1|1].l <= l){return query(id<<1|1,l,r);}else {Node ans1 = query(id<<1,l,st[id<<1].r);Node ans2 = query(id<<1|1,st[id<<1|1].l,r);Node a = query_suffix(id<<1,l,st[id<<1].r);Node b = query_pre(id<<1|1,st[id<<1|1].l,r);Node ans;if(ans1.sum >= ans2.sum){ans.sum = ans1.sum;ans.l = ans1.l;ans.r = ans1.r;}else {ans.sum = ans2.sum;ans.l = ans2.l;ans.r = ans2.r;}if(a.sum + b.sum > ans.sum || (a.sum + b.sum == ans.sum && a.l < ans.l)){ans.sum = a.sum + b.sum;ans.l = a.l;ans.r = b.r;}return ans;}}int main(){//freopen("in.txt","r",stdin);int n,m,cas = 0;while(scanf("%d%d",&n,&m)==2){cas++;Sum[0] = 0;for(int i = 1;i <= n;i++){scanf("%lld",&key[i]);Sum[i] = Sum[i-1] + key[i];}buildtree(1,1,n);printf("Case %d:\n",cas);while(m--){int u,v;scanf("%d%d",&u,&v);Node a = query(1,u,v);printf("%d %d\n",a.l,a.r);}}return 0;}0 0
- LA 3938 线段树(区间合并)
- LA 3938 线段树区间更新
- 线段树 (区间合并)
- 线段树 区间合并
- 线段树 区间合并
- 线段树 区间合并
- 线段树 区间合并
- 线段树-区间合并
- poj3667 线段树(区间合并)
- hdu3308(线段树,区间合并)
- poj3667(线段树,区间合并)
- poj3667 Hotel (线段树区间合并)
- POJ-3667 线段树(区间合并)
- poj3667 (线段树区间合并)
- HDU5316 Magician (线段树区间合并)
- hdu3397(线段树区间合并)
- hdu3308(线段树,区间合并)
- hdu1540(线段树区间合并)
- 萌版胡一菲
- 从哪里为业余项目挤出时间?
- 三星 I9000/Galaxy S 港行2.3.3官方ROM刷内核ROOT教程
- GDB的使用方法(推荐)
- NameNode和DataNode通信机制
- LA 3938 线段树(区间合并)
- Android内存泄漏分析及调试
- ELF文件
- OJ_1086 最小花费
- win7先安装oracle 10g时,安装检测到系统的主 IP 地址是 DHCP 分配的地址
- 自己在linux上编译、链接、动态库和静态库的学习笔记
- 用 Ruby 求定积分
- 如何使用KeyChain保存和获取UDID
- 岁的法国兰蔻经典回顾的房管局三等功