Block && Team Mo study notes

Pit too, and sometimes even fill it up (now Consignee to block)


Block team are very good and Mo violence algorithm,

To make people in the face of some of the requirements to maintain a special sequence of questions, you can get a high score even violence AC

Even multiple times throughout the province selected, learned no loss of good things

(But still we have to seek positive solutions ah cleverly crooked)


Block

Block, by definition, will cut the number of columns, batch answer information on each block,

When asked if the inquiry section covers the block, direct statistical information on the line answer, if not, put the block violence apart, query specific data inside.

Modify If you modify this section covers the block, this block to make a mark, then the query time labeling process to a block in the specific data, if not covered, it will open violence to maintain specific data.

Specifically title.


Luogu P3870 [TJOI2009] Switch

  • There is a sequence of 01, required to support the range of exclusive-or operation range query, by force.

  • = Sequence length = number of operations 1E5

Segment tree, block,Naive violenceA can

First obtains the block length, then the number of blocks is obtained, followed by treatment of the basic information of each block, and then carefully maintained on the line

Many details of the block,I transferred the afternoon, Said here a few konjac wrong place

  • Playing tag when the answer has been processed, the processing time and eliminate tag again answer ......

  • No handling of sections within a block

  • When the cycle is the block index i, the result of which the cycle index and find a subscript ......

#include<bits/stdc++.h>

using namespace std ;

const int MAXN = 100010;
int n,m;
struct Block{
    int l,r;
    int ans,tag;
}blo[1000];
int fab[MAXN],a[MAXN],sq,bctr;

void set_up(){
    sq = sqrt(n);
    bctr = n/sq;
    if(n%sq) ++bctr;
//      cout<<sq<<"<-sq bctr->"<<bctr<<endl;
    for(int i=1;i<=n;i++){
        fab[i] = (i-1)/sq+1;
//          cout<<fab[i]<<" ";
    }
//      cout<<endl;
    for(int i=1;i<bctr;i++){
        blo[i].l = (i-1) * sq + 1;
        blo[i].r = i * sq; 
    }
    blo[bctr].l = (bctr-1) * sq + 1;
    blo[bctr].r = n;
//  for(int i=1;i<=bctr;i++){
//      cout<<"i = "<<i;
//      cout<<" ans = "<<blo[i].ans;
//      cout<<" tag = "<<blo[i].tag;
//      cout<<" l = "<<blo[i].l<<" r = "<<blo[i].r<<endl;
//  }
}

void pushdown(int x){//无脑push 
//  if(blo[x].tag == 0) cout<<"???"<<endl;
    for(int i=blo[x].l;i<=blo[x].r;i++)
        a[i] ^= 1;
    blo[x].tag = 0;
}

void change (int x,int y){
    if(fab[x] == fab[y]){
        if(blo[fab[x]].tag) pushdown(fab[x]);
        for(int i=x;i<=y;i++){
            a[i] ^= 1;
            blo[fab[i]].ans += ((a[i] == 1) ? 1 : -1); 
        }
        return;
    }
/*-----------same block--------*/ 
    if(blo[fab[x]].l != x){
        if(blo[fab[x]].tag) pushdown(fab[x]);
        for(int i=x;i<=blo[fab[x]].r;i++){
            a[i] ^= 1;
            blo[fab[i]].ans += (a[i] == 1) ? 1 : -1;
        }
    }
    else {
        blo[fab[x]].tag ^= 1;
        blo[fab[x]].ans = (blo[fab[x]].r - blo[fab[x]].l + 1) - blo[fab[x]].ans;
    }
/*-----------left part---------*/
    if(blo[fab[y]].r != y){
        if(blo[fab[y]].tag) pushdown(fab[y]);
        for(int i=blo[fab[y]].l;i<=y;i++){
            a[i] ^= 1;
            blo[fab[y]].ans += (a[i] == 1) ? 1 : -1;
        }
    }
    else{
        blo[fab[y]].tag ^= 1;
        blo[fab[y]].ans = (blo[fab[y]].r - blo[fab[y]].l + 1) - blo[fab[y]].ans;
    }
/*-----------right part---------*/
    for(int i=fab[x]+1;i<=fab[y]-1;i++){
        blo[i].tag ^= 1;
        blo[i].ans = (blo[i].r - blo[i].l + 1) - blo[i].ans;
    }
//  for(int i=1;i<=bctr;i++){
//      cout<<blo[i].ans<<" ";
//  }
    
    return;
/*-----------middle part--------*/
}

int ask(int x,int y){
    int ret = 0;
    
//      for(int i=1;i<=bctr;i++){
//          cout<<blo[i].ans<<" ";
//      }
    
    if(fab[x] == fab[y]){
        if(blo[fab[x]].tag) pushdown(fab[x]);
        for(int i=x;i<=y;i++){
            ret += a[i];
        }
        return ret;
    }
    
    if(x != blo[fab[x]].l) {
        if(blo[fab[x]].tag) pushdown(fab[x]);
        for(int i = x; i <= blo[fab[x]].r; i++) {
            ret += a[i];
        }
    }
    else ret += blo[fab[x]].ans;
    
//      for(int i=1;i<=bctr;i++){
//          cout<<blo[i].ans<<" ";
//      }
    
    if(y != blo[fab[y]].r) {
        if(blo[fab[y]].tag) pushdown(fab[y]);
        for(int i = blo[fab[y]].l; i <= y; i++) {
            ret += a[i];
        }
    }
    else ret += blo[fab[y]].ans;
    
//      for(int i=1;i<=bctr;i++){
//          cout<<blo[i].ans<<" ";
//      }
    
    for(int i=fab[x]+1;i<=fab[y]-1;i++){
        ret += blo[i].ans;
//      cout<<"ret = "<<ret<<endl;
//      cout<<"i = "<<i<<endl;
//      cout<<"blo[i].ans = "<<blo[i].ans<<endl;
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    set_up();
    for(int i=1;i<=m;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if(t == 0){
            change(x,y);
        }
        if(t == 1){
            cout<<ask(x,y)<<endl;
        }
//      cout<<"for this round,the ans1 and ans2 and ans3 is "<<blo[1].ans<<" "<<blo[2].ans<<" "<<blo[3].ans<<endl;
    }
    return 0;
}

One million disgrace debugging information

Guess you like

Origin www.cnblogs.com/SINXIII/p/11240348.html