Title Link: http://www.lydsy.com:808/JudgeOnline/problem.php?id=2568
Test instructions: Maintains a collection s that supports the following operations:
(1) INS m: inserting the element m into the set S;
(2) DEL M: Removes all elements equal to M in the set S;
(3) Add M: Adds the value m to all elements in the set S;
(4) Qbit K: The number of elements in the query collection satisfies the second binary's K-bit is 1.
Idea: I'm looking at this.
https://github.com/strongoier/OJ/tree/master/BZOJ/2568%20%E6%AF%94%E7%89%B9%E9%9B%86%E5%90%88
The general idea of the original is as follows:
(1) The ADD operation of the one and separate out, set to sum, the set S of each element x the actual value is x+sum;
(2) Set F[k][t] indicates that the K bit is 1, and this number is less than the number of digits equal to D. Each query, set l=2^x,r=2^ (x+1)-1, the answer is F[K][R]-F[K][L-1];
(3) This f value is a segment change due to the addition of a number, so a tree array is used to maintain the F-array;
(4) for the number of inserted numbers and how many each number, with a map record, so that the deletion will know how much to subtract in the tree array.
I had a problem that I did not understand, because the actual number to insert when inserting x is X-sum, then x-sum is negative when this bit is not the same as a positive digit. The binary representation of a negative number is the binary representation of the corresponding positive number and the negation plus 1.
Like what
-1=11111111 11111111 11111111 11111111
-2=11111111 11111111 11111111 11111110
-3=11111111 11111111 11111111 11111101
-4=11111111 11111111 11111111 11111100
The insertion of the great God above is directly inserted
There's a 1 in the back because the subscript in the tree array starts at 1.
And then the sum is like this.
This is divided into two parts, the first part: Calculate the [l,r] interval, set k=2, then the binary representation l=100,r=111. Set sum=1011, then the actual interval to be calculated is [001,100], as long as a number of the last three bits in this interval, that is [001,100], then it plus sum after the three will fall to the [l,r] interval. In fact, this is not carry.
We set the sum=1110 again, the other constant, then the actual summation interval above becomes [000,001]. We found that, in addition to this interval, [110,111] This range is also possible. This is actually a carry, after the sum interval from [100,111] to [1100,1111], so minus the sum of the next three bits 110 the actual interval is [110,1001], we found that 1001,1000 will not have this value, so the actual is [110,111]. This is the second part of the summation above.
Then a negative number plus sum can also reach this interval, sum=1110,[-10,-7], the binary of these negative numbers is
-10=11111111 11111111 11111111 11110110
-9 = 11111111 11111111 11111111 11110111
-8 = 11111111 11111111 11111111 11111000
-7 = 11111111 11111111 11111111 11111001
We found that the latter three were in the two intervals of the calculation. So negative numbers don't need extra consideration.
int S[20][n];map<int,int> MP; int n; void Add (int k,int X,int t) {while (x<n) s[k][x]+=t,x+=x&-x;} int get (int k,int x) {int ans=0; while (x) ans+=s[k][x],x-=x&-x; return ans;} int main () {n=myint (); int sum=0; while (n--) {char op[10]; int x; scanf ("%s%d", op,&x); if (' A ' ==op[0]) sum+=x; else if (' I ' ==op[0]) {x-=sum; mp[x]++; for (int i=0;i<16;i++) Add (I, (x& ((1<< (i+1))-1)) +1,1); } else if (' D ' ==op[0]) {x-=sum; int t=mp[x]; mp[x]=0; for (int i=0;i<16;i++) Add (I, (x& ((1<< (i+1))-1) +1,-t); } else if (' Q ' ==op[0]) {int ans=0; int l=1<<x,r= (1<< (x+1))-1; Ans+=get (X,min (1<<16,max (0,r-(sum& (1<<)-1)))); Ans-=get (X,min (1<<16,max (0,l-(sum& (1<<)-1)))); l|=1<< (X+1); r|=1<< (x+1); Ans+=get (X,min (1<<16,max (0,r-(sum& (1<<)-1)))); Ans-=get (X,min (1<<16,max (0,l-(sum& (1<<)-1)))); printf ("%d\n", ans); } }}
Bzoj 2568-bit collection