HDU 3473-Minimum Sum(划分树-求区间sigma最小值)

Minimum Sum

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3710    Accepted Submission(s): 852

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

Author

standy

Source

2010 ACM-ICPC Multi-University Training
Contest(4)——Host by UESTC

Recommend

zhengfeng   |   We have carefully selected several similar problems for you:  3474 1828 1698 1540 1394

题目意思:

有一组数,求区间l~r内的一个数,使得sigma值最小。

解题思路:

划分树找到中位数ave,建树的时候用lsum数组保存分到左子树中的数的和。

对于lsum数组的生成说明如下:

举个栗子:

求解ans时可以分成求中位数左边和+中位数右边和。

具体可以看注释。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100050
using namespace std;

int sorted[N];   //排序完的数组
int toleft[30][N]; //toleft[i][j]表示第i层从1到k有多少个数分入左边
int tree[30][N];  //表示每层每个位置的值
int n;//输入的数的个数
long long sum[N];//保存包括当前位置及之前的所有元素之和
long long lsum[30][N];//在deep层 第i个元素在的区间中 在i前面被划分到左子树的元素之和
long long tsum;//表示小于中位数ave部分的和

void building(int l,int r,int dep)
{
    if(l==r) return;
    int mid = (l+r)>>1;
    int temp = sorted[mid];
    int i,sum_same=mid-l+1;//表示等于中间值而且被分入左边的个数
    for(i=l; i<=r; i++)
        if(tree[dep][i]<temp)
            sum_same--;
    int leftpos = l;
    int rightpos = mid+1;
    for(i=l; i<=r; i++)
    {
        if(tree[dep][i]<temp)//比中间的数小,分入左边
        {
            tree[dep+1][leftpos++]=tree[dep][i];
            lsum[dep][i] = lsum[dep][i-1] + tree[dep][i];//记录被划分到左边的和是多少
        }
        else if(tree[dep][i]==temp&&sum_same>0)//等于中间的数值,分入左边,直到sum==0后分到右边
        {
            tree[dep+1][leftpos++]=tree[dep][i];
            lsum[dep][i] = lsum[dep][i-1] + tree[dep][i];
            sum_same--;
        }
        else //右边
        {
            tree[dep+1][rightpos++]=tree[dep][i];
            lsum[dep][i] = lsum[dep][i-1];
        }
        toleft[dep][i] = toleft[dep][l-1] + leftpos - l;   //从1到i放左边的个数
    }
    building(l,mid,dep+1);
    building(mid+1,r,dep+1);
}

//查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
int query(int L,int R,int l,int r,int dep,int k)
{
    if(l==r) return tree[dep][l];
    int mid = (L+R)>>1;
    int cnt = toleft[dep][r] - toleft[dep][l-1]; //[l,r]中位于左边的个数
    if(cnt>=k)
    {
        int newl = L + toleft[dep][l-1] - toleft[dep][L-1]; //L+要查询的区间前被放在左边的个数
        int newr = newl + cnt - 1;  //左端点加上查询区间会被放在左边的个数
        return query(L,mid,newl,newr,dep+1,k);
    }
    else
    {
        int newr = r + (toleft[dep][R] - toleft[dep][r]);
        int newl = newr - (r-l-cnt);
        tsum+=lsum[dep][r]-lsum[dep][l-1];//区间
    }
}

int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int i;
        for(i=1; i<=n; i++)
        {
            scanf("%lld",&sorted[i]);
            sum[i] = sum[i-1]+sorted[i];//保存包括当前位置及之前的所有元素之和
            tree[0][i]=sorted[i] ;
        }
        sort(sorted+1,sorted+1+n);
        building(1,n,0);
        int l,r;
        int m;//查询次数
        scanf("%d",&m);
        printf("Case #%d:\n",cas++);
        while(m--)
        {
            scanf("%d%d",&l,&r);
            l++,r++;
            tsum=0;//表示小于中位数ave部分的和
            int k=(r-l)/2+1;//k是区间ab的中位数
            long long ave= query(1,n,l,r,0,k);//ave是中位数的值,r-l+1-k是左边数的个数
            long long ans=sum[r]-sum[l-1]-ave*(r-l+1-k)-tsum-ave;//中位数右边和,大于ave的部分
            ans+=(k-1)*ave-tsum;//中位数左边和,小于ave的部分
            printf("%lld\n",ans);
        }
        printf("\n");
    }
    return 0;
}

时间: 2024-08-07 21:19:18

HDU 3473-Minimum Sum(划分树-求区间sigma最小值)的相关文章

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g

hdu 3473 Minimum Sum 划分树的应用

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3473 Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3427    Accepted Submission(s): 787 Problem Description You are given N positive i

HDU 3473 Minimum Sum 划分树,数据结构 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=3473 划分树模板题目,需要注意的是划分树的k是由1开始的 划分树: 参考:http://blog.csdn.net/shiqi_614/article/details/8041390 划分树的定义 划分树定义为,它的每一个节点保存区间[lft,rht]所有元素,元素顺序与原数组(输入)相同,但是,两个子树的元素为该节点所有元素排序后(rht-lft+1)/2个进入左子树,其余的到右子树,同时维护一个num域,

HDU 3473 Minimum Sum 划分树

题目大意:给定一个序列,每次询问给出一个区间,我们需要选择一个数,这个数到区间内所有数的距离之和最小,求最小和 由绝对值不等式可得 当我们选择的这个数是中位数的时候距离和最小 于是这题就转换成了区间第k小 但是这题求的是最小和 于是我们做一个处理 我们多维护一个sum域 sum[i]表示[l,i]区间内划分到左子树中元素的总和 然后我们每次查询第k小时 如果我们进入的是右子树 就把划分到左子树中的元素和累加到left_sum上 然后用前缀和计算出区间的和 计算出right_sum 最后的结果就是

[csu/coj 1080]划分树求区间前k大数和

题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数和. 划分树: [1  6  3  8  5  4  7  2] [6  8  5  7][1  3  4  2] [8  7][6  5][3  4][1  2] [8][7][6][5][4][3][2][1] 把快排的结果从上至下依次放入线段树,就构成了划分树,划分的意思就是选定一个数,把原序

hdu 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=2665 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number of the test cases. For each test case, the first line contain two integer n and m (

poj 2401 划分树 求区间第k大的数

题目:http://poj.org/problem?id=2104 划分树待我好好理解下再写个教程吧,觉得网上的内容一般,,, 模板题: 贴代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define CLR(a) memset(a,0,sizeof(a)) const int MAXN = 1000

hdu 3473 Minimum Sum 再来一波划分树,对划分树累觉不爱。

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3084    Accepted Submission(s): 710 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you

HDU 3473 Minimum Sum

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2667    Accepted Submission(s): 609 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you