Bzoj1176:Mokia&Cogs1752:[BOI2007]摩基亚Mokia

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n", ans[i]);
    return 0;
}

以上是关于Bzoj1176:Mokia&Cogs1752:[BOI2007]摩基亚Mokia的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ-1176&2683Mokia&简单题 CDQ分治

bzoj千题计划144:bzoj1176: [Balkan2007]Mokia

BZOJ1176Mokia(CDQ分治)

BZOJ 1176 MOKIA

bzoj 1176: [Balkan2007]Mokia

[BZOJ1176][Balkan2007]Mokia