http://poj.org/problem?id=2761 (题目链接)

题意

  求区间第K大。

Solution

  同poj2104

细节

  可能会有相同元素。

主席树代码

// poj2761
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define lim 1000000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
int n,m,sz,rt[maxn];
struct node {
int son[2],s;
int& operator [] (int x) {return son[x];}
}tr[maxn*40]; void build(int &u,int v,int l,int r,int val) {
u=++sz;
if (l==r) {tr[u].s=tr[v].s+1;return;}
int mid=(l+r)>>1;
if (val<=mid) build(tr[u][0],tr[v][0],l,mid,val),tr[u][1]=tr[v][1];
else build(tr[u][1],tr[v][1],mid+1,r,val),tr[u][0]=tr[v][0];
tr[u].s=tr[tr[u][0]].s+tr[tr[u][1]].s;
}
int query(int u,int v,int l,int r,int k) {
if (l==r) return l;
int mid=(l+r)>>1,c=tr[tr[v][0]].s-tr[tr[u][0]].s;
if (c>=k) return query(tr[u][0],tr[v][0],l,mid,k);
else return query(tr[u][1],tr[v][1],mid+1,r,k-c);
}
int main() {
scanf("%d%d",&n,&m);
for (int x,i=1;i<=n;i++) {
scanf("%d",&x);
build(rt[i],rt[i-1],-lim,lim,x);
}
for (int x,y,k,i=1;i<=m;i++) {
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",query(rt[x-1],rt[y],-lim,lim,k));
}
return 0;
}

分块+莫队代码

// poj2761
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
struct ask {int l,r,k,id;}t[maxn];
struct data {int w,id;}a[maxn];
int pos[maxn],cnts[maxn],b[maxn],p[maxn],ma[maxn],ans[maxn],n,m,s,q,block; bool wcmp(data a,data b) {
return a.w<b.w;
}
bool poscmp(ask a,ask b) {
return pos[a.l]==pos[b.l] ? a.r<b.r : pos[a.l]<pos[b.l];
}
void build() {
block=(int)sqrt((float)m);
for (int i=1;i<=m;i++) pos[i]=(i-1)/block+1;
s=m%block ? m/block+1 : m/block;
for (int i=1;i<=s;i++) cnts[i]=0;
}
void update(int x,int val) {
cnts[pos[x]]+=val;
b[x]+=val;
}
int query(int k) {
int tot=0;
for (int i=1;i<=s;i++) {
tot+=cnts[i];
if (tot>=k) {
tot-=cnts[i];
for (int j=(i-1)*block+1;j<=min(i*block,m);j++) {
if (tot+b[j]>=k) return ma[j];
else tot+=b[j];
}
}
}
}
int main() {
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++) scanf("%d",&a[i].w),a[i].id=i;
for (int i=1;i<=q;i++) scanf("%d%d%d",&t[i].l,&t[i].r,&t[i].k),t[i].id=i;
sort(a+1,a+1+n,wcmp);
m=0;
ma[p[a[1].id]=++m]=a[1].w;
for (int i=2;i<=n;i++) {
if (a[i].w!=a[i-1].w) m++;
ma[p[a[i].id]=m]=a[i].w;
}
block=(int)sqrt((float)n);
for (int i=1;i<=n;i++) pos[i]=(i-1)/block+1;
sort(t+1,t+1+q,poscmp);
build();
for (int l=1,r=0,i=1;i<=q;i++) {
for (;r<t[i].r;r++) update(p[r+1],1);
for (;r>t[i].r;r--) update(p[r],-1);
for (;l<t[i].l;l++) update(p[l],-1);
for (;l>t[i].l;l--) update(p[l-1],1);
ans[t[i].id]=query(t[i].k);
}
for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
return 0;
}

  

【poj2761】 Feed the dogs的更多相关文章

  1. 【POJ2761】【区间第k大】Feed the dogs(吐槽)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

  2. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. 【poj2459】 Feed Accounting

    http://poj.org/problem?id=2459 (题目链接) 题意 一堆不知何时运到的草料原有F1 kg,在第D天被牛吃成F2 kg,每头牛在[l,r]吃草料,每天吃1kg.求草料是什么 ...

  4. POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs Time Limit: 6000MS   Memor ...

  5. 【五】将博客从jekyll迁移到了hexo

    本系列有五篇:分别是  [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面  [二]jekyll 的使用 :主要是jekyll的配置  [三]Markdo ...

  6. .htaccess详解及.htaccess参数说明【转】

    目录(?)[-] htaccess 详解 htaccess rewrite 规则详细说明 RewriteEngine OnOff RewriteBase URL-path RewriteCond Te ...

  7. 【C#】委托

    一.委托的基本的写法 internal class Program { private static void Main(string[] args) { ChainDelegate(); Conso ...

  8. 【POJ3169 】Layout (认真的做差分约束)

    Layout   Description Like everyone else, cows like to stand close to their friends when queuing for ...

  9. JS正则表达式大全【转】

    正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个 ...

随机推荐

  1. Java 集合系列05之 LinkedList详细介绍(源码解析)和使用示例

    概要  前面,我们已经学习了ArrayList,并了解了fail-fast机制.这一章我们接着学习List的实现类——LinkedList.和学习ArrayList一样,接下来呢,我们先对Linked ...

  2. 条件注释判断浏览器版本<!--[if lt IE 9]>

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...

  3. 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块

    出于兴趣爱好,前段时间自己尝试写了一个叫simple的cms,里面使用了百度ueditor编辑器,发现它的多图片上传模块很不错,用起来很方便,又可以选择已经上传好的图片.正好我又是个懒人,发现有现成的 ...

  4. [转]终于找到全annotation配置springMVC的方法了(事务不失效)

    原文:http://icanfly.iteye.com/blog/778401 icanfly 写道 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将servic ...

  5. 开发环境python

    python开发环境搭建   虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上py ...

  6. QT 文件操作

    QT提供了QFile类用于文件读写 QFile可以读写文本文件,也可以读写二进制文件 #include "widget.h" #include <QGridLayout> ...

  7. ORACLE查出表所有的触发器及触发器详细信息

    ORACLE查出表所有的触发器及触发器详细信息 一.查all_triggers表得到trigger_name Sql代码 select trigger_name from all_triggers w ...

  8. 海王星给你好看!FineUI v4.0公测版发布暨《你找BUG我送书》活动开始(活动已结束!)

    <FineUI v4.0 你找BUG我送书>活动已结束,恭喜如下三位网友获得由 FineUI 作者亲自翻译的图书<jQuery实战 第二版>! 奋斗~ 吉吉﹑ purplebo ...

  9. 将DBF文件导入Sqlserver数据库

    项目中的问题:用户选择N个dbf文件导入sql2005数据库,由于每年dbf表结构都在变化,所以在sql2005中根本就不存在,需要每年根据dbf的结构自动建表.(文章来自http://blog.cs ...

  10. 使用rest方式修改服务端xml文件

    想法: 最近想用REST的方式去修改一个远端的xml文件,于是自己写了个Handler 设计: 用现成的XPath,和GET,POST,PUT,DELETE这4个http方法作为动作,达到直接读写xm ...