POJ1195:Mobile phones(二维BIT 或 CDQ分治 或 二维线段树)

本文深入探讨了三种处理二维矩阵数据的高级算法:二维树状数组、CDQ分治+树状数组及二维线段树。通过具体实例,详细讲解了每种方法的实现原理、代码实现及复杂度分析,为解决大规模矩阵数据处理问题提供了有效途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mobile phones

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 22311 Accepted: 10372

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 


The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 4
1 1 2 3
2 0 0 2 2 
1 1 1 2
1 1 2 -1
2 1 1 2 3 
3

Sample Output

3
4

Source

IOI 2001

题意:一个可以单点修改的矩阵,输出给定矩形区域的和,注意题目的下标由0开始。

方法一:二维树状数组

  矩形范围这么小,当然首选常数小又好写的BIT了,复杂度O(n*logn*logn)。

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 2e3;
LL s[maxn][maxn];
int n;
void update(int x, int y, LL val){
    for(int i=x; i<=n; i+=i&-i)
        for(int j=y; j<=n; j+=j&-j)
            s[i][j] += val;
}
LL query(int x, int y){
    LL res = 0;
    if(x == 0 || y == 0) return 0;
    for(int i=x; i>0; i-=i&-i)
        for(int j=y; j>0; j-=j&-j)
            res += s[i][j];
    return res;
}
int main(){
    int op, x, y, x1, y1;
    LL val;
    while(~scanf("%d",&op), op!=3){
        if(op == 0) scanf("%d",&n);
        else if(op == 1){
            scanf("%d%d%lld",&x,&y,&val);
            update(x+1, y+1, val);
        }
        else{
            scanf("%d%d%d%d",&x,&y,&x1,&y1);
            printf("%lld\n",query(x1+1,y1+1)-query(x, y1+1)-query(x1+1, y)+query(x, y));
        }
    }
    return 0;
}

方法二:CDQ分治+树状数组

把查询拆成四个部分的加加减减,本题实质就是一个三维偏序,外层是时间顺序,CDQ分治维护X的顺序,树状数组维护Y的值就行。复杂度O(n*logn*logn)

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 2e3;
struct node{
    int x, y, type, fu, id;
}a[300000], tmp[300000];
int n;
LL ans[300000], s[maxn];
void update(int pos, LL val){
    for(int i=pos; i<=n; i+=i&-i)
        s[i] += val;
}
int god = 0;
LL query(int pos){
    LL res = 0;
    for(int i=pos; i>0; i-=i&-i)
        res += s[i];
    return res;
}
void clr(int pos){
    for(int i=pos; i<=n; i+=i&-i){
        if(s[i] == 0) return;
        s[i] = 0;
    }
}
void cdq(int l, int r, int god){
    if(l == r) return;
    int mid = l+r>>1;
    cdq(l, mid, god+1);
    cdq(mid+1, r, god+1);
    int L=l, R=mid+1, cnt=0;
    while(L<=mid && R<=r){
        if(a[L].x <= a[R].x){
            if(a[L].type == 1) update(a[L].y, a[L].fu);
            tmp[cnt++] = a[L++];
        }
        else{
            if(a[R].type == 2) ans[a[R].id] += 1LL*a[R].fu*query(a[R].y);
            tmp[cnt++] = a[R++];
        }
    }
    while(L <= mid) tmp[cnt++] = a[L++];
    while(R <= r){
        if(a[R].type == 2) ans[a[R].id] += 1LL*a[R].fu*query(a[R].y);
        tmp[cnt++] = a[R++];
    }
    for(int i=0; i<cnt; ++i){
        clr(tmp[i].y);
        a[l+i] = tmp[i];
    }
}
int main(){
    int op, x, y, x1, y1, cnt=0, tot=0, val;
    while(~scanf("%d",&op), op!=3){
        if(op == 0) scanf("%d",&n);
        else if(op == 1){
            scanf("%d%d%d",&x,&y,&val);
            a[++cnt] = {x+1, y+1, 1, val, 0};
        }
        else{
            scanf("%d%d%d%d",&x,&y,&x1,&y1);
            a[++cnt] = {x1+1, y1+1, 2, 1, ++tot};
            a[++cnt] = {x1+1, y, 2, -1, tot};
            a[++cnt] = {x, y1+1, 2, -1, tot};
            a[++cnt] = {x, y, 2, 1, tot};
        }
    }
    cdq(1, cnt, 0);
    for(int i=1; i<=tot; ++i) printf("%lld\n",ans[i]);
    return 0;
}

方法三:二维线段树

  二位线段树一般支持单点修改+区间查询,或者区间修改+单点查询。本题是第一种情况,那么修改时将包含x的所有区间的y值都修改一次,第二维线段树顺便维护区间和,查询时根据线段树的性质能够避免计算同一个x多次,另外第二位线段树动态开点能够节省内存。复杂度O(n*logn*logn)

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cstring>
# define lson l,mid, id<<1
# define rson mid+1, r, id<<1|1
using namespace std;
typedef long long LL;
const int maxn = 2e3;
int cnt=0, RT[maxn*4], n;
LL ans = 0;
struct node{
    int l, r;
    LL sum;
}a[3000000];

void updatey(int l, int r, int &id, int y, LL val){
    if(id == 0) id = ++cnt;
    if(l == r){
        a[id].sum += val;
        return;
    }
    int mid = l+r>>1;
    if(y <= mid) updatey(l, mid, a[id].l, y, val);
    else updatey(mid+1, r, a[id].r, y, val);
    a[id].sum = a[a[id].l].sum + a[a[id].r].sum;
}
void updatex(int l, int r, int id, int x, int y, LL val){
    updatey(1, n, RT[id], y, val);
    if(l == r) return;
    int mid = l+r>>1;
    if(x <= mid) updatex(lson, x, y, val);
    else updatex(rson, x, y, val);
}
LL queryy(int y0, int y1, int l, int r, int id){
    if(y0 <= l && y1 >= r) return a[id].sum;
    int mid = l+r>>1; LL res = 0;
    if(y0 <= mid) res += queryy(y0, y1, l, mid, a[id].l);
    if(y1 > mid) res += queryy(y0, y1, mid+1, r, a[id].r);
    return res;
}
void queryx(int x0, int x1, int y0, int y1, int l, int r, int id){
    if(x0 <= l && x1 >= r){
        ans += queryy(y0, y1, 1, n, RT[id]);
        return;
    }
    int mid = l+r>>1;
    if(x0 <= mid) queryx(x0, x1, y0, y1, lson);
    if(x1 > mid) queryx(x0, x1, y0, y1, rson);
    return;
}
int main(){
    int op, x, y, x1, y1;
    LL val;
    while(~scanf("%d",&op), op!=3){
        if(op == 0) scanf("%d",&n);
        else if(op == 1){
            scanf("%d%d%lld",&x,&y,&val);
            updatex(1, n, 1, x+1, y+1, val);
        }
        else{
            scanf("%d%d%d%d",&x,&y,&x1,&y1);
            ans = 0;
            queryx(x+1, x1+1, y+1, y1+1, 1, n, 1);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值