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

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 
# include 
# include 
# include 
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 
# include 
# include 
# include 
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

方法三:二维线段树

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

# include 
# include 
# include 
# include 
# 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;
}

 

你可能感兴趣的:(线段树)