一个上午唯一的成果T^T还有8天就过年了
说说这个题,比基础的多了一个sum数组,用来记录当前区间在这个数之前进入左子树的元素值之和,
/*************** hdu3473 2016.1.29 561MS 30344K 2956 B C++ ***************/ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1000010; int sorted[MAXN]; //对原集合中元素排序后的值 int val[30][MAXN]; //val记录第k层当前位置的值 int toleft[30][MAXN]; //记录元素所在区间当前位置前的元素进入到左子树的个数 long long sum[30][MAXN]; //记录元素所在区间当前位置前的元素进入到左子树的元素和 int n,t,cnt,m,x,y; long long lsum,rsum; int lnum,rnum; void build(int l, int r, int d) { if (l == r) return ; int mid = (l + r) >> 1; int same = mid - l + 1; for (int i=l; i<=r; i++) if (val[d][i] < sorted[mid]) same--; int lp = l, rp = mid+1; for (int i=l; i<=r; i++) { if (i == l) sum[d][i]=toleft[d][i] = 0; else toleft[d][i] = toleft[d][i-1],sum[d][i]=sum[d][i-1]; if (val[d][i] < sorted[mid]) { toleft[d][i]++; val[d+1][lp++] = val[d][i]; sum[d][i]+=val[d][i]; } else if (val[d][i] > sorted[mid]) { val[d+1][rp++] = val[d][i]; } else { if (same) { same--; toleft[d][i]++; val[d+1][lp++] = val[d][i]; sum[d][i]+=val[d][i]; } else { val[d+1][rp++] = val[d][i]; } } } build(l, mid, d+1); build(mid+1, r, d+1); } int query(int a, int b, int k, int l, int r, int d) { if (a == b) return val[d][a]; int mid = (l + r) >> 1; int s, ss; long long sss; if (a == l) { s = toleft[d][b]; ss = 0; sss=sum[d][b]; } else { s = toleft[d][b] - toleft[d][a-1]; ss = toleft[d][a-1]; sss=sum[d][b]-sum[d][a-1];//为啥没管右区间的事?因为sum数组储存的是”当前区间此数之前进入左子树的元素和“ //和右区间没关系啊。 } if (s >= k) { a = l + ss; b = l + ss + s - 1; return query(a, b, k, l, mid, d+1); } else { lnum+=s;//既然进入这个else了 说明前面进入左子树的个数小于k 那么k-th左侧的个数肯定要+[a,b]中进入左孩子的个数,即+s lsum+=sss;//同理,k-th左侧数之和必然要加上(区间[a,b]中小于第k大元素值之和)即sss a = mid+1 + a - l - ss; b = mid+1 + b - l - toleft[d][b]; return query(a, b, k-s, mid+1, r, d+1); } } long long s[MAXN]; int main() { //freopen("cin.txt","r",stdin); cnt=1; scanf("%d",&t); while(t--) { printf("Case #%d:\n",cnt++); scanf("%d",&n); s[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&sorted[i]); val[0][i]=sorted[i]; s[i]=s[i-1]+sorted[i]; } sort(sorted+1,sorted+n+1); build(1,n,0); scanf("%d",&m); while(m--) { scanf("%d%d",&x,&y); x++;y++; lsum=lnum=0; int ave=query(x,y,(y-x+2)/2,1,n,0); rnum=y-x+1-lnum;//这个区间里除了进入左边的就是进入右边的 rsum=s[y]-s[x-1]-lsum;//s数组储存的是截止到这个数为止的所有元素和 printf("%I64d\n",rsum-lsum+(long long)(lnum-rnum)*ave);// } puts(""); } return 0; }
本来想照着邝斌的思路改来着,结果一直RE
/*************** hdu3473 2016.1.29 ***************/ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200010; int sorted[MAXN]; //对原集合中元素排序后的值 int val[30][MAXN]; //val记录第k层当前位置的值 int toleft[30][MAXN]; //记录元素所在区间当前位置前的元素进入到左子树的个数 long long sum[30][MAXN]; //记录比当前元素小的元素的和 int n,t,cnt,m,x,y; long long lsum,rsum,ans; int lnum,rnum; void build(int l, int r, int d) { if (l == r) { sum[d][l]=val[d][l]; return ; } int mid = (l + r) >> 1; int same = mid - l + 1; for (int i=l; i<=r; i++) { if (val[d][i] < sorted[mid]) same--; sum[d][i]=val[d][i]; if(i>l) sum[d][i]+=sum[d][i-1]; } int lp = l, rp = mid+1; for (int i=l; i<=r; i++) { if (i == l) toleft[d][i] = 0; else toleft[d][i] = toleft[d][i-1]; if (val[d][i] < sorted[mid]) { toleft[d][i]++; val[d+1][lp++] = val[d][i]; // sum[d][i]+=val[d][i]; } else if (val[d][i] > sorted[mid]) { val[d+1][rp++] = val[d][i]; } else { if (same) { same--; toleft[d][i]++; val[d+1][lp++] = val[d][i]; // sum[d][i]+=val[d][i]; } else { val[d+1][rp++] = val[d][i]; } } } build(l, mid, d+1); build(mid+1, r, d+1); } int query(int a, int b, int k, int l, int r, int d) { if (a == b) return val[d][a]; int mid = (l + r) >> 1; int cnt=toleft[d][b]-toleft[d][a-1]; int ss=toleft[d][a-1]-toleft[d][l-1]; int ee=a-l-ss; int s=toleft[d][b]-toleft[d][a-1]; int e=b-a+1-s; if(cnt>=k) { if(e>0) { if(ee>0) ans+=sum[d+1][mid+e+ee]-sum[d+1][mid+ee]; else ans+=sum[d+1][mid+e]; } int newl=l+toleft[d][a-1]-toleft[d][l-1]; int newr=newl+cnt-1; return query(newl,newr,k,l,mid,d+1); } else { if(s>0) { if(ss>0) ans-=sum[d+1][l+ss+s-1]-sum[d+1][l+ss-1]; else ans-=sum[d+1][l+s-1]; } int newr=b+toleft[d][r]-toleft[d][b]; int newl=newr-(b-a-cnt); return query(newl,newr,k-cnt,mid+1,r,d+1); } } long long s[MAXN]; int main() { //freopen("cin.txt","r",stdin); cnt=1; scanf("%d",&t); while(t--) { printf("Case #%d:\n",cnt++); scanf("%d",&n); s[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&sorted[i]); val[0][i]=sorted[i]; s[i]=s[i-1]+sorted[i]; } sort(sorted+1,sorted+n+1); build(1,n,0); scanf("%d",&m); while(m--) { scanf("%d%d",&x,&y); x++;y++; lsum=lnum=0; //int ave=query(x,y,(y-x+2)/2,1,n,0); rnum=y-x+1-lnum; rsum=s[y]-s[x-1]-lsum; ans=0; int tt=query(x,y,(y-x)/2+1,1,n,0); if((y-x+1)%2==0) ans-=tt; printf("%I64d\n",ans); } puts(""); } return 0; }