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.强制转换类型之后就能使用子类 ...
随机推荐
- lua栈
既然Lua虚拟机模拟的是CPU的运作,那么Lua栈模拟的就是内存的角色.在Lua内部,参数的传递是通过Lua栈,同时Lua与C等外部进行交互的时候也是使用的栈.,先关注的是Lua栈的分配,管理和相关的 ...
- 如何在unix系统中用别的用户运行一个程序?
1.问题的缘由 实际开发系统的时候,经常需要用别的用户运行一个程序.比如,有些系统为保证系统安全,不允许使用root来运行.这里,我们总结了unix系统下如何解决这个问题的一些方法.同时,我们还讨论如 ...
- ABP.ModuleZero.Feature——特性管理
原文地址:http://aspnetboilerplate.com/Pages/Documents/Feature-Management (在翻译原文的基础上增补更多细节说明) 简介 在大多数SAA ...
- 如何防范和应对Redis勒索,腾讯云教你出招
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 9月10日下午,又一起规模化利用Redis未授权访问漏洞攻击数据库的事件发生,此次 ...
- eos源码编译
编译源码 运行代码 在阿里云 纽约服务器上运行没有出现任何问题. 在其他电脑上出现很多问题. 搜集到的问题如下: 随着EOSIO软件越来越成熟,后来的开发者也越来越幸福.EOS相关源码的编译和运行变得 ...
- openssl在多平台和多语言之间进行RSA加解密注意事项
首先说一下平台和语言: 系统平台为CentOS6.3,RSA加解密时使用NOPADDING进行填充 1)使用C/C++调用系统自带的openssl 2)Android4.2模拟器,第三方openssl ...
- eject命令详解
基础命令学习目录首页 原文链接:http://www.kgc.cn/bbs/post/128680.shtml Linux eject命令用于退出抽取式设备. 若设备已挂入,则eject会先将该设备卸 ...
- python爬虫项目(新手教程)之知乎(requests方式)
-前言 之前一直用scrapy与urllib姿势爬取数据,最近使用requests感觉还不错,这次希望通过对知乎数据的爬取为 各位爬虫爱好者和初学者更好的了解爬虫制作的准备过程以及requests请求 ...
- React.js - 入门
React.js - 第1天 1. React简介 React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 ...
- Java面向对象程序设计
北京电子科技学院(BESTI) 实 ...