POJ - 2104(平方分割
又是二分边界处理出了问题,刚开始r初始值设置为n-1,但这样会导致l不可能取到n-1,所以r应该设为n,话说这题我居然跑了10s多,也算是破了个人记录了,orz
Accepted
|
1860 | 10829 | 1249 |
2015-05-08 00:38:41
|
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<utility> #include<vector> #include<cstring> #include<cmath> #define INF 0x3fffffff #define pb push_back #define pn(x) cerr<<x<<endl using namespace std; typedef long long ll; const int maxv=100005; const int B=1000; int n,m; int A[maxv]; int num[maxv]; vector<int> bucket[maxv/B]; ll calc(int l,int r,int m){///计算比m小的个数 ll ans=0; if(l/B==r/B){ for(int i=l;i<=r;i++){ if(A[i]<m) ans++; } return ans; } int lb=(l+B)/B,rb=r/B; for(int i=l;i<lb*B;i++) if(A[i]<m) ans++; for(int i=rb*B;i<=r;i++) if(A[i]<m) ans++; for(int i=lb;i<rb;i++){ ans+=lower_bound(bucket[i].begin(),bucket[i].end(),m)-bucket[i].begin(); } return ans; } int main(){ freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ scanf("%d",&A[i]); num[i]=A[i]; bucket[i/B].pb(A[i]); } for(int i=0;i<=(n-1)/B;i++) sort(bucket[i].begin(),bucket[i].end()); sort(num,num+n); num[n]=INF; while(m--){ ll i,j,k; scanf("%I64d%I64d%I64d",&i,&j,&k); ll l=0,r=n; while(r-l>1){ ll mid=(r+l)/2; if(calc(i-1,j-1,num[mid])<k) l=mid; else r=mid; } printf("%d\n",num[l]); } return 0; }