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
在x轴上按从小到大的顺序给出n个点的坐标,每次询问第i到第j个村庄内,在哪儿建一个post office,使这些村庄到post office的距离和最小。
思路:很显然,邮局应该建在这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 树状数组的更多相关文章

  1. 数据结构--树状数组(黑龙江省第八届大学生程序设计竞赛--post office)

    例题来源: 题目: 1468: Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered fr ...

  2. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  3. 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 ...

  4. POJ2828 Buy Tickets [树状数组,二分答案]

    题目传送门 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22611   Accepted: 110 ...

  5. codeforces 589G G. Hiring(树状数组+二分)

    题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...

  6. POJ2828 Buy Tickets 树状数组

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  7. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  8. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  9. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. html+css—two

    1.滚动字幕(不常用) 默认状态:<marquee>向左移动</marquee> //-- hspasc滚动区域height滚动字幕高度 <marquee height= ...

  2. IIS配置ASP.NET和服务器错误页

    以下两种方法均为全站出错处理 方法一: 1.在Web.config配置文件中<system.web></system.web>中添加<customErrors mode= ...

  3. IIS7.5支持解析读取.json文件数据

    在站点中添加 MIME类型去支持Json文件的解析 添加mime类型 文件扩展名:.json MIME类型:application/json 添加成功后即可. 如果不能直接操作iis也可以直接在web ...

  4. 分享一些WinForm数据库连接界面UI

    1.动软代码生成器源码中. 2.DevExpress控件包中有类似的界面 3.代码生成器:http://www.csharpwin.com/csharpspace/11666r2577.shtml 4 ...

  5. csharp: json to csharp

    http://json2csharp.com/ http://jsonclassgenerator.codeplex.com/ http://jsonutils.com/ JSON生成类文件 http ...

  6. Linux Shell系列教程之(十二)Shell until循环

    本文是Linux Shell系列教程的第(十二)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上两篇文章Linux Shell系列教程之(十)Shell for循环和Lin ...

  7. YII 的基本CURL操作

    Order::model()->updateAll(array('merchant_id'=>'bbb'), "id in ('140868169311','1408690584 ...

  8. Oracle计算时间差函数

    两个Date类型字段:START_DATE,END_DATE,计算这两个日期的时间差(分别以天,小时,分钟,秒,毫秒): 天: ROUND(TO_NUMBER(END_DATE - START_DAT ...

  9. Android Studio利用Gradle删除没有使用到的资源和代码文件

    一.打包时忽略无用资源 我们在打包的时候默认会把没有用到的资源(比如图片)也打包成app,徒增了应用的大小.现在我们可以利用Gradle来优雅的去除没有用到的资源文件了! 就是在gradle中配置sh ...

  10. Android Studio 使用小技巧

    1.如何打开设置界面? File --> Settings 快捷键  Ctrl + Alt  + s 2.修改Java文件字体大小 ? File --> Settings --> E ...