Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We'll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t.

You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.

Input

The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n = k = 0. Otherwise, 1 <= n <= 100000 and there follow n integers with absolute values <= 10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0 <= t <= 1000000000.

Output

For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.

Sample Input

5 1
-10 -5 0 5 10
3
10 2
-9 8 -7 6 -5 4 -3 2 -1 0
5 11
15 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 100
0 0

Sample Output

5 4 4
5 2 8
9 1 1
15 1 15
15 1 15

思路:维护前缀和,排序后维护双指针取值,细节直接看代码

using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int n, k; LL C[maxm]; void add(int x, int val) {
for(; x <= n; x += lowbit(x))
C[x] += val;
} LL getsum(int x) {
LL ret = ;
for(; x; x -= lowbit(x))
ret += C[x];
return ret;
} struct Node {
LL val;
int id;
bool operator<(const Node &a) const {
return val < a.val;
}
} Nodes[maxm]; int main() {
ios::sync_with_stdio(false), cin.tie();
int t, val;
while(cin >> n >> k && n+k) {
memset(C, , sizeof(C));
for(int i = ; i <= n; ++i) {
cin >> val;
add(i, val);
Nodes[i] = {getsum(i), i};
}
Nodes[].id = , Nodes[].val = ;
sort(Nodes,Nodes++n);
while(k--) {
int l = , r = , ansL, ansR;
LL ans, t, diff = 0x3f3f3f3f;
cin >> t;
while(l <= n && r <= n) {
LL val = Nodes[r].val - Nodes[l].val;
if(abs(val - t) < diff) {
ans = val;
diff = abs(val-t);
ansL = min(Nodes[l].id+,Nodes[r].id+);
ansR = max(Nodes[l].id, Nodes[r].id);
}
if(val > t)
l++;
else if(val < t)
r++;
else break;
if(l == r) r++;
}
cout << ans << " " << ansL << " " << ansR << "\n";
} } return ;
}

Day8 - G - Bound Found ZOJ - 1964的更多相关文章

  1. hdu4507

    数位dp,终于守得云开见月明了.建议初学者先试试两道比较简单的hdu2089,hdu3555. 鸣谢:http://blog.csdn.net/acm_cxlove/article/details/8 ...

  2. HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest

    赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...

  3. Solution -「多校联训」数学考试

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个函数,第 \(i\) 个有 \(f_i(x)=a_ix^3+b_ix^2+cx_i+d~(x\in[l_i, ...

  4. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  5. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  6. ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)

    题目链接  ZOJ Monthly, March 2018 Problem G 题意  给定一个字符串.现在求一个下标范围$[0, n - 1]$的$01$序列$f$.$f[x] = 1$表示存在一种 ...

  7. POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0

    http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...

  8. zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)

    题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...

  9. 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)

    Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...

随机推荐

  1. postman使用get请求的url地址传参中文乱码问题

    编码之后

  2. SpringBoot与Jpa自定义增删查改

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. pymongo 用户密码连接

    # db mongodbdb_mongo_attr = { 'host': '*', 'port': 27, 'database':'tease', 'username':'*v', 'passwor ...

  4. python学习笔记(3) -- 字符与数字之间的转换函数

    转载:python中的字符数字之间的转换函数 int(x [,base ])         将x转换为一个整数     long(x [,base ])        将x转换为一个长整数     ...

  5. kafka 高吞吐量的因素

    1.顺序的方式存储数据: 2.批量发送: 3.零拷贝: 来源:咕泡学院

  6. kafka 副本同步细节

    图片来源:咕泡学院

  7. gcd && exgcd算法

    目录 欧几里德算法与扩展欧几里德算法 1.欧几里德算法 2.扩展欧几里德算法 欧几里德算法与扩展欧几里德算法 1.欧几里德算法 #include<bits/stdc++.h> using ...

  8. 【PAT甲级】1018 Public Bike Management (30 分)(SPFA,DFS)

    题意: 输入四个正整数C,N,S,M(c<=100,n<=500),分别表示每个自行车站的最大容量,车站个数,此次行动的终点站以及接下来的M行输入即通路.接下来输入一行N个正整数表示每个自 ...

  9. 61二叉搜索树的第k个结点

    题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 思路:二叉搜索树的中序遍历是递增的序列,使用 ...

  10. sklearn.neighbors.KNeighborsClassifier(k近邻分类器)

    KNeighborsClassifier参数说明KNeighborsClassifier(n_neighbors=5, weights='uniform', algorithm='auto', lea ...