| ||||||||||
Minimum SumTime Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2960 Accepted Submission(s): 685
Problem Description
You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you some intervals [l, r]. For each interval, you need to find a number x to make
as small as possible!
Input
The first line is an integer T (T <= 10), indicating the number of test cases. For each test case, an integer N (1 <= N <= 100,000) comes first. Then comes N positive integers x (1 <= x <= 1,000, 000,000) in the next line. Finally, comes an integer Q (1 <= Q <= 100,000), indicting there are Q queries. Each query consists of two integers l, r (0 <= l <= r < N), meaning the interval you should deal with.
Output
For the k-th test case, first output “Case #k:” in a separate line. Then output Q lines, each line is the minimum value of
. Output a blank line after every test case.
Sample Input
Sample Output
|
题意:对于给出的区间(l,r),找出一个数x,使得公式的值最小。
思路:肯定是找出中位数,也就是区间第(l+r)/2大的数,套划分树模板,询问的时候维护答案
a,b,c,d,x,e,f,g,h
那么答案就是(e+f+g+h)-(a+b+c+d)
所以就很好维护了,最后注意,如果有两个中位数的时候答案要减去一个
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
int N,Q,a[maxn];
LL ans;
int L,R;
struct P_TREE
{
int n;
int order[maxn];
int val[21][maxn],num[21][maxn];
LL sum[21][maxn];
void init()
{
n=N;
for(int i=1;i<=N;i++)order[i]=a[i];
memset(sum,0,sizeof(sum));
memset(val,0,sizeof(val));
for(int i=0;i<21;i++)val[i][0]=num[i][0]=0;
for(int i=1;i<=n;i++)val[0][i]=order[i];
sort(order+1,order+1+n);
build(0,1,n);
}
void build(int ind,int l,int r)
{
if(l==r)
{
sum[ind][l]=sum[ind][l-1]+val[ind][l];
return;
}
int mid=(l+r)>>1;
int ln=l,rn=mid+1,same=mid-l+1;
for(int i=l;i<=r;i++)
{
if(val[ind][i]<order[mid])same--;
sum[ind][i]+=sum[ind][i-1]+val[ind][i];
}
for(int i=l;i<=r;i++)
{
int flag=0;
if(val[ind][i]<order[mid]||(val[ind][i]==order[mid]&&same))
{
flag=1;
val[ind+1][ln++]=val[ind][i];
if(val[ind][i]==order[mid])same--;
}
else val[ind+1][rn++]=val[ind][i];
num[ind][i]=num[ind][i-1]+flag;
}
build(ind+1,l,mid);
build(ind+1,mid+1,r);
}
int query(int ind,int l,int r,int q1,int q2,int k)
{
if(q1==q2) return val[ind][q1];
int mid=(l+r)>>1;
int lx=num[ind][q1-1]-num[ind][l-1];
int ly=num[ind][q2]-num[ind][q1-1];
int rx=q1-l-lx;
int ry=q2-q1+1-ly;
int st=mid+1+rx;
int en=mid+1+rx+ry-1;
if(ly>=k)
{
ans+=sum[ind+1][ry+rx+mid]-sum[ind+1][rx+mid];
return query(ind+1,l,mid,l+lx,l+lx+ly-1,k);
}
else
{
ans-=sum[ind+1][l+lx+ly-1]-sum[ind+1][l+lx-1];
return query(ind+1,mid+1,r,st,en,k-ly);
}
}
}tree;
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(int i=1;i<=N;i++)scanf("%d",&a[i]);
tree.init();
scanf("%d",&Q);
printf("Case #%d:\n",cas++);
while(Q--)
{
scanf("%d%d",&L,&R);
L++,R++;
ans=0;
int tmp=tree.query(0,1,N,L,R,(L+R)/2-L+1);
if((L+R)%2)ans-=tmp;
printf("%I64d\n",ans);
}
printf("\n");
}
return 0;
}