题目链接:uva 1406 - A Sequence of Numbers
题目大意;给定n个数,有两种操作:
- Q x:计算与2x取且不为0的数的个数
- C x:每个数加上x
输出所有Q操作的和。
解题思路:因为x最大为15,所以开16个树状数组,fenx[x]记录的是每个数取模2x+1的情况,然后有一个add值标记总共加了多少。根据add值确定原先数的范围。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lowbit(x) ((x)&(-x))
const int maxn = 1<<16;
const int maxr = 16;
int base[maxr+5], fenx[maxr+5][maxn+5];
int N, add;
int query_treeArr (int* bit, int x) {
int ret = 0;
while (x) {
ret += bit[x];
x -= lowbit(x);
}
return ret;
}
void insert_treeArr (int* bit, int x, int v) {
while (x <= maxn) {
bit[x] += v;
x += lowbit(x);
}
}
void init () {
add = 0;
for (int i = 0; i < maxr; i++)
memset(fenx, 0, sizeof(fenx));
int x;
for (int i = 0; i < N; i++) {
scanf("%d", &x);
for (int j = 1; j <= maxr; j++) {
insert_treeArr(fenx[j-1], x % base[j] + 1, 1);
}
}
}
long long solve () {
long long ret = 0;
int x;
char order[5];
while (scanf("%s", order) == 1 && strcmp(order, "E")) {
scanf("%d", &x);
if (order[0] == 'Q') {
int have = add % base[x];
if (add & base[x]) {
ret += query_treeArr(fenx[x], base[x] - have);
ret += query_treeArr(fenx[x], base[x+1]) - query_treeArr(fenx[x], base[x+1] - have);
} else
ret += query_treeArr(fenx[x], base[x+1] - have) - query_treeArr(fenx[x], base[x] - have);
} else
add = (add + x) % base[16];
}
return ret;
}
int main () {
int cas = 1;
base[0] = 1;
for (int i = 1; i <= maxr; i++)
base[i] = base[i-1] * 2;
while (scanf("%d", &N) == 1 && N != -1) {
init();
printf("Case %d: %lld\n", cas++, solve());
}
return 0;
}
分享到:
相关推荐
解决Invalid byte 1 of 1-byte UTF-8 sequence
State-of-the-art sequence labeling systems traditionally require large mounts of task-specific knowledge in the form of hand-crafted features and data pre-processing.In this paper, we introduce a ...
论文《End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF》的代码实现
Ember CLI 数组序列插件。 ember-cli-array-sequence公开了一个 子类,它将在其长度更新时生成一个数字序列。 例子 import ArraySequence from 'array-sequence' ; var seq = ArraySequence . create ( { offset...
Edward Grefenstette - Beyond Sequence to Sequence with Augmented RNNs
agreement between a pair of target-directional LSTMs, which generates more balanced targets. In addition, we develop two efficient approximate search methods for agreement that are empirically shown ...
基于循环神经网络和注意力机制的Sequence-to-Sequence模型神经网络方法在信息抽取和自动摘要生成方面发挥了重要作用。然而,该方法不能充分利用文本的语言特征信息,且生成结果中存在未登录词问题,从而影响文本摘要...
Durbin et al - Biological Sequence Analysis (CUP 2002) no OCR-----Durbin et al - Biological Sequence Analysis (CUP 2002) no OCR
机器学习之sequence to sequence learning。(Sequence Generation-----Hung-yi Lee 李宏毅.ppt)
前端开源库-gulp-run-sequencegulp run sequence,已弃用-请改用run sequence:https://npmjs.org/package/run-sequence
Label-Free-DNA-Sequence-Detection-through-FRET-from-a-Fluorescent
这是ten so r f lo w中sequence2sequence的那一章节,这里单独拿出来
Oracle创建自增字段方法-ORACLE SEQUENCE的简单介绍 很有用哦
matlab code for zadoff-chu sequence
CRNN论文
北大POJ1019-Number Sequence 解题报告+AC代码
041007-S6D0129-initial sequence-ver0.0.pdf
Performance analysis of an all-digital BPSK direct-sequence spread-spectrum IF receiver architecture1993.pdf
基于数据库的分布式发号器-viemall-sequence基于数据库的分布式发号器-viemall-sequence基于数据库的分布式发号器-viemall-sequence基于数据库的分布式发号器-viemall-sequence基于数据库的分布式发号器-viemall-...
恒河YOKOGAWA yokogawa FA-M3 Sequence CPU Network Functions for F3SP66-4S,F3SP67-6S user manualpdf,恒河YOKOGAWA yokogawa FA-M3 Sequence CPU Network Functions for F3SP66-4S,F3SP67-6S user manual