zoukankan      html  css  js  c++  java
  • Bzoj1176:Mokia&Cogs1752:[BOI2007]摩基亚Mokia

    题目

    Cogs
    没有Bzoj的权限号

    Sol

    离线,(CDQ)分治,把询问拆成(4)个,变成每次求二维前缀和
    那么只要一个修改操作(关键字为时间,(x)(y))都在这个询问前,就可以累计答案
    那么就成了偏序问题了,直接(CDQ)
    注意当(x)相等时要把修改丢在前面

    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    # define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
    using namespace std;
    typedef long long ll;
    const int _(2e6 + 5);
    
    IL ll Input(){
    	RG ll x = 0, z = 1; RG char c = getchar();
    	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    	return x * z;
    }
    
    int n, m, cal[_], Q;
    ll ans[_], bit[_], a[_];
    struct Data{
    	int id, x, y, op;
    } q[_], tmp[_];
    
    IL void Add(RG int x, RG ll v){
    	for(; x <= n; x += x & -x) bit[x] += v;
    }
    
    IL ll Query(RG int x){
    	RG ll ret = 0;
    	for(; x; x -= x & -x) ret += bit[x];
    	return ret;
    }
    
    IL bool Cmp(RG int x, RG int y){
    	if(q[x].x != q[y].x) return q[x].x < q[y].x;
    	if(!q[x].op) return 1;
    	return 0;
    }
    
    IL void CDQ(RG int l, RG int r){
    	if(l == r) return;
    	RG int mid = (l + r) >> 1;
    	CDQ(l, mid); CDQ(mid + 1, r);
    	for(RG int i = l, j = mid + 1, k = l; k <= r; ++k)
    		if(j > r || (i <= mid && Cmp(i, j))) tmp[k] = q[i++];
    		else tmp[k] = q[j++];
    	for(RG int i = l; i <= r; ++i) q[i] = tmp[i];
    	for(RG int i = l; i <= r; ++i)
    		if(q[i].id <= mid){
    			if(!q[i].op) Add(q[i].y, a[q[i].id]);
    		}
    		else{
    			if(q[i].op){
    				ans[cal[q[i].id]] += 1LL * Query(q[i].y) * q[i].op;
    			}
    		}
    	for(RG int i = l; i <= r; ++i)
    		if(q[i].id <= mid && !q[i].op) Add(q[i].y, -a[q[i].id]);
    }
    
    int main(RG int argc, RG char* argv[]){
    	File("mokia");
    	Input(); n = Input();
    	for(RG int op = Input(); op != 3; op = Input()){
    		if(op == 1){
    			RG int x = Input(), y = Input(); a[++m] = Input();
    			q[m] = (Data){m, x, y, 0};
    		}
    		else{
    			RG int x1 = Input(), y1 = Input(), x2 = Input(), y2 = Input();
    			++Q;
    			q[++m] = (Data){m, x2, y2, 1}; cal[m] = Q;
    			q[++m] = (Data){m, x1 - 1, y1 - 1, 1}; cal[m] = Q;
    			q[++m] = (Data){m, x1 - 1, y2, -1}; cal[m] = Q;
    			q[++m] = (Data){m, x2, y1 - 1, -1}; cal[m] = Q;
    		}
    	}
    	CDQ(1, m);
    	for(RG int i = 1; i <= Q; ++i) printf("%lld
    ", ans[i]);
    	return 0;
    }
    
    
  • 相关阅读:
    QPS的优化
    面试题
    py-redis
    集群化的部署
    RESTful设计
    python第三方库------jieba库(中文分词)
    csv文件读写处理
    python中的turtle库(图形绘画库)
    html中form表单的使用方法和介绍
    WC2019
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8372282.html
Copyright © 2011-2022 走看看