题目链接:http://acdream.info/problem?

pid=1108

题意:n个数的数列,m次查询某个区间出现次数第k多的数出现的次数。n,m<=100000

解法:这个由于是离线的所以能够先统一处理,然后再输出。能够维护一个left和right指针。pre,pre[i]表示此时区间内出现次数大于等于i的数的种类。

为了降低复杂度,关键是left和right的移动方式,即查询区间怎样排序,假设紧靠区间左端点排序,那么右端点每次一定最大回是n。假设依照右端点排序,左端点每次一定最大是n。这里有个非常好的处理办法,就是模糊排序。先左端点非严格排序。即除以sqrt(n)再排序,这样复杂度最大是n*sqrt(n)

代码:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=100010;
const int INF=1e9+7; int num[Max];
int help[Max];
int r[Max];
int l[Max];
int k[Max];
int pre[Max];
int cnt[Max];
int tool;
bool cmp(int i,int j)
{
if(l[i]/tool==l[j]/tool&&r[i]!=r[j])
return r[i]<r[j];
return l[i]<l[j];
}
int ans[Max];
int n,m;
int findans(int t)
{
int l=1,r=n;
while(l<=r)
{
int middle=(l+r)/2;
if(pre[middle]>=t)
l=middle+1;
else
r=middle-1;
}
return l-1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
tool=sqrt(n);
for(int i=0; i<n; i++)
scanf("%d",num+i),help[i]=i;
for(int i=0; i<m; i++)
scanf("%d%d%d",l+i,r+i,k+i),l[i]--,r[i]--;
sort(help,help+m,cmp);
memset(cnt,0,sizeof cnt);
memset(pre,0,sizeof pre);
int left=0,right=-1;
for(int i=0;i<m;i++)
{
int L=l[help[i]],R=r[help[i]];
while(left<L){ pre[cnt[num[left++]]--]--;}
while(L<left){ pre[++cnt[num[--left]]]++;}
while(right<R){ pre[++cnt[num[++right]]]++;}
while(R<right){ pre[cnt[num[right--]]--]--;}
ans[help[i]]=findans(k[help[i]]);
}
for(int i=0;i<m;i++)
printf("%d\n",ans[i]);
}
return 0;
}

acdreamoj1108(The kth number)的更多相关文章

  1. 协议端口号(protocol port number)

    协议端口号(protocol port number) 先来个注意事项 (-> ->) 这种在协议层间的抽象的协议端口是软件端口,和硬件端口是完全不同的概念.硬件端口是不同设备进行交互的接 ...

  2. 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)

    SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...

  3. C++之路进阶——poj2104(K-th Number)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44537   Accepted: 14781 Ca ...

  4. 图的最短路径-----------Dijkstra算法详解(TjuOj2870_The Kth City)

    做OJ需要用到搜索最短路径的题,于是整理了一下关于图的搜索算法: 图的搜索大致有三种比较常用的算法: 迪杰斯特拉算法(Dijkstra算法) 弗洛伊德算法(Floyd算法) SPFA算法 Dijkst ...

  5. 一个快速double转int的方法(利用magic number)

    代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...

  6. javascript弹出框打印某个数值时,弹出NaN?(not a number)

    一.NaN:表示not a number null 未定义或空字符串 undefined 对象属性不存在 或是声明了变量但从未赋值. 二.出现这种情况有(1)此常数的值是零被零除所得到的结果. (2) ...

  7. C#LeetCode刷题之#414-第三大的数(Third Maximum Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...

  8. 非数值(Not a Number)NaN的解释

    它是一个特殊的数值.它用于表示一个本来要返回数值的操作数未返回数值的情况. 在ECMAScript中,任何数值除以0会返回NaN,而不会导致错误,不会停止代码的执行,因此不会影响其他代码的执行. Na ...

  9. PAT-1144(The Missing Number)set的使用,简单题

    The Missing Number PAT-1144 #include<iostream> #include<cstring> #include<string> ...

随机推荐

  1. 批量还原数据库 SQL Server 2008

    1.如果你够懒,不想一步一步点路径,一步一步选择 2.如果你连单个备份数据库的存储过程都不想多执行,一般每还原一个需要修改数据库名 下面的脚本适合你: /*********************** ...

  2. cubieboard中使用py-kms与dnsmasq搭建局域网内全自动KMS激活环境

    众所周知,KMS激活方式是当前广大网民“试用”windows,office的最广泛的激活方式.几乎可以用于微软的全线产品. 但是在本机使用KMS类的激活工具总是有些不放心,一方面每隔180天都要重新激 ...

  3. brew 更新

    更新: brew update brew update —system 安装, 如:brew install unrar 卸载, 如:brew uninstall unrar

  4. RAD XE10 Seattle

    RAD Studio 10 Seattle RAD XE10 Seattle RAD 10 Seattle c++builder 10 Seattle Delphi 10 Seattle http:/ ...

  5. HTML5边玩边学(1)画布实现方法

    一.<canvas>标签 Html5 引入了一个新的 <canvas> 标签,这个标签所代表的区域就好象一块画布,你的所有图形绘制最后都要在这块画布上呈现.有了这个标签,浏览器 ...

  6. C# Common Keyword

    [C# Common Keyword] 1.abstract Use the abstract modifier in a class declaration to indicate that a c ...

  7. U盘FAT32文件系统

    一.FAT文件系统分为四个部分 参考别人的博客 1.http://blog.163.com/ourhappines@126/blog/static/121363154201311811495492/ ...

  8. java之四大皆空

    SE中的所有空的情况: 第一空:定义变量,变量没有值不能使用,不能打印 public class DiDaJieKong01 { public static void main(String[] ar ...

  9. java反射工具类

    package com.yingchao.kgou.core; import java.lang.reflect.Field; import java.lang.reflect.InvocationT ...

  10. JQ避免出现多次执行一个事件的解决方案

    点击按钮之后会多次执行一个事件的话,就在方法结尾加入如下代码,这样的话事件就可以只执行一次了 //避免出现多次执行事件的问题 event.stopPropagation(); 此外,时间的重复绑定也有 ...