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 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...
随机推荐
- 使用jquery清除select中的所有option
html代码 <select id="search"> <option>baidu</option> <option>sogou&l ...
- svn提交报错,提示:locked,需要cleanup
版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/9285137.html 在使用SVN提交代码或更新代码时经常会 ...
- 学习路由器vue-router
vue-router:vue官方路由管理器. 功能:嵌套的路由/视图表模块化的.基于组件的路由配置路由参数.查询.通配符基于 Vue.js 过渡系统的视图过渡效果细粒度的导航控制带有自动激活的 CSS ...
- 不使用脚手架的 vue 应用
工作中的项目不止有页面繁多的模块化项目,还会只有一两个页面的类似于填写信息参与活动的活动页.这个时候,就可以回归以前的三剑客模式,在 index.html 里引用 vue.js 进行开发. 关键点: ...
- 【mysql】【转发】my.cnf 讲解
PS:本配置文件针对Dell R710,双至强E5620.16G内存的硬件配置.CentOS 5.6 64位系统,MySQL 5.5.x 稳定版.适用于日IP 50-100w,PV 100-300w的 ...
- Python爬虫,爬取实验楼全部课程
目的: 使用requests库以及xpath解析进行实验楼所有课程,存入MySQL数据 库中. 准备工作: 首先安装,requests库,lxml库,以及peewee库.在命令行模式,使用以下命令. ...
- jquery中arrt()和prop()的区别
在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法也几乎完全相同. 但不得不说的是,这两个函数的用处却并不相同.下面我们来详细介绍这两个函数之间的区别. ...
- gcc——预处理(预编译),编译,汇编,链接
一,预编译 操作步骤:gcc -E hello.c -o hello.i 主要作用: 处理关于 “#” 的指令 [1]删除#define,展开所有宏定义.例#define portnumber 333 ...
- CodeForces 567F DP Mausoleum
本着只贴代码不写分析的题解是在耍流氓的原则,还是决定写点分析. 思路很清晰,参考的官方题解,一下文字仅对题解做一个简要翻译. 题意: 有1~n这n个数,每个数用两次.构成一个长为2n的序列,而且要求序 ...
- 智能DNS解析之edns-client-subnet篇
摘要:智能DNS解析是CDN的重要组成部份,所谓的智能也就是根据请求用户来对同一域名作出相应不同解析(目前大多数域名注册商还没提供线路解析的服务),所以CDN的调度准确性也就完全依靠DNS智能解析,但 ...