您的位置:首页 > 其它

划分树(多校联合)hdu3473

2014-10-23 20:53 141 查看




Online Judge
Online Exercise
Online Teaching
Online Contests
Exercise Author

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

【比赛提醒】BestCoder 你报名了吗?(点击报名)

【科普】什么是BestCoder?如何参加?

NEW~ 关于举办计算机学院大学生程序设计竞赛(2014'11)的报名通知



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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: