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. error C2220: warning treated as error - no 'object' file generated解决方法

    error C2220: warning treated as error - no 'object' file generated 警讯视为错误 - 生成的对象文件 / WX告诉编译器将所有警告视为 ...

  2. 第二篇、Maven快速上手

    1.目标 该篇主要是为了快速利用maven来构建工程,maven作为项目管理的工具已经得到极大程度的应用,很多开源项目都用maven来构建.如何建立 一个maven工程,如何导入别人的maven工程, ...

  3. Unity的Profiler性能分析

    1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时间总 ...

  4. BZOJ 3240 矩阵游戏

    Description 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的\(n\)行\(m\)列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用\(F[i][j]\)来 ...

  5. 【Java】怎么回答java垃圾回收机制

    (1) GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃,Java提供的GC功能可以自动监测对象 ...

  6. [转]Windows Azure上安装SharePoint 2013

    基于Windows Azure 安装SharePoint 2013 前段时间写的基于Windows Azure安装SharePoint系列,由于Azure的体验账号过期了,所以不得不暂停.今天有幸参加 ...

  7. Black Box《优先队列》

    Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...

  8. Word Pattern

    ​package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @au ...

  9. 14.5.3 Locks Set by Different SQL Statements in InnoDB

    14.5.3 Locks Set by Different SQL Statements in InnoDB 通过不同的SQL语句设置的锁 在InnoDB中 一个锁定读, 一个UPDATE 或者一个D ...

  10. 从ulimit命令看socket的限制

      从ulimit命令看socket的限制 在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题,比如还有Squid做代理,当文 ...