HDU-3473Minimum Sum

as possible!
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.

case.
2 5
3 6 2 2 4
2
1 4
0 2 2
7 7
2
0 1
1 1
Case #1:
6
4 Case #2:
0
0
pid=3474">3474
1828 3397pid=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的更多相关文章
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
Sum Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 5776 sum (模拟)
sum 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5776 Description Given a sequence, you're asked ...
- hdu 5586 Sum 最大子段和
Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5586 Desc ...
- hdu 5586 Sum【dp最大子段和】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5586 Sum Time Limit: 2000/1000 MS (Java/Others) Me ...
- hdu 4432 Sum of divisors(十进制转其他进制)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4432 代码: #include<cstdio> #include<cstring&g ...
- hdu 4407 Sum
http://acm.hdu.edu.cn/showproblem.php?pid=4407 题意:给定初始n个数1..n,两个操作,①1 x y p 询问第x个数到第y个数中与p互质的数的和; ② ...
随机推荐
- php 备份数据库脚本
<?php// 备份数据库$host = "localhost";$user = "root"; //数据库账号$password = "123 ...
- linux之uniq
Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...
- 13号中断 int 13(转)
第一部分 简 介 1,1 一. 硬盘结构简介 1. 硬盘参数释疑 到目前为止, 人们常说的 ...
- ajax 文件上传,ajax
ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...
- ThinkPHP框架下,jq实现在div中添加标签并且div的大小会随之变化
php初学者,有什么不对的还请指正. 首先是在html页面中用jq实现添加标签:divAchivePersonnal是select所在的div的外层div,divselectAchivePersonn ...
- VS2010 release 和 debug 调试区别
VC下Debug和Release区别 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下又无法进行调试,于是只能采用printf方式逐步 ...
- delphi 2010 动态链接库DLL断点调试
DELPHI 2010 动态链接库DLL断点调试 马根峰 (广东联合电子服务股份有限公司,广州 510300) 摘要:本文详细介绍了Delphi 2010中的动态链接库DLL断点调试技术 关键词:DE ...
- for循环,列表和格式化输出
一:for 循环 1. 简单的说如果让你输出1到100之间的整数,用while该怎么实现呢? i= : print(i) i+= 看着是不是只有4行,但是有没有更加简单的办法,不妨我 ...
- Python面向对象OOP
一 OOP 与C++和Java一样,Python同样具有OOP设计. 过程式:从前到后,一条一条,机器能接受的顺序性方式:方式大概为"首先你应该做什么,第二应该做什么,高级点的做点假 ...
- GC的前世与今生
GC的前世与今生 虽然本文是以.net作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...