划分树(多校联合)hdu3473

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author ID 
Password 
  Register new ID

Minimum Sum

Time 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
      
      
2 5 3 6 2 2 4 2 1 4 0 2 2 7 7 2 0 1 1 1
 

Sample Output
      
      
Case #1: 6 4 Case #2: 0 0


题意:对于给出的区间(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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值