Tree-like array + bitwise operations LA 4013 A Sequence of Numbers

Source: Internet
Author: User
Tags bitwise cas

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 <&lt ;    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&LT;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.