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自带的模块并不多,好处就是Lua足够的小,毕竟它的设计目标是定位成一个嵌入式的轻量级语言的. 相关的函数index2adr static TValue *index2adr (lua_State ...
- 错误结果保存示例 - 【jmeter】
- Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第7节: 获取异线程释放的对象
Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第七节: 获取异线程释放的对象 上一小节分析了异线程回收对象, 原理是通过与stack关联的WeakOrder ...
- 【python 2.7】输入任意字母数字,输出其对应的莫尔斯码并播放声音
#python 2.7 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import winsound,sys,time __author ...
- linux磁盘扩容日志
//针对ext4文件格式的操作系统(如CentOS6):// umount /dev/vdb e2fsck -f /dev/vdb resize2fs /dev/vdb mount /dev/vdb ...
- dvwa——sql手动注入和sqlmap自动注入
手动注入 low: 源码: <?php if( isset( $_REQUEST[ 'Submit' ] ) ) { // Get input $id = $_REQUEST[ 'id' ]; ...
- kafka handler
1.配置kafka 参数文件 在ogg主目录下有示例文件: [root@WH0PRDBRP00AP0013 ogg]# cd AdapterExamples/big-data/kafka/ [root ...
- 第二次作业(homework-02)成绩公布
学位后三位和对应成绩: 057 0008 4011 4012 7014 5015 5017 6018 0019 0026 2027 7036 0038 7.5046 7048 6.5051 0061 ...
- 第十周psp作业
本周psp 本周进度条 代码累积折线图 博文字数累积折线图 饼状图
- Scrum Meeting 10.30
成员 今日任务 明日计划 用时 徐越 配置servlet环境,设计开发文档 设计开发文档,配置服务器,使得本地可以访问服务器 5h 武鑫 软件界面设计:学习使用Activity和Fragment 设计 ...