Codeforces Gym101502 F.Building Numbers-前缀和
3.0 s
256 MB
standard input
standard output
In this problem, you can build a new number starting from 1, by performing the following operations as much as you need:
- Add 1 to the current number.
- Add the current number to itself (i.e. multiply it by 2).
For example, you can build number 8 starting from 1 with three operations . Also, you can build number 10 starting from 1 with five operations
.
You are given an array a consisting of n integers, and q queries. Each query consisting of two integers l and r, such that the answer of each query is the total number of operations you need to preform to build all the numbers in the range from l to r (inclusive) from array a, such that each number ai (l ≤ i ≤ r) will be built with the minimum number of operations.
The first line contains an integer T (1 ≤ T ≤ 50), where T is the number of test cases.
The first line of each test case contains two integers n and q (1 ≤ n, q ≤ 105), where n is the size of the given array, and q is the number of queries.
The second line of each test case contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1018), giving the array a.
Then q lines follow, each line contains two integers l and r (1 ≤ l ≤ r ≤ n), giving the queries.
For each query, print a single line containing its answer.
1
5 3
4 7 11 8 10
4 5
1 5
3 3
7
18
5
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
In the first query, you need 3 operations to build number 8, and 4 operations to build number 10. So, the total number of operations is 7.
这个题要用前缀和处理一下,要不然会超时,数组开的时候注意一下,开小了,RE,很不幸,一开始没用前缀和,数组也开小了,都被卡了,(抱头痛哭)
代码:
1 //F. Building Numbers-前缀和处理,否则超时,数组开小了被卡了一手
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 typedef long long ll;
9 const int N=1e5+10;
10 ll ans[N];
11 ll solve(ll x){
12 int num=0;
13 while(x!=1){
14 if(x%2==0)x/=2;
15 else x--;
16 num++;
17 }
18 return num;
19 }
20 int main(){
21 int t;
22 scanf("%d",&t);
23 while(t--){
24 memset(ans,0,sizeof(ans));
25 int n,m,l,r;
26 scanf("%d%d",&n,&m);
27 ll x;
28 for(int i=1;i<=n;i++){
29 scanf("%lld",&x);
30 ans[i]=ans[i-1]+solve(x);
31 }
32 for(int i=1;i<=m;i++){
33 scanf("%d%d",&l,&r);
34 printf("%lld\n",ans[r]-ans[l-1]);
35 }
36 }
37 return 0;
38 }
Codeforces Gym101502 F.Building Numbers-前缀和的更多相关文章
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- Codeforces 449D Jzzhu and Numbers(高维前缀和)
[题目链接] http://codeforces.com/problemset/problem/449/D [题目大意] 给出一些数字,问其选出一些数字作or为0的方案数有多少 [题解] 题目等价于给 ...
- Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...
- Codeforces.449D.Jzzhu and Numbers(容斥 高维前缀和)
题目链接 \(Description\) 给定\(n\)个正整数\(a_i\).求有多少个子序列\(a_{i_1},a_{i_2},...,a_{i_k}\),满足\(a_{i_1},a_{i_2}, ...
- Codeforces 165 E. Compatible Numbers【子集前缀和】
LINK 题目大意 给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...
随机推荐
- 【nginx】nginx.sh nginx 安装脚本
#! /bin/shcd /usr/local/srcwget http://nginx.org/download/nginx-1.10.1.tar.gzecho 'download success' ...
- Spring Cloud构建微服务架构(三)消息总线
注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...
- leetcode-19-merge
88. Merge Sorted Array 解题思路: 需要注意,两个数组是排好序的,且nums1够大.所以从两个数组的尾端开始比较,大的那个放在nums1的尾部,并且放了之后就可以前进. 例如nu ...
- Solution: 最近公共祖先·一 [hiho一下 第十三周]
题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...
- 1036: [ZJOI2008]树的统计Count(树链剖分)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 19830 Solved: 8067[Submit ...
- web安全测试---AppScan扫描工具(转)
安全测试应该是测试中非常重要的一部分,但他常常最容易被忽视掉. 尽管国内经常出现各种安全事件,但没有真正的引起人们的注意.不管是开发还是测试都不太关注产品的安全.当然,这也不能怪我们苦B的“民工兄弟” ...
- 令人惊叹的Chrome浏览器插件
Chrome是一个简洁而又高效(高性能,高消耗)的浏览器.接下来让我吐血推荐一些常用的Chrome插件. 日常插件 uBlock Origin ----- 比Adblock性能更高的广告插件. Adk ...
- webdriver高级应用- 启动带有用户配置信息的firefox浏览器窗口
由于WebDriver启动FireFox浏览器时会启用全新的FireFox浏览器窗口,导致当前机器的FireFox浏览器已经配置的信息在测试中均无法生效,例如已经安装的浏览器插件.个人收藏夹等.为了解 ...
- Python全栈开发第二期课表
day01-python 全栈开发-基础篇 01 开课介绍 01:55:13 ★ 02 开课介绍02 01:28:31 ★ 03 开课介绍03 00:22:55 ...
- [译]pandas中的iloc loc的区别?
loc 从特定的 gets rows (or columns) with particular labels from the index. iloc gets rows (or columns) a ...