Topic Portal
Test instructions: N number, two operations, one is each number plus X, second is the number of query & (1 << T) = = 1
Analysis: Because the accumulation is always, so can be processed offline. The tree array point is c[16][m] that represents the number X (1 << J) after the digital POS, considering the number of the first J bits. When asked, depending on the value of the add different processing conditions.
#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 may be equal to 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 ') {//offline 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) bits already have 1 ans + = bit.sum (t, (1 << T)-1- tail); After +tail, both are 0 ans + = bit.sum (t, (1 << (T + 1))-1)-Bit.sum (T, (1 << (T + 1))-1- tail); +tail before 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;}
Tree-like array + bitwise operations LA 4013 A Sequence of Numbers