Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct sqrt_block{
- ll n , b;
- vector<vector<ll>>blocks;
- vector<ll>A , Shift;
- void init(ll _n){
- n = _n;
- b = ceil(sqrt(n));
- blocks.clear();
- Shift.clear();
- A.clear();
- for(int i=0;i<n;i+=b){
- vector<ll>temp1;
- for(int j=i;j<min(i+b ,n);j++){
- temp1.pb(0);
- }
- if(sz(temp1)){
- blocks.pb(temp1);
- }
- }
- Shift.assign(b , 0);
- A.assign(n , 0);
- }
- void rebuild(ll ind, ll l1 , ll r1 , ll add){
- blocks[ind].clear();
- for(int j = (ind)*b ; j < min(n , (ind+1)*b) ;j++){
- if(l1 <= j && j <= r1) A[j] += add;
- blocks[ind].push_back(A[j]);
- }
- sort(all(blocks[ind]));
- }
- void add_new_range(ll l , ll r , ll add){
- ll ind1 = l/b;
- ll ind2 = r/b;
- if(ind1==ind2){
- //in the same block
- rebuild(ind1 , l , r , add);
- }
- else{
- //some blocks
- rebuild(ind1 , l , ((ind1+1)*b) - 1 , add);
- for(int j=ind1+1;j<ind2;j++){
- Shift[j] += add;
- }
- rebuild(ind2 , ind2*b , r , add);
- }
- }
- ll calculate(){ //count of zeros
- ll ans = 0;
- for(int i=0;i<blocks.size();i++){
- ans += (lb(all(blocks[i]) , 1 - Shift[i]) - blocks[i].begin());
- }
- return n - ans;
- }
- }st;
Advertisement
Add Comment
Please, Sign In to add comment