POJ3368(Frequent values)--线段树
3368 | Accepted | 7312K | 1829MS | C++ | 6936B |
题意为给你一组数据,再给定一组区间,问你这个区间内出现次数最多的元素的次数是多少。
我还记得这题是学校校赛基础的题目,当时懵懵懂懂的用分治交了6次TLE。知道了线段树之后才后悔每更早的认识她。
一段区间内的多次出现的数的次数,在线段树查询中有以下几种情况
1.次数最多的都集中在某一结点的左区间内
2.次数最多的都集中在某一结点的有区间内
3.次数最多的在左右两边都有,这时maxCount ==左右两边的maxCount之和
在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新nSum(加上本次增量),再将增量往下传。这样更新的复杂度就是O(log(n))在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到nSum上后将Inc清0,接下来再往下查询。
- /*************************************************************************
- > File Name: poj3368rmq.cpp
- > Author: YeGuoSheng
- > Description:
- > Created Time: 2019年07月11日 星期四 15时34分56秒
- ************************************************************************/
- #include<iostream>
- #include<stdio.h>
- #include<cstring>
- #include<cmath>
- #include<vector>
- #include<stack>
- #include<map>
- #include<set>
- #include<list>
- #include<queue>
- #include<string>
- #include<algorithm>
- #include<iomanip>
- using namespace std;
- template <typename T>
- struct node
- {
- int l;
- int r;//左右区间端点
- T value;
- T add;
- int lCount;//元素在左边的计数
- int rCount;//元素在右边的计数
- int maxCount;//总计出现的次数
- int Len()const;
- int Mid()const;
- };
- template <typename T>
- int node<T>::Mid()const
- {
- return (r + l )/ ;
- }
- template<typename T>
- int node<T>::Len()const
- {
- return (r - l ) +;
- }
- template<typename T>
- class IntervalTree
- {
- protected:
- int n;
- node<T>*tree;
- public:
- IntervalTree(int n);
- ~IntervalTree();
- void BuildTree(int v,int l,int r);
- void Add(int v,int l,int r,T m);
- T Query(int v,int l,int r);
- void Insert(int v,int i,T value);
- void BuildCount(int v,int l,int r);
- int FindIndexInTree(int v,int y);//在数组下标为u的在树中的下标
- };
- template<typename T>
- IntervalTree<T>::IntervalTree(int n)
- {
- this->n = n;
- tree = new node<T>[*n];
- }
- template<typename T>
- IntervalTree<T>::~IntervalTree()
- {
- delete []tree;
- }
- template<typename T>
- void IntervalTree<T>::BuildTree(int v ,int l,int r)
- {
- tree[v].l =l;
- tree[v].r = r;
- tree[v].add = ;
- tree[v].value = ;
- if(l == r)//相等
- {
- tree[v].lCount =tree[v].rCount = tree[v].maxCount = ;
- return ;
- }
- int mid =(l + r) /;//二分
- BuildTree(v* +,l,mid);
- BuildTree(v*+,mid+,r);
- tree[v].value = tree[*v+].value + tree[*v+].value;
- }
- template<typename T>
- int IntervalTree<T>::FindIndexInTree(int v,int u)
- {
- if( tree[v].l == u && tree[v].r == u)
- {
- return v;
- }
- else
- {
- int mid = (tree[v].l + tree[v].r) / ;
- if(u <= mid)
- {
- FindIndexInTree(v * +,u);
- }
- else
- {
- FindIndexInTree(v * +,u);
- }
- }
- }
- template<typename T>
- void IntervalTree<T>::BuildCount(int v,int l ,int r)
- {
- tree[v].l = l;
- tree[v].r = r;
- if(l == r)
- {
- tree[v].lCount = tree[v].rCount =tree[v].maxCount = ;
- return ;
- }
- int mid = (l + r )/ ;
- BuildCount(* v+ ,l,mid);
- BuildCount(*v +,mid+,r);
- int repeat = ;
- int leftIndex = FindIndexInTree(v,tree[*v+].r);
- int rightIndex = FindIndexInTree(v,tree[*v+].l);
- if(tree[leftIndex].value == tree[rightIndex].value)
- {
- repeat = tree[ * v +].rCount + tree[*v +].lCount;
- }
- else
- {
- repeat = ;
- }
- tree[v].maxCount = max( repeat,max( tree[*v+].maxCount, tree[*v+].maxCount) );
- tree[v].lCount = tree[*v+].lCount;
- if(tree[*v + ].lCount == mid - l + && tree[leftIndex].value==tree[rightIndex].value)
- {
- tree[v].lCount += tree[*v +].lCount;
- }
- if(tree[*v + ].rCount == r- mid && tree[rightIndex].value==tree[leftIndex].value)
- {
- tree[v].rCount += tree[*v +].rCount;
- }
- }
- template<typename T>
- void IntervalTree<T>::Add(int v,int l,int r,T m)//区间的更新操作
- {
- if(tree[v].l == l && tree[v].r == r)
- {
- tree[v].add +=m;
- return ;
- }
- tree[v].value += m * (r- l +);
- int mid = (tree[v].l + tree[v].r) /;
- if( r<= mid)
- {
- Add(v * +,l,r,m);
- }
- else
- {
- if(l > mid)
- {
- Add(v * +,l,r,m);
- }
- else
- {
- Add(v * +,l,mid,m);
- Add(v*+,mid+,r,m);
- }
- }
- }
- template<typename T>
- T IntervalTree<T>::Query(int v,int l,int r)//对根结点为v,查询区间l 到 r
- {
- if(tree[v].l == l && tree[v].r == r)
- {
- return tree[v].value +(tree[v].r - tree[v].l +) * tree[v].add;
- }
- if(tree[v].add != )
- {
- tree[v].value += (tree[v].r - tree[v].l +) * tree[v].add;
- Add( v * +,tree[v].l,tree[v].Mid(),tree[v].add);
- Add( v * +,tree[v].Mid()+,tree[v].r,tree[v].add);
- tree[v].add = ;
- }
- int mid = tree[v].Mid();
- if( r <= mid)
- {
- return Query(v * +,l,r);
- }
- else
- {
- if( l > mid)
- {
- return Query(v * + ,l ,r);
- }
- else
- {
- return Query(v * +,l,mid) + Query(v* +,mid+,r);
- }
- }
- }
- template<typename T>
- void IntervalTree<T>::Insert(int r,int i,T value)
- {
- if(tree[r].l == i && tree[r].r == i)
- {
- tree[r].value= value;
- return ;
- }
- tree[r].value += value;
- if(i <= tree[r].Mid())
- {
- Insert(*r+,i,value);
- }
- else
- {
- Insert( * r+ ,i,value);
- }
- }
- int main()
- {
- int n = ;
- while(cin>>n && n > )
- {
- IntervalTree<int> it(n);
- it.BuildTree(,,n-);
- int q = ;
- scanf("%d",&q);
- for(int i = ; i< n;i++)
- {
- int num = ;
- scanf("%d",&num);
- it.Insert(,i,num);
- }
- it.BuildCount(,,n-);
- for(int i = ;i < q;i++)
- {
- int x,y;
- cin>>x>>y;
- cout<<it.Query(,x-,y-)<<endl;
- }
- }
- return ;
- }
POJ3368(Frequent values)--线段树的更多相关文章
- HDOJ-1806 ( Frequent values ) 线段树区间合并
http://acm.hdu.edu.cn/showproblem.php?pid=1806 线段树维护区间出现频率最高的出现次数.为了维护上者,需要维护线段前后缀的出现次数,当和其他线段在端点处的字 ...
- UVA 11235 Frequent values 线段树/RMQ
vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- hdu 1806 Frequent values 线段树
题目链接 给一个非递减数列, n个数, m个询问, 每个询问给出区间[L, R], 求这个区间里面出现次数最多的数的次数. 非递减数列, 这是最关键的一个条件... 需要保存一个区间最左边的数, 最右 ...
- poj3368 Frequent values(线段树)
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- POJ3368 Frequent values(RMQ线段树)
题目大概说给一个递增序列,询问区间出现最多的数. 用莫队算法比较直观,虽然应该会T..好像也可以主席树..不过题目给的序列是有序的,因而相同的数会聚在一起. 考虑把序列分成一段一段,使每段都包含极大的 ...
- [poj3368]Frequent values(rmq)
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ ...
- poj3368 Frequent values
思路: 转化为RMQ. 实现: #include <cstdio> #include <cstring> #include <algorithm> using na ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
随机推荐
- Python设计模式之MVC模式
# -*- coding: utf-8 -*- # author:baoshan quotes = ('A man is not complete until he is married. Then ...
- openresty开发系列23--lua面向对象
openresty开发系列23--lua面向对象 面向对象编程(Object Oriented Programming,OOP)是一种非常流行的计算机编程架构.java,c++,.net等都支持面向对 ...
- postgresql 利用pgAgent实现定时器任务
1.安装pgAgent 利用Application Stack Builder安装向导,安装pgAgent. 根据安装向导一步一步安装即可. 安装完成之后,windows服务列表中会增加一个服务:Po ...
- Js/jQuery实时监听input输入框值变化
前言在做web开发时候很多时候都需要即时监听输入框值的变化,以便作出即时动作去引导浏览者增强网站的用户体验感.而采用onchange时间又往往是在输入框失去焦点(onblur)时候触发,有时候并不能满 ...
- SeetaFace2 cmake VS2015编译编译
cmake Selecting Windows SDK version 10.0.17134.0 to target Windows 10.0.18362. == BUILD_VERSION: v2. ...
- Linux记录-Nginx+Tomcat负载均衡配置
Nginx负载均衡配置及策略: 轮询(默认) 优点:实现简单缺点:不考虑每台服务器的处理能力配置示例如下:upstream www.xxx.com {# 需要负载的server列表server www ...
- python flask框架学习(一)——准备工作和环境配置与安装
Flask装备: 学习自:知了课堂Python Flask框架——全栈开发 1.Python版本:3.6 2.Pycharm软件: 3.安装虚拟环境: (1)安装virtualenv: pip ins ...
- Python - Django - 模板语言之变量
前言: 在 Django 模板语言中变量用 {{ }},逻辑用 {% %} 在 urls.py 中添加对应关系 from django.conf.urls import url from django ...
- matplot画3d图像
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np fig = plt ...
- 【GStreamer开发】GStreamer基础教程08——pipeline的快捷访问
目标 GStreamer建立的pipeline不需要完全关闭.有多种方法可以让数据在任何时候送到pipeline中或者从pipeline中取出.本教程会展示: 如何把外部数据送到pipeline中 如 ...