poj2761 feed the dog

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

Description

Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.

Your task is to help Jiajia calculate which dog ate the food after each feeding.

Input

The first line contains n and m, indicates the number of dogs and the number of feedings.

The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.

Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.

You can assume that n<100001 and m<50001.

Output

Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.

Sample Input

7 2
1 5 2 6 3 7 4
1 5 3
2 7 1

Sample Output

3
2

题意就是求无修改的区间第K大。。

感想:

厉害了我的哥!终于A了这道题!

记得在去年9月,就写了这道题,结果很遗憾。。wa了。。调不出来了。。就放弃了。

几个月后,我突然心血来潮,又想调这道题了!于是呢——发现两个月的懵逼只因将id[i]写成了i。。。

sad。。。。

题解:

就是整体二分,对所有的问题一起二分,先二分处出一个mid,再把问题是否满足于mid给分为两类,继续递归二分,记得是离线处理,所以要记一下初始的编号。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
struct data1{int l,r,k;}ask[50001];
struct data2{int i,v;}a[100001];
int i,j,k,n,m,x,y,T,t,q[100001],id[100001],mx,tem[100001],ans[100001];
bool mark[100001];
bool cmp(const data2&a,const data2&b){return a.v<b.v;}
int max(int x,int y){return x>y?x:y;}
void add(int t,int x){while (t<=n){q[t]+=x;t+=t&-t;}}
int query(int t){int sum=0;while (t){sum+=q[t];t-=t&-t;}return sum;}
void Acheing(int L,int R,int l,int r){
    if (L>R) return;
    int mid=(l+r)>>1;
    while (a[T+1].v<=mid&&T<n){add(a[T+1].i,1);T++;}
    while (a[T].v>mid&&T){add(a[T].i,-1);T--;}
    int cnt=0;
    for (int i=L;i<=R;i++)if (query(ask[id[i]].r)-query(ask[id[i]].l-1)>=ask[id[i]].k){mark[i]=1;ans[id[i]]=mid;cnt++;}else mark[i]=0;
    int l1=L,l2=L+cnt;
    for (int i=L;i<=R;i++)
        if (mark[i]==1)tem[l1++]=id[i];else tem[l2++]=id[i];
    for (int i=L;i<=R;i++)id[i]=tem[i];
    if (l==r) return;
    Acheing(L,l1-1,l,mid);Acheing(l1,l2-1,mid+1,r);
}
int main(){
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++)scanf("%d",&a[i].v),a[i].i=i,mx=max(mx,a[i].v);
    sort(a+1,a+1+n,cmp);
    for (i=1;i<=m;i++)scanf("%d%d%d",&ask[i].l,&ask[i].r,&ask[i].k),id[i]=i;
    Acheing(1,m,0,mx);
    for (i=1;i<=m;i++) printf("%d\n",ans[i]);
}
时间: 2024-10-07 01:08:50

poj2761 feed the dog的相关文章

POJ2761 Feed the dogs

Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a sp

【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs

先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. 所以总共的复杂度是O(m*(sqrt(n)+sqrt(m)))的. 常数极小. 别的按权值维护的数据结构无法做到O(1)地插入删除. poj2104 的输出优化 别忘了处理负数. 完爆主席树,这份代码目前在 poj2761 上 Rank1. Rank Run ID User Memory Time

[POJ2761] Feed the dogs (Treap)

题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Treap,自己学着写了个板子,留贴备用. 离线操作,将区间移动之,删除旧的添加新的. #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <ma

[POJ2761]Feed the dogs(静态区间第k小,主席树)

题目链接:http://poj.org/problem?id=2761 题意:如题 主席树只能用模版,好菜. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm>

[nRF51822] 7、基础实验代码解析大全(前十)

实验01 - GPIO输出控制LED 引脚输出配置:nrf_gpio_cfg_output(LED_1); 引脚输出置高:nrf_gpio_pin_set(LED_1); 引脚电平转换:nrf_gpio_pin_toggle(LED_1); 毫秒延时:nrf_delay_ms(100); 1 int main(void) 2 { 3 nrf_gpio_cfg_output(LED_1);//配置P0.21为输出 4 nrf_gpio_pin_set(LED_1); //指示灯D1初始状态为熄灭

java面向对象编程(七)--四大特征之多态

1.多态概念 多态性是对象多种表现形式的体现.比如我们说"宠物"这个对象,它就有很多不同的表达或实现,比如有小猫.小狗.蜥蜴等等.那么我到宠物店说"请给我一只宠物",服务员给我小猫.小狗或者蜥蜴都可以,我们就说"宠物"这个对象就具备多态性. java中的多态,就是指一个引用(类型)在不同情况下的多种状态.也可以理解成,多态是指通过指向父类的指针,来调用在不同子类中实现的方法.也可以理解为"一个接口,多个方法". 实现多态有两种

黑马程序员--Objective-C之--OC面向对象的三大特征

OC面向对象的三大特征是封装.继承和多态. OC语言也支持面向对象的三大特征,OC提供了@private.@package.@protected.@public等访问控制符来实现封装 OC也允许子类来继承父类,并且支持多态. 一.封装 封装指的是將对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类所提供的方法对内部信息的操作和访问. OC的封装特性需要通过使用访问控制符来实现. (一).访问控制符 1.@private:当前类访问权限.如果类的成员变量使用@priva

Java多态的体现之继承

/** * * 功能:演示多态 */ package com.litao; public class Demo5 { public static void main(String[] args) { // TODO Auto-generated method stub //体现多态 /*Cat cat1 = new Cat(); cat1.cry(); Dog dog = new Dog(); dog.cry();*/ //自动判断右边引用是什么类型的 //所有子类或孙子类的实例统统让一个父类来

Java从入门到精通04-类、对象

Java语言是面向对象的,计算机语言的发展向接近人的思维方式演变: 类和对口箱的区别和联系1.类是抽象的,概念的,代表一类事物,比如人类.猫类...2.对象是具体的,实际的,代表一个事物,比如张三.李四...3.类是对象的模板,对象是类的一个个体.实例: 一个全面类的定义模板: package 包名; class 类名 extends 父类 implements 接口名{ 成员变量; 构造方法; 成员方法; } 类的成员方法的参数可以是多个,方法可以没有返回值: 构造方法是类的一种特殊方法,它的