zoukankan      html  css  js  c++  java
  • Luogu3870 [TJOI2009]开关 (分块)

    线段树做法很简单,但分块好啊

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    //#define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 100007;
    
    int block[N], blockSize;
    int a[N], ans[N], tag[N];
    inline void Change(int l, int r){
    	int minn = Min(r, block[l] * blockSize);
    	R(i,l,minn){
    		ans[block[i]] -= a[i] ^ tag[block[i]];
    		a[i] ^= 1;
    		ans[block[i]] += a[i] ^ tag[block[i]];
    	}
    	if(block[l] != block[r]){
    		R(i, (block[r] - 1) * blockSize + 1, r){
    			ans[block[i]] -= a[i] ^ tag[block[i]];
    			a[i] ^= 1;
    			ans[block[i]] += a[i] ^ tag[block[i]];
    		}
    	}
    	R(i,block[l] + 1, block[r] - 1){
    		ans[i] =  blockSize - ans[i]; // md, wo ge sa bi
    		tag[i] ^= 1;
    	}
    }
    inline int Query(int l, int r){
    	int sum = 0;
    	int minn = Min(r, block[l] * blockSize);
    	R(i,l,minn){
    		sum += a[i] ^ tag[block[i]];
    	}
    	if(block[l] != block[r]){
    		R(i, (block[r] - 1) * blockSize + 1, r){
    			sum += a[i] ^ tag[block[i]];
    		}
    	}
    	R(i,block[l] + 1, block[r] - 1){
    		sum += ans[i]; // I'm so sb
    	}
    	return sum;	
    }
    
    int main(){
    	int n, m;
        io >> n >> m;
        blockSize = sqrt(n); 
        R(i,1,n){
        	block[i] = (i - 1) / blockSize + 1;
        }
        R(i,1,m){
        	int opt, l, r;
        	io >> opt >> l >> r;
        	if(opt == 0){
        		Change(l, r);
        	}
        	else{
        		printf("%d
    ", Query(l, r));
        	} 
        }
        
        return 0;
    }
    

  • 相关阅读:
    MySQL操作表中的数据
    mysql查询语句进阶
    mysql基本查询语句
    mysql函数
    mysql约束
    操作MySQL表
    操作MySQL数据库
    mysql视图
    as2 播放停止音效
    as3 深复制
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11226335.html
Copyright © 2011-2022 走看看