poj 3264(线段树)】的更多相关文章

题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,000 设计每次查询的复杂度为logm. 例如, 输入: 6 3 1 7 3 4 2 5 1 5 4 6 2 2 输出: 6 3 0     分析:这道题是典型的空间换时间的题.一看到是区间的查找,我们应该想到线段树,这里有一篇我觉得写得挺好的介绍线段树的博客和大家分享一下:http://www.cnb…
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 rang…
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点,不断维护,最好每次询问维护一个询问区间的最大值和最小值,最后相减即可.其实就相当于,线段树找区间的最大值和最小值. #include<iostream> #include<stdio.h> #include<algorithm> #include<string.h&…
题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值) 代码: #include<stdio.h> #include<algorithm> #define inf 0x3f3f3f3f using namespace std; ]; int ansmax, ansmin; struct Tree { int maxx, minn, le…
题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 多次求任一区间Ai-Aj中最大数和最小数的差 线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天一题,风格用kuangbin大神和以前的,两种都写一遍 2015-05-18:擦,太水了,当年居然这种题都做不出来 RMQ做法:poj3264 #include<cstdio> #include<iostream> #include<algorithm> #include&…
题意:给你一个数列,从中挑一段,问你这段数的最大值减最小值是多少. 思路:线段树. // by Sirius_Ren #include <cstdio> #include <algorithm> #define N 50000 using namespace std; int n,q,xx,yy,tree[N*4],MAX[N*4],MIN[N*4],ansmax,ansmin; void build(int l,int r,int pos){ if(l==r){scanf(&qu…
题意 给出一个序列  每次查询区间的max-min是多少 思路:直接维护max 和min即可  写两个query分别查最大最小值 #include<cstdio> #include<algorithm> #include<set> #include<vector> #include<cstring> #include<iostream> using namespace std; ; int a[maxn]; struct Node{…
Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Accepted: 3968 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise…
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线段树的题目,就是线段树的用区间更新就可以,我也是第一次用.. #include <stdio.h> #include <string.h> #define maxn 100050 long long arra[ maxn ]; struct note{ //要用long long 类型…
Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue… The Lunar New Year wa…