zoukankan      html  css  js  c++  java
  • SPOJ:To the moon

    题面

    vjudge

    Sol

    主席树模板

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(1e5 + 5);
    const int __(5e6);
    
    IL int Input(){
        RG int 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 rt[_], tot, ls[__], rs[__];
    ll add[__], sum[__];
    int n, m;
    
    IL void Build(RG int &x, RG int l, RG int r){
    	x = ++tot;
    	if(l == r){
    		sum[x] = Input();
    		return;
    	}
    	RG int mid = (l + r) >> 1;
    	Build(ls[x], l, mid), Build(rs[x], mid + 1, r);
    	sum[x] = sum[ls[x]] + sum[rs[x]];
    }
    
    IL void Modify(RG int &x, RG int l, RG int r, RG int L, RG int R, RG ll ad){
    	ls[++tot] = ls[x], rs[tot] = rs[x], sum[tot] = sum[x], add[tot] = add[x];
    	x = tot;
    	RG int len = min(r, R) - max(l, L) + 1;
    	sum[x] += 1LL * ad * len;
    	if(L <= l && R >= r){
    		add[x] += ad;
    		return;
    	}
    	RG int mid = (l + r) >> 1;
    	if(L <= mid) Modify(ls[x], l, mid, L, R, ad);
    	if(R > mid) Modify(rs[x], mid + 1, r, L, R, ad);
    }
    
    IL ll Query(RG int x, RG int l, RG int r, RG int L, RG int R, RG ll ad){
    	if(L <= l && R >= r) return sum[x] + 1LL * ad * (r - l + 1);
    	ad += add[x];
    	RG int mid = (l + r) >> 1; RG ll ret = 0;
    	if(L <= mid) ret = Query(ls[x], l, mid, L, R, ad);
    	if(R > mid) ret += Query(rs[x], mid + 1, r, L, R, ad);
    	return ret;
    }
    
    int main(RG int argc, RG char* argv[]){
    	n = Input(), m = Input();
    	Build(rt[0], 1, n);
    	for(RG int i = 1, now = 0, x, y, z; i <= m; ++i){
    		RG char op; scanf(" %c", &op);
    		if(op == 'C'){
    			++now, rt[now] = rt[now - 1];
    			x = Input(), y = Input(), z = Input();
    			Modify(rt[now], 1, n, x, y, z);
    		}
    		else if(op == 'Q'){
    			x = Input(), y = Input();
    			printf("%lld
    ", Query(rt[now], 1, n, x, y, 0));
    		}
    		else if(op == 'H'){
    			x = Input(), y = Input(), z = Input();
    			printf("%lld
    ", Query(rt[z], 1, n, x, y, 0));
    		}
    		else now = Input();
    	}
        return 0;
    }
    
    
  • 相关阅读:
    引擎设计跟踪(九.10) Max插件更新,地形问题备忘
    引擎设计跟踪(九.9) 文件包系统(Game Package System)
    [原] Android上使用native IO
    [原] GLES在iOS和Android上的不同
    [原] perforce 获取本地最近更新的Changelist
    [转]GLES 3.0 新特性
    [原]android不支持命名的semaphore
    [原]android 链接错误
    引擎设计跟踪(九.8) Gizmo helper实现与多国语言
    [原]游戏引擎与游戏逻辑
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8481384.html
Copyright © 2011-2022 走看看