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 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...
随机推荐
- 批量ping IP并检测IP延迟率和丢包率脚本
脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...
- MySQL 创建函数失败提示1418
MySQL 创建函数失败提示1418 在创建函数时,往往会遇到创建函数失败的情形,除去书写的创建函数的sql语句本身语法错误之外,还会碰到一个错误就是, 1418:This function has ...
- 在ArchLinux、manjaro中安装MySql(mariaDB)
安装MySql数据库.但是在MySql被Oracle收购之后,很多开源支持者就转而使用MariaDb了.不过MariaDb也和MySql兼容的,所以基本不用有什么担心.由于ArchLinux只带了Ma ...
- jenkins 之 Android 打包及上传至蒲公英
准备条件 iMAC,非必须(如果是 安卓 和 苹果 可以在同一台电脑上打包则要 Mac OS 系统的电脑,如果是只是给安卓打包 windows 电脑也是可以的, window 下 需要把 ls 换成 ...
- Zend Framework 2中如何使用Service Manager
end Framework 2 使用ServiceManager(简称SM)来实现控制反转(IoC).有很多资料介绍了service managers的背景,我推荐大家看看this blog post ...
- python常用方法总结
1.os模块的路径拼接: import os now_path=os.path.abspath(__file__)#当前运行文件的路径 print(now_path) uppeer_path=os.p ...
- 对python的想法
作为计算机专业的学生,在编程语言之余,我认为掌握一门脚本语言是很必要的.尤其是现在在数据分析,AI,机器学习等各个方面都大放异彩的python.相比于之前接触过的Java,C,C++乃至于php等语言 ...
- csa Round #70
Digit Holes Time limit: 1000 msMemory limit: 256 MB When writing digits, some of them are consider ...
- Java判断浏览器是微信还是支付宝
private static final String WX_AGENT = "micromessenger"; private static final String ALI_A ...
- java环境配置classpath和path变量的作用及设置方法
1.path:指定cmd中命令执行文件所在的路径.比如javac.java两个可执行文件在jdk的bin目录下,如果path值含有这个bin目录,在cmd下执行这两个命令的时候就会到path指定的目录 ...