POJ1195Mobile phones

Posted redwind

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1195Mobile phones相关的知识,希望对你有一定的参考价值。

Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 21905   Accepted: 10188

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

[Submit]   [Go Back]   [Status]   [Discuss]

技术分享图片Home Page   技术分享图片Go Back  技术分享图片To top

Solution:

线段树套线段树,第一次写……

反正我这次写的与常规线段树不一样,外围X线段树表示[l,r]内,内层Y线段树表示内层和。

更新是从上往下的而不是从下往上的,虽然据某fyj同学说可以从下往上维护,但是方法不明。

https://blog.csdn.net/zzkksunboy/article/details/70215755 ←这一篇讲的特别好,先安利一下。

每一个X节点上都要建一个线段树,表示在X节点这个范围上[l,r]的和(同

样是用线段树维护)。

我算是发现了,似乎线段树上每个点并不需要储存l,r信息(这点存疑,至少我目前遇到的所有题目都可以不储存)。

二维线段树我觉得不好理解,就没学,相比之下树套树反而显得很优美。

技术分享图片
/*In Search Of Life*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iomanip>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
    int now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c==-)ju=-1;
        else if(c>=0&&c<=9)
        {
            now=now*10+c-0;
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
inline long long llinit()
{
    long long now=0,ju=1;char c;bool flag=false;
    while(1)
    {
        c=getchar();
        if(c==-)ju=-1;
        else if(c>=0&&c<=9)
        {
            now=now*10+c-0;
            flag=true;
        }
        else if(flag)return now*ju;
    }
}
#define lson (now<<1)
#define rson (now<<1|1)
#define mid ((l+r)>>1)
int sz,v,n; 
struct NodeX
{
    int rooty;
}treeX[5005];
struct NodeY
{
    int lch,rch;
    int sum;
}treeY[5000005];
void Build_Tree_Y(int &now,int l,int r)
{
    now=++sz;
    if(l==r)return;
    else
    {
        Build_Tree_Y(treeY[now].lch,l,mid);
        Build_Tree_Y(treeY[now].rch,mid+1,r);
        treeY[now].sum=treeY[treeY[now].lch].sum+treeY[treeY[now].rch].sum;
    }
}
void Build_Tree_X(int now,int l,int r)
{
    Build_Tree_Y(treeX[now].rooty,1,n);
    if(l==r)return;
    else
    {
        Build_Tree_X(lson,l,mid);
        Build_Tree_X(rson,mid+1,r);
        return;
    }
}
void Modify_Tree_Y(int now,int l,int r,int y)
{
    treeY[now].sum+=v;
    if(l==r)return;
    else
    {
        if(y<=mid)Modify_Tree_Y(treeY[now].lch,l,mid,y);
        else Modify_Tree_Y(treeY[now].rch,mid+1,r,y);
    }
} 
void Modify_Tree_X(int now,int l,int r,int x,int y)
{
    Modify_Tree_Y(treeX[now].rooty,1,n,y);
    if(l==r)return;
    else
    {
        if(x<=mid)Modify_Tree_X(lson,l,mid,x,y);
        else if(x>mid)Modify_Tree_X(rson,mid+1,r,x,y);
    }
}
int Query_Tree_Y(int now,int l,int r,int y1,int y2)
{
    if(l==y1&&r==y2)
    {
        return treeY[now].sum;
    }
    else 
    {
        if(y2<=mid)return Query_Tree_Y(treeY[now].lch,l,mid,y1,y2);
        else if(y1>mid)return Query_Tree_Y(treeY[now].rch,mid+1,r,y1,y2);
        else return Query_Tree_Y(treeY[now].lch,l,mid,y1,mid)+Query_Tree_Y(treeY[now].rch,mid+1,r,mid+1,y2);
    }
}
int Query_Tree_X(int now,int l,int r,int x1,int x2,int y1,int y2)
{
    if(l==x1&&r==x2)
    {
        return Query_Tree_Y(treeX[now].rooty,1,n,y1,y2);
    }
    else 
    {
        if(x2<=mid)return Query_Tree_X(lson,l,mid,x1,x2,y1,y2);
        else if(x1>mid)return Query_Tree_X(rson,mid+1,r,x1,x2,y1,y2);
        else return Query_Tree_X(lson,l,mid,x1,mid,y1,y2)+Query_Tree_X(rson,mid+1,r,mid+1,x2,y1,y2);
    }
}
int t,a,b,c,d;
int main()
{
    while(t=init(),t!=3)
    {
        if(t==0)n=init(),Build_Tree_X(1,1,n);
        else if(t==1)
        {
            a=init();b=init();v=init();
            a++;b++;
            Modify_Tree_X(1,1,n,a,b);
        }
        else if(t==2)
        {
            a=init();b=init();c=init();d=init();
            a++;b++;c++;d++; 
            printf("%d
",Query_Tree_X(1,1,n,a,c,b,d));
        }
    }
    return 0;
}
View Code

复杂度分析:

空间复杂度:外层线段树总共$O(n log{n})$,对于每一个外部点内部对应一颗线段树同样也是$O(n log{n})$,总复杂度$O(n^2log^2{n})$。

时间复杂度:外层查询每次$O(log{n})$,内层查询每次$O(log{n})$,总复杂度$O(nlog^2{n})$。

基本就这样……











以上是关于POJ1195Mobile phones的主要内容,如果未能解决你的问题,请参考以下文章

POJ_1195 Mobile phones 二维树状数组

poj-1195-Mobile Phones

poj 1195 Mobile phones

POJ1195Mobile phones

●POJ 1195 Mobile phones

POJ1195 Mobile phones