POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 53721 | Accepted: 25244 | |
Case Time Limit: 2000MS |
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
题意就是在一堆数里找一定范围里的最大值和最小值,计算差值。
因为知道这个题肯定不能瞎写就写对,所以还很认真的写了一个代码,真是智障,不管怎么写,反正T了。
线段树,不会用,瞎写。。。
线段树大法好,可惜智障,改了12遍(此刻内心¥…&%……*(*&),出现各种问题嘛,存最小值要再有东西存人家啊。。。
最后实在改不出来了,求助了大佬,最后问题还是出在最小值问题上,要求最小值,一开始比较的ans就要比数组里的最大数要大啊。。。要审清题和题意。。。
线段树!!!
代码(垃圾写的乱七八糟):
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cstdlib> #include<string.h> #include<set> #include<vector> #include<queue> #include<stack> #include<map> #include<cmath> using namespace std; #define N 300000 struct node{ int left,right,maxx,minn; }tree[N*4]; int n,m,minn,maxx; void build(int l,int r,int pos){ tree[pos].left=l; tree[pos].right=r; tree[pos].maxx=0; tree[pos].minn=1e9; if(tree[pos].left==tree[pos].right)return; int mid=(l+r)>>1; build(l,mid,pos*2); build(mid+1,r,pos*2+1); } void update(int x,int y,int pos){ if(tree[pos].left==x&&tree[pos].right==x){ tree[pos].maxx=y; tree[pos].minn=y; return; } int mid=(tree[pos].left+tree[pos].right)>>1; if(x>mid) update(x,y,pos*2+1); else update(x,y,pos*2); tree[pos].maxx=max(tree[pos*2].maxx,tree[pos*2+1].maxx); tree[pos].minn=min(tree[pos*2].minn,tree[pos*2+1].minn); } void query(int x,int y,int pos){ if(tree[pos].left==x&&tree[pos].right==y){ maxx=max(maxx,tree[pos].maxx); minn=min(minn,tree[pos].minn); return ; } int mid=(tree[pos].left+tree[pos].right)>>1; if(y<=mid)query(x,y,pos*2); else if(x>mid)query(x,y,pos*2+1); else{ query(x,mid,pos*2); query(mid+1,y,pos*2+1); } } int main(){ int m,n; int ans1,ans2; while(~scanf("%d%d",&n,&m)){ getchar(); build(1,n,1); int x; for(int i=1;i<=n;i++){ scanf("%d",&x); update(i,x,1); } int a,b; while(m--){ scanf("%d%d",&a,&b); maxx=0; minn=1e9; query(a,b,1); printf("%d\n",maxx-minn); } } return 0; }
(╯°Д°)╯︵┻━┻
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?