hdu 5975---Aninteresting game(树状数组)
When we add i,we must create a new set, and put iinto it.And meanwhile we have to bring [i-lowbit(i)+1,i-1] from their original sets, and put them into the new set,too.When we put one integer into a set,it costs us one unit physical strength. But bringing integer from old set does not cost any physical strength.
After we add 1,2...n,we have q queries now.There are two different kinds of query:
1 L R:query the cost of strength after we add all of [L,R](1≤L≤R≤n)
2 x:query the units of strength we cost for putting x(1≤x≤n) into some sets.
For each case,the first line contains two integers n and q.Then q lines follow.Each line contains one query.The form of query has been shown above.
n≤10^18,q≤10^5
lowbit(i) =i&(-i).It means the size of the lowest nonzero bits in binary of i. For example, 610=1102, lowbit(6) =102= 210
When we add 8,we should bring [1,7] and 8 into new set.
When we add 9,we should bring [9,8] (empty) and 9 into new set.
So the first answer is 8+1=9.
When we add 6 and 8,we should put 6 into new sets.
So the second answer is 2.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
LL lowbit(LL x)
{
return x&(-x);
}
LL query(LL x,LL n)
{
LL ans=;
while(x<=n)
{
ans++;
x+=lowbit(x);
}
return ans;
}
LL cal(LL x)
{
LL ans=;
LL tmp=;
for(LL i=; tmp<=x; i++)
ans+=(x/(tmp)-x/(tmp<<))*tmp,tmp<<=;
return ans;
}
int main()
{
LL n,q;
while(scanf("%lld%lld",&n,&q)!=EOF)
{
while(q--)
{
int op;
scanf("%d",&op);
if(op==)
{
LL x,y;
scanf("%lld%lld",&x,&y);
LL ans=cal(y)-cal(x-);
printf("%lld\n",ans);
}
else
{
LL x;
scanf("%lld",&x);
LL ans=query(x,n);
printf("%lld\n",ans);
}
}
}
return ;
}
hdu 5975---Aninteresting game(树状数组)的更多相关文章
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- HDU - 1541 Stars 【树状数组】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
- HDU 3854 Glorious Array(树状数组)
题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 5101 Select --离散化+树状数组
题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...
- HDU 3584 Cube --三维树状数组
题意:给一个三维数组n*n*n,初始都为0,每次有两个操作: 1. 翻转(x1,y1,z1) -> (x2,y2,z2) 0. 查询A[x][y][z] (A为该数组) 解法:树状数组维护操作次 ...
随机推荐
- 自动获取代理IP信息的例子,含代码,分享哦,
/// <summary> /// 读取URL数据内容 /// </summary> /// <param name="url">网址</ ...
- 关于centos 7 systemctl自定义服务笔记
通过添加 Restart=always RestartSec=2s StartLimitBurst=10 实现systemctl服务崩溃自动重启
- less的基本操作
less的四大特性及示例 1.特性一(变量) less写法 @color:#ffffff: body{background-color:@color:} 生成的css body{background- ...
- 以防忘记,建立stm32工程的过程
然后会弹出manage run -time environment的运行环境界面,必须选择的有CMSIS下的core和device下的startup,至于其它的根据你需要的外设, 在stdperiph ...
- java OJ题目判断输入结束(与C语言的EOF结束等价)
/* * java 作Oj题目是会有输入若干数据的情况,不好判断输入结束符, * 类似于C语言中的EOF符号 * 在这里提供了一种方法 * */ import java.util.Iterator; ...
- 初识Android触摸事件传递机制
前言 今天总结的一个知识点是Andorid中View事件传递机制,也是核心知识点,相信很多开发者在面对这个问题时候会觉得困惑,另外,View的另外一个难题滑动冲突,比如在ScrollView中嵌套Li ...
- Angularjs快速入门(四)-css类和样式
例子: .error{background-color:red;} .warning{background-color:yellow;} <div ng-controller='HeaderCo ...
- ORB_SLAM2之Pangolin的安装与问题处理
前言 本篇博客中,我们主要介绍了在安装ORB_SLAM2所需的第三方库Pangolin的过程中遇到的一些问题及解决方法. 1.Pangolin是什么? Pangolin是一个用于OpenGL显示/交互 ...
- Charles抓取https请求详解
大家好,我是TT,互联网测试行业多年,没有牛逼的背景,也没有什么可炫耀的,唯独比他人更努力,在职场打拼.遇到过的坑,走过的弯路,愿意与大家分享,分享自己的经验,少走弯路.首发于个人公众号[测试架构师] ...
- React入门---属性(props)-8
Props 和 State对于组件Component是非常重要的两个属性. 区别:State对于模块来说是 自身属性: Props对于模块来说是 外来属性: 同样的,props也是只作用于当前的组 ...