码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2761(Feed the dogs)

时间:2016-06-29 11:09:21      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:传送门

题目大意:静态区间询问第k小

题目思路:整体二分,与poj2104一模一样 讲解链接:传送门

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <cstring>
 7 #include <stack>
 8 #include <cctype>
 9 #include <queue>
10 #include <string>
11 #include <vector>
12 #include <set>
13 #include <map>
14 #include <climits>
15 #define lson root<<1,l,mid
16 #define rson root<<1|1,mid+1,r
17 #define fi first
18 #define se second
19 #define ping(x,y) ((x-y)*(x-y))
20 #define mst(x,y) memset(x,y,sizeof(x))
21 #define mcp(x,y) memcpy(x,y,sizeof(y))
22 using namespace std;
23 #define gamma 0.5772156649015328606065120
24 #define MOD 1000000007
25 #define inf 0x3f3f3f3f
26 #define N 200005
27 #define maxn 50005
28 typedef pair<int,int> PII;
29 typedef long long LL;
30 
31 int n,m;
32 struct Node{
33     int id,l,r,v,f;
34     Node(){}
35     Node(int a,int b,int c,int d,int e){
36         id=a;l=b;r=c;v=d;f=e;
37     }
38 }node[N],t1[N],t2[N];
39 int tree[N],hcnt,ans[N];
40 void add(int i,int v){for(;i<=n;i+=(i&-i))tree[i]+=v;}
41 int query(int i){int res=0;for(;i;i-=(i&-i))res+=tree[i];return res;}
42 void solve(int ql,int qr,int l,int r){
43     if(ql>qr)return;
44     if(l==r){
45         for(int i=ql;i<=qr;++i)if(node[i].f==2)
46             ans[node[i].id]=l;
47         return;
48     }
49     int mid=l+r>>1;
50     int len1=0,len2=0;
51     for(int i=ql;i<=qr;++i){
52         if(node[i].f==1){
53             if(node[i].v<=mid){
54                 add(node[i].id,1);
55                 t1[len1++]=node[i];
56             }
57             else t2[len2++]=node[i];
58         }
59         else{
60             int temp=query(node[i].r)-query(node[i].l-1);
61             if(temp<node[i].v){
62                 node[i].v-=temp;
63                 t2[len2++]=node[i];
64             }
65             else t1[len1++]=node[i];
66         }
67     }
68     for(int i=0;i<len1;++i)if(t1[i].f==1)add(t1[i].id,-1);
69     for(int i=0;i<len1;++i)node[i+ql]=t1[i];
70     for(int i=0;i<len2;++i)node[i+ql+len1]=t2[i];
71     solve(ql,ql+len1-1,l,mid);
72     solve(ql+len1,qr,mid+1,r);
73 }
74 int main(){
75     int i,j,group,x,y,v;
76     while(scanf("%d%d",&n,&m)!=EOF){
77         hcnt=0;
78         int _min=inf,_max=-inf;
79         for(i=1;i<=n;++i){
80             scanf("%d",&x);
81             _max=max(_max,x);_min=min(_min,x);
82             node[++hcnt]=Node(i,1,1,x,1);
83         }
84         for(i=1;i<=m;++i){
85             scanf("%d%d%d",&x,&y,&v);
86             node[++hcnt]=Node(i,x,y,v,2);
87         }
88         solve(1,hcnt,_min,_max);
89         for(i=1;i<=m;++i)printf("%d\n",ans[i]);
90     }
91     return 0;
92 }

 

POJ2761(Feed the dogs)

标签:

原文地址:http://www.cnblogs.com/Kurokey/p/5626100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!