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. 一种实现C++反射功能的想法(三)

    如何实现类型名跟类型的对应, 我们很容易想到map, 没错, 就是使用map实现的. std::map<std::string, .....>, 等下, 第二部分该填什么类型, 一个函数指 ...

  2. 修改原代码定制bootstrap

    1.下载对应的Bootstarap和node.js 注:less文件夹中包含了bootstrap中所有样式组件的less源代码: dist保存编译后的css和js等文件 2.命令行输入npm inst ...

  3. 每个Linux新手都应该记住的10个基本Linux命令

    Linux对我们的生活有着很大的影响.至少,你的安卓手机上面就有Linux内核.然而,头一次入手Linux只会让你觉得不适.因为在Linux上,你通常应该使用终端命令,而不是只要点击启动器图像(就像你 ...

  4. bzoj1563: [NOI2009]诗人小G

    Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...

  5. Unity3d 项目管理 版本管理

    Unity3d中项目管理     版本管理 如果在提交文件的时候发现提示有"先更新,再提交的"提示的时候,这是因为,A提交了一个版本文件,版本是13,那么你还在修改版本为12的文件 ...

  6. hdu 5151 Sit sit sit

    http://acm.hdu.edu.cn/showproblem.php?pid=5151 题意:一共有N个椅子,然后有N个学生依次去坐,满足下面三个条件不能坐上去,1:这个椅子旁边有左椅子也有右椅 ...

  7. 工业CF卡与商业CF卡对比

    工业CF卡:1.SLC FLASH芯片 .每个区块读写次数为10万次 2.可分区 识别为本地磁盘 3.平均写入技术.ECC自动校验技术 4.完全模拟硬盘引导系统开机,支持长期稳定工作 商业CF卡:1. ...

  8. 整整68页学习C++的文章

    有空看看,有不少好东西: http://dev.21tx.com/language/c/index.shtml

  9. Linxu安装Lamp环境

    安装MySQL数据库 sudo apt-get install mysql-server mysql-client 下图为提示输入数据库密码,然后回车,之后还有提示,再重复输入一次,再回车 最后安装完 ...

  10. 使用 libevent 和 libev 提高网络应用性能

    使用 libevent 和 libev 提高网络应用性能 Martin C. Brown, 作家, Freelance 简介: 构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件, ...