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
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[];
struct data2{int i,v;}a[];
int i,j,k,n,m,x,y,T,t,q[],id[],mx,tem[],ans[];
bool mark[];
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=;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)>>;
while (a[T+].v<=mid&&T<n){add(a[T+].i,);T++;}
while (a[T].v>mid&&T){add(a[T].i,-);T--;}
int cnt=;
for (int i=L;i<=R;i++)if (query(ask[id[i]].r)-query(ask[id[i]].l-)>=ask[id[i]].k){mark[i]=;ans[id[i]]=mid;cnt++;}else mark[i]=;
int l1=L,l2=L+cnt;
for (int i=L;i<=R;i++)
if (mark[i]==)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-,l,mid);Acheing(l1,l2-,mid+,r);
}
int main(){
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)scanf("%d",&a[i].v),a[i].i=i,mx=max(mx,a[i].v);
sort(a+,a++n,cmp);
for (i=;i<=m;i++)scanf("%d%d%d",&ask[i].l,&ask[i].r,&ask[i].k),id[i]=i;
Acheing(,m,,mx);
for (i=;i<=m;i++) printf("%d\n",ans[i]);
}
poj2761 feed the dog的更多相关文章
- 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs
先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...
- [POJ2761] Feed the dogs (Treap)
题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...
- [POJ2761]Feed the dogs
Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...
- [Poj2761]Feed the dogs(主席树)
Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...
- [nRF51822] 7、基础实验代码解析大全(前十)
实验01 - GPIO输出控制LED 引脚输出配置:nrf_gpio_cfg_output(LED_1); 引脚输出置高:nrf_gpio_pin_set(LED_1); 引脚电平转换:nrf_gpi ...
- 瘋耔java语言笔记
一◐ java概述 1.1 ...
- nRF51822之WDT浅析
看门狗定时器 NRF51822 的看门狗定时器是倒计数器, 当计数值减少到 0 时产生 TIMEOUT 事件. 通过 START task 来启动看门狗定时器. 看门狗定时器启动时,如没有其他 32. ...
- Objective-C中的封装、继承、多态、分类
封装的好处: 过滤不合理的值 屏蔽内部的赋值过程 让外界不必关注内部的细节 继承的好处: 不改变原来模型的基础上,拓充方法 建立了类与类之间的联系 抽取了公共代码 坏处:耦合性强(当去掉一个父类,子类 ...
- [Objective-c 基础 - 2.4] 多态
A.对象的多种形态 1.父类指针指向子类对象 2.调用方法的时候,会动态监测真实地对象的方法 3.没有继承,就没有多态 4.好处:用一个父类指针可以指向不同的子类对象 5.强制转换类型之后就能使用子类 ...
随机推荐
- 第十一次ScrumMeeting博客
第十一次ScrumMeeting博客 本次会议于11月29日(三)22时整在3公寓725房间召开,持续30分钟. 与会人员:刘畅.辛德泰张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的内容和 ...
- Spring自定义标签解析与实现
在Spring Bean注册解析(一)和Spring Bean注册解析(二)中我们讲到,Spring在解析xml文件中的标签的时候会区分当前的标签是四种基本标签(import.alias ...
- 查询数据库时mapper报错:It's likely that neither a Result Type nor a Result Map was specified.
因为mapper.xml里把resultType写成了parameterType
- 20162325 金立清 S2 W5 C14
20162325 2017-2018-2 <程序设计与数据结构>第5周学习总结 关键内容摘要 集合是收集并组织其他对象的对象 集合中的元素一般由加入集合的次序或元素之间某些固有的关系而组织 ...
- iOS自学-混合编程
OC调用swift,引入头文件 #improt "工程名字-swift.h" swift调用OC,在桥梁文件里面引入OC文件 的头文件 尽情混合编程吧...
- Task 1 四则运算
第一节课作业 1.设计思想:随机产生30道题目,主体是一个for循环,在循环体中加上相应的内容,包括随机数函数得到两个运算数值以及运算符号,再利用switch,case结构输出相应的题目即可. 2.源 ...
- ubuntu下安装matlab2015b
========= 安装过程 1.下载MATLAB2015b破解版 操作系统:Ubuntu 16.04 LTS 程序文件:Matlab2015b-glnxa64破解版 解压提取文件:在ubuntu系统 ...
- wamp 修改www目录
我的情况 Wamp版本:2.2 WAMP2.2安装目录:C:/ www目录:D:/wamp/www/ 变更目录:E:/HbuilderProjects/ 一 主要过程: (1)修改 D:\wamp\b ...
- Do~Hamburger~
在上一次的结对编程中,我的结对队友是 方俊杰 ,大家都称他为“JJ师兄”. 我们两个彼此在合作中发现错误并在合作中一起进步. First(汉堡上层面包): JJ他的JAVA功底比我扎实很多,所 ...
- 13_Java面向对象_第13天(static、final、匿名对象、内部类、包、修饰符、代码块)_讲义
今日内容介绍 1.final 关键字 2.static 关键字 3.匿名对象 4.内部类 5.包的声明与访问 6.访问修饰符 7.代码块 01final关键字概念 A: 概述 继承的出现提高了代码的复 ...