标签:限制 pre 后缀 sum tput printf 整数 接下来 shelf
正解:二分+主席树。
闲得无聊刷水题。。因为这题用可持久化二维线段树过不去,所以只能写两个程序。。
$R,C<=200$,因为$Pi,j$很小,直接记一个后缀和,二分答案即可。
$R=1$,直接建主席树,乱搞一下即可。
1 //It is made by wfj_2048~ 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 #include <cstdlib> 6 #include <cstdio> 7 #include <vector> 8 #include <cmath> 9 #include <queue> 10 #include <stack> 11 #include <map> 12 #include <set> 13 #define il inline 14 #define RG register 15 #define ll long long 16 17 using namespace std; 18 19 int n,m,Q; 20 21 il int gi(){ 22 RG int x=0,q=1; RG char ch=getchar(); 23 while ((ch<‘0‘ || ch>‘9‘) && ch!=‘-‘) ch=getchar(); 24 if (ch==‘-‘) q=-1,ch=getchar(); 25 while (ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-48,ch=getchar(); 26 return q*x; 27 } 28 29 namespace chair{ 30 31 #define N (500010) 32 33 struct query{ int l,r,h; }q[N]; 34 35 int sum[15*N],num[15*N],ls[15*N],rs[15*N],rt[N],a[N],sz; 36 37 il void insert(RG int x,RG int &y,RG int l,RG int r,RG int v){ 38 sum[y=++sz]=sum[x]+v,num[y]=num[x]+1,ls[y]=ls[x],rs[y]=rs[x]; 39 if (l==r) return; RG int mid=(l+r)>>1; 40 v<=mid?insert(ls[x],ls[y],l,mid,v):insert(rs[x],rs[y],mid+1,r,v); return; 41 } 42 43 il int query(RG int x,RG int y,RG int l,RG int r,RG int v){ 44 if (l==r) return (v-1)/l+1; RG int mid=(l+r)>>1,tmp=sum[rs[y]]-sum[rs[x]]; 45 return v<=tmp?query(rs[x],rs[y],mid+1,r,v):query(ls[x],ls[y],l,mid,v-tmp)+num[rs[y]]-num[rs[x]]; 46 } 47 48 int main(){ 49 for (RG int i=1;i<=m;++i) a[i]=gi(); 50 for (RG int i=1;i<=m;++i) insert(rt[i-1],rt[i],1,1000,a[i]); 51 for (RG int i=1,l1,l2,r1,r2,h;i<=Q;++i){ 52 l1=gi(),r1=gi(),l2=gi(),r2=gi(),h=gi(); 53 if (sum[rt[r2]]-sum[rt[r1-1]]<(ll)h) puts("Poor QLW"); 54 else printf("%d\n",query(rt[r1-1],rt[r2],1,1000,h)); 55 } 56 return 0; 57 } 58 59 #undef N 60 61 } 62 63 namespace twopoints{ 64 65 #define N (210) 66 67 int sum[1010][N][N],num[1010][N][N],p[N][N]; 68 69 il int asksum(RG int k,RG int l1,RG int r1,RG int l2,RG int r2){ 70 return sum[k][l2][r2]-sum[k][l1-1][r2]-sum[k][l2][r1-1]+sum[k][l1-1][r1-1]; 71 } 72 73 il int asknum(RG int k,RG int l1,RG int r1,RG int l2,RG int r2){ 74 return num[k][l2][r2]-num[k][l1-1][r2]-num[k][l2][r1-1]+num[k][l1-1][r1-1]; 75 } 76 77 il int query(RG int l1,RG int r1,RG int l2,RG int r2,RG int h){ 78 RG int l=1,r=1000,mid,key,res=1; 79 while (l<=r){ 80 mid=(l+r)>>1; 81 if (asksum(mid,l1,r1,l2,r2)>=h) res=mid,l=mid+1; else r=mid-1; 82 } 83 key=asksum(res+1,l1,r1,l2,r2); 84 return asknum(res+1,l1,r1,l2,r2)+(h-key-1)/res+1; 85 } 86 87 int main(){ 88 for (RG int i=1;i<=n;++i) 89 for (RG int j=1;j<=m;++j) 90 p[i][j]=gi(),sum[p[i][j]][i][j]=p[i][j],num[p[i][j]][i][j]=1; 91 for (RG int k=1000;k;--k) 92 for (RG int i=1;i<=n;++i) 93 for (RG int j=1;j<=m;++j) 94 sum[k][i][j]+=sum[k+1][i][j],num[k][i][j]+=num[k+1][i][j]; 95 for (RG int k=1000;k;--k) 96 for (RG int i=1;i<=n;++i) 97 for (RG int j=1;j<=m;++j){ 98 sum[k][i][j]+=sum[k][i][j-1]+sum[k][i-1][j]-sum[k][i-1][j-1]; 99 num[k][i][j]+=num[k][i][j-1]+num[k][i-1][j]-num[k][i-1][j-1]; 100 } 101 for (RG int i=1,l1,r1,l2,r2,h;i<=Q;++i){ 102 l1=gi(),r1=gi(),l2=gi(),r2=gi(),h=gi(); 103 if (asksum(1,l1,r1,l2,r2)<h) puts("Poor QLW"); 104 else printf("%d\n",query(l1,r1,l2,r2,h)); 105 } 106 return 0; 107 } 108 109 #undef N 110 111 } 112 113 int main(){ 114 #ifndef ONLINE_JUDGE 115 freopen("bookshelf.in","r",stdin); 116 freopen("bookshelf.out","w",stdout); 117 #endif 118 n=gi(),m=gi(),Q=gi(); 119 if (n==1) chair::main(); 120 else twopoints::main(); 121 return 0; 122 }
标签:限制 pre 后缀 sum tput printf 整数 接下来 shelf
原文地址:http://www.cnblogs.com/wfj2048/p/7267289.html