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 ...
随机推荐
- WebApi 登录身份验证
前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个功能复杂的业务应用系统,通过角色授权来控制用户访问,本文通过Form认证,Mvc的Controller基类及Action的权 ...
- .NET程序的编译和运行
程序的编译和运行,总得来说大体是:首先写好的程序是源代码,然后编译器编译为本地机器语言,最后在本地操作系统运行. 下图为传统代码编译运行过程: .NET的编译和运行过程与之类似,首先编写好的源代码,然 ...
- ADO.net中常用的对象介绍
ADO.NET的对象主要包括:DataSet,DataTable,DataColumn,DataRow,和DataRelation. DataSet:这个对象是一个集合对象,它可以包含任意数量的数据表 ...
- 获取 Windows Phone 手机系统信息
wpf: <phone:PhoneApplicationPage x:Class="ABSystemInfo.MainPage" xmlns="http://sch ...
- [javaSE] java获取文件列表
递归测试 import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Lis ...
- jxl导出Excel文件
一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; im ...
- Yii路径总结
如果是 // 就会默认去调 protected/views/layouts //代表 绝对路径 其实 就是 绝对和相对的关系 /代表相对路径,如module/user下的layout 用单斜杠的话默认 ...
- Maven多模块项目使用MyBatis Generator
开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...
- 中国快递包裹总量的预测-基于SARIMA模型
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- Dalvik VM和JVM 的比较
dx:dx工具用于将.class字节码(bytecode)转换为Android字节码(保存在.dex文件中)这个字节码文件 是给 Android 的 Java 虚拟机(Dalvik虚拟机)运行用的. ...