Post-office 树状数组
Post office
题目描述
There are N(N<=1000) villages along a straight road, numbered from 1 to N for simplicity. We know exactly the position of every one (noted pos[i],pos[i] is positive integer and pos[i]<=10^8). The local authority wants to build a
post office for the people living in the range i to j(inclusive). He wants to make the sum of |pos[k]-position_of_postoffice| (i<=k<=j) is minimum.
输入
For each test case, the first line is n. Then n integer, representing the position of every village and in acending order. Then a integer q (q<=200000), representing the queries. Following q lines, every line consists of two integers
i and j. the input file is end with EOF. Total number of test case is no more than 10.
Be careful, the position of two villages may be the same.
输出
For every query of each test case, you tell the minimum sum.
样例输入
3
1 2 3
2
1 3
2 3
样例输出
2
1
思路:很显然,邮局应该建在这j-i+1个村庄的最中间村庄,对区间距离求和我们使用树状数组!
代码如下:
#include "stdio.h"
#include "string.h" long long sum[]; long long Low(long long x)
{
return x&(-x);
} long long SUM(long long x) //树状数组求区间和
{
long long ans = ;
for(long long i=x; i>; i-=Low(i))
ans += sum[i];
return ans;
} int main()
{
long long n;
long long i,j,k;
long long x,y,Q;
long long mid;
while(scanf("%lld",&n)!=EOF)
{
memset(sum,,sizeof(sum));
for(i=; i<=n; ++i)
{
scanf("%lld",&k);
for(j=i; j<=n; j += Low(j))
sum[j] += k;
}
scanf("%lld",&Q);
while(Q--)
{
scanf("%lld %lld",&x,&y);
mid = x+(y-x+)/;
if((y-x+)%==)
printf("%lld\n",SUM(y)-SUM(mid-)-(SUM(mid-)-SUM(x-)));
else
printf("%lld\n",SUM(y)-SUM(mid)-(SUM(mid-)-SUM(x-)));
}
}
return ;
}
Post-office 树状数组的更多相关文章
- 数据结构--树状数组(黑龙江省第八届大学生程序设计竞赛--post office)
例题来源: 题目: 1468: Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered fr ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- POJ2828 Buy Tickets [树状数组,二分答案]
题目传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22611 Accepted: 110 ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- POJ2828 Buy Tickets 树状数组
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- 详细讲解PHP中缓存技术的应用
PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站,在速度上有绝对的优势,想mssql转6万条数据php如需要40秒,asp不下2 ...
- OAuth2.0 基础概述
web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...
- ahjesus js 快速求幂
/* 快速幂计算,传统计算方式如果幂次是100就要循环100遍求值 快速幂计算只需要循环7次即可 求x的y次方 x^y可以做如下分解 把y转换为2进制,设第n位的值为i,计算第n位的权为x^(2^(n ...
- XStream的例子
写个小例子,方便以后复习: 1 package com.demo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 impor ...
- CRM行编辑控件
原创,转载请说明出处 王红福 http://www.cnblogs.com/hellohongfu/p/4792452.html CRM 本身的表格可以根据定义显示列信息,但是出于性能考虑不能详细的展 ...
- Sharepoint 2013 关于"SPChange"简介
在SharePoint中,我们经常会需要获取那些改变的项目,其实api为我们提供了SPChange对象,下面,我们通过列表简单介绍下这一对象. 1.创建一个测试列表,名字叫做“SPChangeItem ...
- [Java] JDK 系统环境变量设置 bat
@echo off set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environmen ...
- redis实现主从复制-单机测试
一.redis实现主从复制-单机测试1.安装redis tar -zxvf redis-2.8.4.tar.gzcd redis-2.8.4make && make install2. ...
- maven eclipse 插件下载地址
要用的时候,搜索了半天,自己记录下 单独下载地址 http://maven.apache.org/download.cgi eclipse 更新地址 http://download.eclipse.o ...
- File类的常用方法
public static void GetFileInfo() { File file=new File("e:","two.txt ...