poj2761 feed the dog
Posted Acheing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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[50001]; struct data2{int i,v;}a[100001]; int i,j,k,n,m,x,y,T,t,q[100001],id[100001],mx,tem[100001],ans[100001]; bool mark[100001]; 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=0;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)>>1; while (a[T+1].v<=mid&&T<n){add(a[T+1].i,1);T++;} while (a[T].v>mid&&T){add(a[T].i,-1);T--;} int cnt=0; for (int i=L;i<=R;i++)if (query(ask[id[i]].r)-query(ask[id[i]].l-1)>=ask[id[i]].k){mark[i]=1;ans[id[i]]=mid;cnt++;}else mark[i]=0; int l1=L,l2=L+cnt; for (int i=L;i<=R;i++) if (mark[i]==1)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-1,l,mid);Acheing(l1,l2-1,mid+1,r); } int main(){ scanf("%d%d",&n,&m); for (i=1;i<=n;i++)scanf("%d",&a[i].v),a[i].i=i,mx=max(mx,a[i].v); sort(a+1,a+1+n,cmp); for (i=1;i<=m;i++)scanf("%d%d%d",&ask[i].l,&ask[i].r,&ask[i].k),id[i]=i; Acheing(1,m,0,mx); for (i=1;i<=m;i++) printf("%d\n",ans[i]); }
以上是关于poj2761 feed the dog的主要内容,如果未能解决你的问题,请参考以下文章