HDU 4666 Hyperspace (线段树+曼哈顿距离+离线查询)

Hyperspace

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1450    Accepted Submission(s): 708


 

Problem Description

The great Mr.Smith has invented a hyperspace particle generator. The device is very powerful. The device can generate a hyperspace. In the hyperspace, particle may appear and disappear randomly. At the same time a great amount of energy was generated.
However, the device is in test phase, often in a unstable state. Mr.Smith worried that it may cause an explosion while testing it. The energy of the device is related to the maximum manhattan distance among particle.
Particles may appear and disappear any time. Mr.Smith wants to know the maxmium manhattan distance among particles when particle appears or disappears.

 

Input

The input contains several test cases, terminated by EOF.
In each case: In the first line, there are two integer q(number of particle appear and disappear event, ≤60000) and k(dimensions of the hyperspace that the hyperspace the device generated, ≤5). Then follows q lines. In each line, the first integer ‘od’ represents the event: od = 0 means this is an appear
event. Then follows k integer(with absolute value less then 4 × 107). od = 1 means this is an disappear event. Follows a integer p represents the disappeared particle appeared in the pth event.

 

Output

Each test case should contains q lines. Each line contains a integer represents the maximum manhattan distance among paticles.

 

Sample Input

 

10 2 0 208 403 0 371 -180 1 2 0 1069 -192 0 418 -525 1 5 1 1 0 2754 635 0 -2491 961 0 2954 -2516

 

Sample Output

 

0 746 0 1456 1456 1456 0 2512 5571 8922

 

Source

2013 Multi-University Training Contest 7

 

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  6414 6413 6412 6411 6410 

 

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)

#define lrt int l,int r,int rt
#define root l,r,rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
/*
题目大意:每次给定两种操作的一种,
删除前面的点,添加一个k维的点。
每次操作输出最远曼哈顿距离。

首先要有曼哈顿距离可以二进制枚举
的背景知识,不然想破头都想不出来。
拿二维来说,|x1-x2|+|y1-y2|,
最终可以延展成这四种情况,
(x1+y1)-(x2+y2),(x1-y1)-(x2-y2),
(-x1+y1)-(-x2+y2),(-x1-y1)-(-x2-y2).
可以看到可以用二进制枚举出每一种选择的情况。
所以这题我们可以直接离线,
然后枚举1<<m的所有子集,代表上面小括号的每一种选择,
然后用线段树维护一个最大最小值即可。
*/
const int  maxn =6e4+5;
const int INF = 1e8;
///需要离线查询
///线段树维护极值
int maxv[maxn<<2],minv[maxn<<2];
void pushup(lrt)
{
    maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
    minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}
void build(lrt)
{
    if(l==r) {  minv[rt]=INF;maxv[rt]=-INF; return ; }
    int mid=l+r>>1;
    build(lson),build(rson),pushup(root);
}
void Clear(lrt,int pos,int v1,int v2)
{
    if(l==r) {maxv[rt]=v1;minv[rt]=v2;return ;}
    int mid=l+r>>1;
    if(pos<=mid) Clear(lson,pos,v1,v2);
    if(mid<pos) Clear(rson,pos,v1,v2);
    pushup(root);
}

int n,m;
int x,y;
int s[maxn][10],op[maxn];///数据离线记录
int ans[maxn];

void init()
{
    memset(ans,0,sizeof(ans));
    memset(op,0,sizeof(op));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&op[i]);
        if(op[i]) scanf("%d",&op[i]);
        else    for(int j=0;j<m;j++) scanf("%d",&s[i][j]);
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();///初始化
        for(int i=0;i<(1<<m);i++)///对于一种枚举情况,对应一种最值,离线查询
        {
            build(1,n,1);
            for(int j=1;j<=n;j++)
            {
                if(op[j]) Clear(1,n,1,op[j],-INF,INF);///
                else
                {
                    int tp=0;
                    for(int k=0;k<m;k++)
                    {
                        if((1<<k)&i) tp+=s[j][k];
                        else tp-=s[j][k];
                    }
                    Clear(1,n,1,j,tp,tp);///x修改函数
                }
                ans[j]=max(ans[j],maxv[1]-minv[1]);
            }
        }
        for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81865147