题目传送门

题意:n个数,两种操作,一是每个数字加x,二是查询& (1 << T) == 1 的个数

分析:因为累加是永远的,所以可以离线处理。树状数组点是c[16][M] 表示数字x%(1 << j) 后的数字pos,考虑第j位的个数。当询问时根据add不同的值不同的处理情况。

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 5;
const int R = (int) 1 << 16;
const int M = R + 10;
struct BIT {
int c[16][M];
void init(void) {
memset (c, 0, sizeof (c));
}
void updata(int b, int pos) {
pos++; //pos 可能等于0
while (pos < M) {
c[b][pos] += 1;
pos += pos & -pos;
}
}
int sum(int b, int pos) {
pos++;
int ret = 0;
while (pos > 0) {
ret += c[b][pos];
pos -= pos & -pos;
}
return ret;
}
}bit; int main(void) {
int n, cas = 0;
while (scanf ("%d", &n) == 1) {
if (n == -1) break;
bit.init ();
for (int x, i=0; i<n; ++i) {
scanf ("%d", &x);
for (int j=0; j<16; ++j) {
bit.updata (j, x % (1 << (j + 1)));
}
}
ll add = 0, ans = 0; char str[2];
while (scanf ("%s", &str) == 1) {
if (str[0] == 'E') break;
if (str[0] == 'C') { //离线
int x; scanf ("%d", &x);
add += x;
if (add >= R) add %= R;
}
else {
int t; scanf ("%d", &t);
int tail = add % (1 << t);
if (add & (1 << t)) { //(1<<t)位上已经有1
ans += bit.sum (t, (1 << t) - 1 - tail); //+tail 之前之后,都是0
ans += bit.sum (t, (1 << (t + 1)) - 1) - bit.sum (t, (1 << (t + 1)) - 1 - tail); //+tail 之前1,之后0
}
else {
ans += bit.sum (t, (1 << (t + 1)) - 1 - tail) - bit.sum (t, (1 << t) - 1 - tail); //+tail 之后1
}
}
}
printf ("Case %d: %lld\n", ++cas, ans);
} return 0;
}

  

树状数组 + 位运算 LA 4013 A Sequence of Numbers的更多相关文章

  1. 洛谷 P5057 [CQOI2006]简单题 (树状数组,位运算)

    题意:有一个长度为\(n\)的数组,进行\(m\)次操作,每次读入一个值\(t\),如果\(t=1\),则将区间\([l,r]\)的数字反转,若\(t=2\),则查询下标为\(i\)的值. 题解:树状 ...

  2. LA 4329 (树状数组) Ping pong

    第一次写树状数组,感觉那个lowbit位运算用的相当厉害. 因为-x相当于把x的二进制位取反然后整体再加上1,所以最右边的一个1以及末尾的0,取反加一以后不变. 比如1000取反是0111加一得到10 ...

  3. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  4. BZOJ4888 [Tjoi2017]异或和 FFT或树状数组+二进制拆位

    题面 戳这里 简要题解 做法一 因为所有数的和才100w,所以我们可以直接求出所有区间和. 直接把前缀和存到一个权值数组,再倒着存一遍,大力卷积一波. 这样做在bzoj目前还过不了,但是luogu开O ...

  5. 【poj2155】【Matrix】二位树状数组

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34310873 Description Given ...

  6. BZOJ 4017 小 Q 的无敌异或 ( 树状数组、区间异或和、区间异或和之和、按位计贡献思想 )

    题目链接 题意 : 中文题 分析 : 首先引入两篇写的很好的题解 题解一.题解二 听说这种和异或相关区间求和的问题都尽量按位考虑 首先第一问.按二进制位计贡献的话.那么对于第 k 位而言 其贡献 = ...

  7. LA 4329 ping-pong树状数组

    题目链接: 刘汝佳,大白书,P197. 枚举裁判的位置,当裁判为i时,可以有多少种选法,如果已经知道在位置i之前有ci个数比ai小,那么在位置i之前就有i-1-ci个数比ai大. 在位置i之后有di个 ...

  8. HDU4456-Crowd(坐标旋转+二位树状数组+离散化)

    转自:http://blog.csdn.net/sdj222555/article/details/10828607 大意就是给出一个矩阵 初始每个位置上的值都为0 然后有两种操作 一种是更改某个位置 ...

  9. LA 4329(树状数组)

    题目描述: N <tex2html_verbatim_mark>(3N20000) <tex2html_verbatim_mark>ping pong players live ...

随机推荐

  1. October 8th 2016 Week 41st Saturday

    When ambition ends, happiness begins. 野心消亡之日,正是快乐破茧之时. If I don't have the wish to be a useful man, ...

  2. August 12th 2016 Week 33rd Friday

    Everything is good in its season. 万物逢时皆美好. Every dog has its day. You are not in your best condition ...

  3. stdafx.h的作用

    // stdafx.h : include file for standard system include files,// or project specific include files th ...

  4. 模拟赛1030d2

    他[问题描述]一张长度为N的纸带, 我们可以从左至右编号为0 − N( 纸带最左端标号为0). 现在有M次操作, 每次将纸带沿着某个位置进行折叠, 问所有操作之后纸带的长度是多少.[输入格式]第一行两 ...

  5. 常用shell命令操作

    1.找出系统中所有的*.c 和*.h 文件 (-o 或者) $find / -name "*.cpp" -o -name "*.h" 2.设定 eth0 的 I ...

  6. 二维码相关工具Qrcode笔记

  7. NYOJ题目1162数字

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr8AAAJ/CAIAAAD+kywNAAAgAElEQVR4nO3dO1LjzN4H4G8T5CyE2A ...

  8. python中读取\写入CSV中数据的方法

  9. 【jquery】幻灯片效果

    闲着无聊,用Jquery写了一个幻灯片效果. 我这人喜欢造轮子,除了jquery这种有强大开发团队的框架级别JS,其实的一些小程序都是尽量自己写. 一是因为怕出问题了没人问,二是自己写的改起来也方便. ...

  10. [LeetCode] Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...