[RMQ] [线段树] POJ 3368 Frequent Values】的更多相关文章

一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同的一段 (RMQ) 但是要注意 to[i] 大于查询范围的情况, 以及RMQ时 x < y 的情况!! AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib>…
                                                     Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15229   Accepted: 5550 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In…
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23016   Accepted: 8060 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that,…
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是数字1,它的频数3. 思路:假设查询时的参数为a, b.这道题查询时有以下两种情况: 1. num[a] = num[b]. 即区间内的数字全相同,此时答案为b - a + 1. 2. 如果不相同,则以一般情况来讨论.见下图. 因为序列为非递减序列,因此值相同的数字必然连续出现.将区间分为3部分.n…
/************************************************************ 题目: Frequent values(poj 3368) 链接: http://poj.org/problem?id=3368 题意: 给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之 间连续出现次数最多的次数 算法: RMQ 思路: 借助数组f[i].表示第i位前面有f[i]个相同的数.对于 每个区间(l,r).暴力求前面几个相同的数.然后在用RMQ 求后面…
Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 Description You are given a sequence of n integersa1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consi…
Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries cons…
Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16537   Accepted: 5981 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indice…
/* 线段树区间合并 维护几个信息 到时候乱搞一下就好了 开始T了 有一种情况可以不用递归 直接算出来 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100010 #define lc (k<<1) #define rc (k<<1)+1 #define mid ((l+r)>>1) using namespace std; ],rs[max…
[题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的答案 每次线段合并的时候,对区间数据进行合并即可. [代码] #include <cstdio> #include <algorithm> using namespace std; const int N=100010; struct data{int a,b,l,r,val;}T[N…
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, dete…
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重新分段,并标记相同的个数为 该段的数值,然后RMQ... #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm>…
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among t…
<题目链接> 题目大意: 给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次. 解题分析: 因为该序列是非降序的,所以该序列中的所有相等的数都是连续的,因此,我们可以先用dp预处理一下,dp[i]表示以第i个数结尾的最大连续相等个数,于是,本题目就变成了,在指定区间内,dp[i]的最大的值是多少. #include <cstdio> #include <cstring> #include <cmath&g…
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int y) { return x>y?x:y; } int a[maxn]; int left[maxn]; int right[maxn]; int num[maxn]; ]; void init() { memset(a,,sizeof a); memset(left,-,sizeof left); me…
题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的,也就是相同的数会连续不间断地在一起,于是就才有了代码中这个部分来预判了: if (s > t)        printf("%d\n", ans); 这个人写RMQ 写得不错:http://dongxicheng.org/structure/lca-rmq/ 这题就是套模板的,纪念…
NBU 2475Survivors 题目链接:http://acm.nbu.edu.cn/v1.0/Problems/Problem.php?pid=2475 题意:给定n个人,每个人有strength,dexterity,intelligence3个属性! 如果在这n个人当中存在某个人3项属性都不比他小,这个人则淘汰!否则生存下来,求最后生存下来的人数! 思路:按3个属性优先从大到小排序!那么对于当前幸存者i,因为之前的[1,i-1]幸存者的strength都比他大,如果存在dexterity…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次数最多的数的个数: 如下图所示: AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> #include<…
昨天写的博客删了,占坑失败,还是先把RMQ玩的6一点再去搞后面的东西.废话少说,题解题姐姐_(:з」∠)_      Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20960   Accepted: 7403 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing ord…
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小时,只需遍历一遍就可以,数据量一大时就容易时间超限,RMQ算法是一种高效算法,和线段树差不多(当没有数据的实时更新时),当然两者都需要预处理. 定义映射f(i,j)=x,即以i为起点,长度为2j 区间内的最大最小值,显而易见f(i,0)为该数本身,那么求f(i,j+1)时: 可得公式 f(i,j)=…
题目链接: http://poj.org/problem?id=2763 题意: 第一行输入 n, q, s 分别为树的顶点个数, 询问/修改个数, 初始位置. 接下来 n - 1 行形如 x, y, w 的输入为点 x, y 之间连边且边权为 w. 接下来 q 行输入, 若输入形式为 1 x y 则为将点 x 的权值修改为 y , 若输入形式为 0 x 则询问 s 到 x 的最短距离为多少. 上一组的 x 为下一组的 s. 思路: 若去掉修改边权部分, 则为一个 lca 模板题. 对于修改边权…
题意:n种物品,每种物品对不同的人都有不同的价值,有三个人选,第一个为普通学生,第二个是集,第三个是祈,集和祈可以选一样的,并且还会获得加分,集和祈选的普通学生都不能选,问三个人怎样选才能使总分最高. 解法: 先把集和祈选一样的和存到一个数组sum,然后可以枚举普通学生选的是哪个,再在sum的左边和右边找一个最大值,更新Maxi,然后再考虑集祈选的不同的情况,即在集的数组两边取个最大值,以及在祈的数组两边取个最大值,相加即可,如果集的最大值和祈的最大值为一个标记时,我们在前面的sum最大值就已经…
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 5077 Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silho…
题目链接 /* 单点更新,用RMQ维护最大值,add对c[i]修改,或加,或减. 求[l,r]的和,用sum(r)-sum(l-1).即可. */ #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn =500005; int a[maxn]; int c[…
Description You have N integers, A1, A2, ... ,AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. Inp…
题目链接 \(Description\) 给定一棵树.每次询问给定\(a\sim b,c\sim d\)两个下标区间,从这两个区间中各取一个点,使得这两个点距离最远.输出最远距离. \(n,q\leq10^5\). \(Solution\) 一个集合直径的两端点,在被划分为两个集合后一定是两个集合直径的四个端点中的两个. 即假设将\(S\)分为两个集合后,另外两个集合的直径的两端点分别为a,b和c,d,那么\(S\)集合的直径的两端点一定是a,b,c,d中的两个. 证明类似树的直径. 所以信息可…
https://www.luogu.org/problemnew/show/P3379 1.欧拉序+rmq(st) /* 在这里,对于一个数,选择最左边的 选择任意一个都可以,[left_index,right_index],深度都大于等于这个数的深度 */ #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <time.h> #inc…
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但是代码就是过不去..这次应该是无缘复赛了.. 先不水了,省赛回来,我看了一下这个题,当时有个类似于快排的想法,今天试了一下,勉强AC了..跑了3S多. 思路就是我枚举区间左值lt,那么[lt, n]区间内最值的角标分别为mi和ma.于是设to = max(mi, ma).也就是说在to右侧的所有区间…
题目链接: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a continuous subsequence of the given sequence and its…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的数字,可以得到最小值,在这个基础下,又继续删除最后一位递增的数字,得到的依然是最小值.这就表明当前这步的贪心不仅是当前最优,而且对于下一步贪心来说也是最优的.所以每次删除最后递增项就可以了. 初期代码(每次循环找最后递增项): Accepted 3183 46MS 1408K 1259 B G++…