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.强制转换类型之后就能使用子类 ...
随机推荐
- Netty源码分析第5章(ByteBuf)---->第10节: SocketChannel读取数据过程
Netty源码分析第五章: ByteBuf 第十节: SocketChannel读取数据过程 我们第三章分析过客户端接入的流程, 这一小节带大家剖析客户端发送数据, Server读取数据的流程: 首先 ...
- CentOS7下,防火墙设置
CentOS中防火墙程序主要是firewall和iptables两种. CentOS7中firewall服务已经默认安装好了,而iptables服务需要自己用yum install iptabes- ...
- rename命令详解
基础命令学习目录首页 原文链接:http://man.linuxde.net/rename 将main1.c重命名为main.c rename main1.c main.c main1.c renam ...
- resize2fs命令详解
基础命令学习目录首页 原文链接:http://blog.51cto.com/woyaoxuelinux/1870299 resize2fs:调整ext文件系统的空间大小 搭配逻辑卷lv使用方法: ...
- 数据库——SQL数据定义
数据定义 SQL的数据定义语句 操 作 对 象 操 作 方 式 创 建 删 除 修 改 表 CREATE TABLE DROP TABLE ALTER TABLE 视 图 CREATE ...
- “Hello World!“”团队第七周召开的第三次会议
今天是我们团队“Hello World!”团队第七周召开的第三次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...
- 软件工程-东北师大站-第十二次作业(PSP)
1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图
- Scrum Meeting 10.28
今天大部分同学仍停留在学习阶段,进度快的同学已经在配置SQLserver. 成员 今日完成任务 明日计划 所用时间 徐越 配置SQLserver,试用java程序连接数据库 学习servlet,htt ...
- 20135234mqy 实验四
北京电子科技学院(BESTI) 实 验 报 告 课程:java程序设计 班级:1352 姓名:mqy 学号:20135234 成绩: 指导教师:娄嘉鹏 ...
- Sprint7
进展:根据昨天查到的资料,今天开始编写闹钟部分的代码,主要实现了闹钟添加事件显示时间主界面.