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.强制转换类型之后就能使用子类 ...
随机推荐
- 通过XML文件实现人物之间的对话
一.建立一个XML文档,放在项目中Assert/Resources/XML文件下 XML的内容如下: <?xml version="1.0" encoding="u ...
- 比较undefined和“undefined”
说实话,它们之间的区别挺明显的,我们一般认为undefined是JavaScript提供的一个“关键字”,而“undefined”却是一个字符串,只是引号的内容和undefined一样. undefi ...
- CocoStuff—基于Deeplab训练数据的标定工具【三、标注工具的使用】
一.说明 本文为系列博客第三篇,主要展示COCO-Stuff 10K标注工具的使用过程及效果. 本文叙述的步骤默认在完成系列文章[二]的一些下载数据集.生成超像素处理文件的步骤,如果过程中有提示缺少那 ...
- PCL 库存在vtk的问题导致libproj.so链接错误
常变现为** No rule to make target '/usr/lib/x86_64-linux-gnu/libproj.so', needed by ××× vtk库的bug导致,目前尚未修 ...
- 机器学习(一):记一次k一近邻算法的学习与Kaggle实战
本篇博客是基于以Kaggle中手写数字识别实战为目标,以KNN算法学习为驱动导向来进行讲解. 写这篇博客的原因 什么是KNN kaggle实战 优缺点及其优化方法 总结 参考文献 写这篇博客的原因 写 ...
- An internal error occurred during: "Launching MVC on Tomcat 7.x".
删除工作空间下的“/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.genuitec.eclipse.ast.deploy.core ...
- 第二阶段Sprint3
昨天:查看资料,开始视频录制部分的代码实现 今天:事实现保存到指定路径,并能够选择播放 遇到的问题:自动生成文件名,是否需要自己命名?怎么实现?
- Hibernate笔记③--集合映射、组合映射、联合主键、查询案例
lazy 懒加载 默认为proxy 继承映射 discriminant column="type" type="string" 集合映射 生成表的语句: ...
- struts2 Action生命周期
Struts2.0中的对象既然都是线程安全的,都不是单例模式,那么它究竟何时创建,何时销毁呢? 这个和struts2.0中的配置有关,我们来看struts.properties ### if spec ...
- 剑指offer:矩形覆盖
题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 解题思路: 和跳台阶那道题差不多.分别以矩形的两条边长做拓 ...