• 总结-二分


    CDQ(时间二分)

    BZOJ2683简单题

    拆询问,二分过程中归并解决偏序

    #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 Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
    
    #define ON_DEBUGG
    
    #ifdef ON_DEBUGG
    
    #define D_e_Line printf("
    -----------
    ")
    #define D_e(x) std::cout << (#x) << " : " <<x << "
    "
    #define FileOpen() freopen("in.txt", "r", stdin)
    #define FileSave() freopen("out.txt", "w", stdout)
    #define Pause() system("pause")
    #include <ctime>
    #define TIME() fprintf(stderr, "
    TIME : %.3lfms
    ", clock() * 1000.0 / CLOCKS_PER_SEC)
    
    #else
    
    #define D_e_Line ;
    #define D_e(x) ;
    #define FileOpen() ;
    #define FilSave ;
    #define Pause() ;
    #define TIME() ;
    
    #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;
    
    template<typename ATP> inline ATP Min(ATP a, ATP b) {
    	return a < b ? a : b;
    }
    template<typename ATP> inline ATP Max(ATP a, ATP b) {
    	return a > b ? a : b;
    }
    
    const int N = 1e6 + 7;
    
    int n, tim, qid;
    
    struct Ques {
    	int opt, x, y, val, id, qid;
    	bool operator < (const Ques &com) const {
    		if(x != com.x) return x < com.x;
    		if(opt != com.opt) return opt < com.opt;
    		return y < com.y;
    	}
    } q[N], tmp[N];
    
    long long t[N];
    inline void Updata(int x, int w) {
    	for(; x <= n; x += x & -x) t[x] += w;
    }
    
    inline long long Query(int x) {
    	long long sum = 0;
    	for(; x; x -= x & -x) sum += t[x];
    	return sum;
    }
    
    long long ans[N];
    inline void CDQ(int l, int r) {
    	if(l == r) return;
    	int mid = (l + r) >> 1;
    	CDQ(l, mid), CDQ(mid + 1, r);
    	int i = l, j = mid + 1, k = l;
    	while(i <= mid || j <= r){
    		if(j > r || (i <= mid && q[i] < q[j])){
    			if(q[i].opt == 1) Updata(q[i].y, q[i].val);
    			tmp[k++] = q[i++];
    		}
    		else{
    			if(q[j].opt == 2) ans[q[j].qid] += 1ll * q[j].val * Query(q[j].y);
    			tmp[k++] = q[j++];
    		}
    	}
    	R(i,l,r) if(q[i].id <= mid && q[i].opt == 1) Updata(q[i].y, -q[i].val);
    	R(i,l,r) q[i] = tmp[i];
    } 
    
    int main() {
    //FileOpen();
    //FileSave();
    	io >> n;
    	
    	while(1){
    		int opt;
    		io >> opt;
    		if(opt == 1){
    			int x, y, val;
    			io >> x >> y >> val;
    			q[++tim] = (Ques){ opt, x, y, val, tim};
    		}
    		else if(opt == 2){
    			int X1, X2, Y1, Y2;
    			io >> X1 >> Y1 >> X2 >> Y2;
    			++qid;
    			q[++tim] = (Ques){ opt, X1 - 1, Y1 - 1, 1, tim, qid};
    			q[++tim] = (Ques){ opt, X1 - 1, Y2, -1, tim, qid};
    			q[++tim] = (Ques){ opt, X2, Y1 - 1, -1, tim, qid};
    			q[++tim] = (Ques){ opt, X2, Y2, 1, tim, qid};
    		}
    		else{
    			break;
    		}
    	}
    	
    	CDQ(1, tim);
    	
    	R(i,1,qid){
    		printf("%lld
    ", ans[i]);
    	}
    	
    	return 0;
    } 
    

    没看的了,咕了

  • 相关阅读:
    libnet.h no such file or directory 规格严格
    postgresql通用赋权管理 规格严格
    PostgreSQL的shared_buffers和系统OS cache的关系 规格严格
    Redis使用命令 规格严格
    no module named pytz(pycharm) 规格严格
    安装postgresql报错:Requires: llvmtoolset7clang >= 4.0.1 规格严格
    解决:Caused by: java.lang.ClassNotFoundException: org.springframework.core.metrics.ApplicationStartup 规格严格
    libpqfe.h:没有那个文件或目录 规格严格
    Centos7 安装 PostgreSql 14 数据库 和 timescaledb 时序库 规格严格
    PostgreSQL Primer for Busy People 规格严格
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11681721.html
Copyright © 2020-2023  润新知