1. Bitwise Operations
1.1 Background
When performing bit operations, we can view numbers as binary, and bitwise operations operate on the value at specific positions in the number.
1.2 Example problems
There are two common methods for finding the number of 1s in a number.
- When
1<<i & x = 1, the i-th bit is 1 lowbit(x) = x & -xto find the least significant 1
Reference code:
int lowbit(int x){ return x & -x;}void solve(){ for(int i=0;i<n;i++){ cin>>t; int cnt = 0; for(;t;t -= lowbit(t)) cnt++; cout<<cnt<<' '; }}2. Discretization
2.1 Background
When the obtained data are sparsely distributed over a wide range, to save space and speed up data searching, we use discretization to process the data.
2.2 Example problems
For query problems, we can obtain it in O(1) via prefix sums, so the key of this problem lies mainly in data discretization.
It can be seen that on the number line the numbers we need to use are the insertion position x, and the left and right query ranges l,r — a total of three numbers. Put these three numbers into the discretized array and maintain their mapping to the original array positions.
Reference code:
void solve(){ cin>>n>>m; for(int i=0;i<n;i++){ cin>>x>>c; if(a[x]) a[x] += c; else a[x] = c; all.push_back(x); } while(m--){ cin>>l>>r; q.push_back({l,r}); all.push_back(l); all.push_back(r); } sort(all.begin(),all.end()); all.erase(unique(all.begin(),all.end()),all.end());
for(int i=1;i<=all.size();i++){ int t = a[all[i-1]]; s[i] = t + s[i-1]; } for(auto i:q){ l = lower_bound(all.begin(),all.end(),i.first)-all.begin(); r = lower_bound(all.begin(),all.end(),i.second)-all.begin(); cout<<s[r+1] - s[l]<<endl; }}3. Interval Merging
3.1 Background
Quickly merge multiple intervals by sorting the intervals by their left endpoints and checking for overlaps.
3.2 Example problems
Reference code:
void solve(){ for(int i=0;i<n;i++){ cin>>l>>r; a.push_back({l,r}); } sort(a.begin(),a.end(),[](PII t1,PII t2){ return t1.first < t2.first; }); int al = -2e9,ar = -2e9; for(auto i : a){ int ml = i.first,mr = i.second; if(ml > ar){ ans ++; al = ml;ar = mr; }else if(mr > ar){ ar = mr; } }cout<<(ans ? ans : 1)<<endl;}If this article helped you, please share it with others!
Some information may be outdated





