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
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  

pid=3474">3474 1828 3397 

pid=3333">3333 3472





被杭电的输出坑了 好久。。。。

printf lld 就WA 要I64d才行。。。。。真吭。!

易得使绝对值和最小就是中位数,能够參考坐标上的点到两点间距离之和最小的原理。

这道题让我对划分树的原理理解更加深刻了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
#define md(x, y) (((x)+(y))>>1)
const int maxn = 100000+10;
typedef long long LL;
int n,m;
int ls;
int num[maxn];
int seg[20][maxn];
int lftnum[20][maxn];
LL lfts[20][maxn];
LL presum[maxn];
LL lsum;
void build(int L,int R,int dep){
if(L==R)return;
int mid = md(L,R);
int key = num[mid];
int lcnt = mid-L+1;
for(int i = L; i <= R; i++){
if(seg[dep][i] < key)
lcnt--;
}
int lp = L,rp = mid+1;
for(int i = L; i <= R; i++){
if(i==L){
lftnum[dep][i] = 0;
lfts[dep][i] = 0;
}else{
lfts[dep][i] = lfts[dep][i-1];
lftnum[dep][i] = lftnum[dep][i-1];
}
if(seg[dep][i] < key){
lftnum[dep][i]++;
lfts[dep][i] += seg[dep][i];
seg[dep+1][lp++] = seg[dep][i];
}
else if(seg[dep][i] > key){
seg[dep+1][rp++] = seg[dep][i];
}
else{
if(lcnt>0){
lcnt--;
lftnum[dep][i]++;
lfts[dep][i] += seg[dep][i];
seg[dep+1][lp++] = seg[dep][i];
}else{
seg[dep+1][rp++] = seg[dep][i];
}
}
} build(L,mid,dep+1);
build(mid+1,R,dep+1);
} LL query(int L,int R,int ll,int rr,int dep,int k){
if(ll == rr)
return seg[dep][ll];
int mid = md(ll,rr);
int ncnt,ucnt;
LL tsum = 0;
if(L==ll){
ncnt = 0;
tsum = lfts[dep][R];
ucnt = lftnum[dep][R]-ncnt;
}else{
ncnt = lftnum[dep][L-1];
ucnt = lftnum[dep][R]-ncnt;
tsum = lfts[dep][R]-lfts[dep][L-1];
}
if(ucnt >= k){
L = ll + ncnt;
R = ll + ncnt + ucnt-1;
return query(L,R,ll,mid,dep+1,k);
}else{
int aa = L-ll-ncnt;
int bb = R-L-ucnt+1;
L = mid+aa+1;
R = mid+aa+bb;
ls += ucnt;
lsum += tsum;
return query(L,R,mid+1,rr,dep+1,k-ucnt);
}
}
int main(){ int ncase,T=1;
cin >>ncase;
while(ncase--){
scanf("%d",&n);
presum[0] = 0;
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
seg[0][i] = num[i];
presum[i] = presum[i-1]+num[i];
}
sort(num+1,num+n+1);
build(1,n,0);
scanf("%d",&m);
printf("Case #%d:\n",T++);
while(m--){
int a,b,k;
scanf("%d%d",&a,&b);
++a;++b;
k = (b-a)/2+1;
lsum = 0;
ls = 0;
int rs = 0;
int t = query(a,b,1,n,0,k);
LL rsum = presum[b]-presum[a-1]-t-lsum;
rs = b-a-ls;
LL ans = rsum-lsum+t*(ls-rs);
printf("%I64d\n",ans);
}
printf("\n");
}
return 0;
}

HDU-3473Minimum Sum的更多相关文章

  1. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  2. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  3. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

  4. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  5. HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

    Sum Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  6. HDU 5776 sum (模拟)

    sum 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5776 Description Given a sequence, you're asked ...

  7. hdu 5586 Sum 最大子段和

    Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5586 Desc ...

  8. hdu 5586 Sum【dp最大子段和】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 Sum Time Limit: 2000/1000 MS (Java/Others)    Me ...

  9. hdu 4432 Sum of divisors(十进制转其他进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4432 代码: #include<cstdio> #include<cstring&g ...

  10. hdu 4407 Sum

    http://acm.hdu.edu.cn/showproblem.php?pid=4407 题意:给定初始n个数1..n,两个操作,①1 x y p  询问第x个数到第y个数中与p互质的数的和; ② ...

随机推荐

  1. 给表格设置border还可以这样玩

    <table width="100%" border="0" cellpadding="0" cellspacing="1& ...

  2. SGU 133.Border

    水题不说了 #include <iostream> #include <cstring> #include <cstdio> #include <cmath& ...

  3. js string操作总结

    var str = "0123456789"; console.log(str.substring(0)); //------------"0123456789" ...

  4. Joomla插件汉化小程序

    这两天在搞joomla插件,在看peter的视频,在此谢过他了.看到它汉化插件那个视频.反正闲着无聊,就写了一个Java小程序,方便使用joomla的人汉化插件.这个程序的方法很简单,你只要先运行ou ...

  5. Day12(补充) Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  6. bzoj3140: [Hnoi2013]消毒

    Description 最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为a*b*c,a.b.c 均为正整数.为了实验的方便,它被划分为a*b*c ...

  7. DevOps - Development And Operations

    简介: 研发运维一体化 相关资料: 关于DevOps你必须知道的11件事 我眼中的DevOps DevOps 门户 docker for dotnet系列 docker4dotnet #1 前世今生 ...

  8. java分页查询

    很多数据库自身都提供了分页机制,如SQL Server中提供的top关键字,MySQL数据库中提供的limit关键字,它们都可以设置数据返回的记录数. 通过各种数据库的分页机制实现分页查询,其优点是减 ...

  9. 一键安装mysql5.6

    #!/bin/bash # @Name:install_mysql.sh # @Author:Eivllom # @Create -- # @Modify -- app_soft="/app ...

  10. 解决weblogic Managed Server启动非常慢的情况

    jdk版本:1.7.0_79 查看控制台日志停留在如下地方: . . JAVA Memory arguments: -Xms2048m -Xmx4096m -XX:MaxPermSize=512m . ...