zoj2859 Matrix Searching

不带修改的二维求RMQ,二维线段树。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

typedef long long ll;
const int maxn=1200;
const int INF=1e9+10;

int n,q;
int val[maxn][maxn];
int x,y,xt,yt;
struct NodeX
{
    int Min[maxn<<2];
    void up(int rt)
    {
        Min[rt]=min(Min[rt<<1],Min[rt<<1|1]);
    }
    void build(int l,int r,int rt)
    {
        if(l==r){
            Min[rt]=INF;
            return;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        up(rt);
    }
    void update(int p,int c,int l,int r,int rt)
    {
        if(l==r){
            Min[rt]=min(Min[rt],c);
            return;
        }
        int m=(l+r)>>1;
        if(p<=m) update(p,c,lson);
        else update(p,c,rson);
        up(rt);
    }
    int query(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R) return Min[rt];
        int m=(l+r)>>1;
        int res=INF;
        if(L<=m) res=min(res,query(L,R,lson));
        if(R>m) res=min(res,query(L,R,rson));
        return res;
    }
};NodeX tx[maxn<<2];

void build(int l,int r,int rt)
{
    tx[rt].build(1,n,1);
    if(l==r) return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}

void update(int x,int y,int c,int l,int r,int rt)
{
    tx[rt].update(y,c,1,n,1);
    if(l==r) return;
    int m=(l+r)>>1;
    if(x<=m) update(x,y,c,lson);
    else update(x,y,c,rson);
}

int query(int xL,int xR,int yL,int yR,int l,int r,int rt)
{
    if(xL<=l&&r<=xR) return tx[rt].query(yL,yR,1,n,1);
    int m=(l+r)>>1;
    int res=INF;
    if(xL<=m) res=min(res,query(xL,xR,yL,yR,lson));
    if(xR>m) res=min(res,query(xL,xR,yL,yR,rson));
    return res;
}

int main()
{
    freopen("in.txt","r",stdin);
    int T;cin>>T;
    while(T--){
        scanf("%d",&n);
        REP(i,1,n) REP(j,1,n) scanf("%d",&val[i][j]);
        build(1,n,1);
        REP(i,1,n) REP(j,1,n) update(i,j,val[i][j],1,n,1);
        scanf("%d",&q);
        while(q--){
            scanf("%d%d%d%d",&x,&y,&xt,&yt);
            printf("%d\n",query(x,xt,y,yt,1,n,1));
        }
    }
    return 0;
}

我是直接把初值设为INF,然后再赋值的时候用Min[rt]=min(Min[rt],c)在每一层进行单点更新,但是这样我要怎么修改啊。。。维护个二维的RMQ连单点修改都做不到,这和咸鱼有什么区别。。。

时间: 2024-10-16 20:33:01

zoj2859 Matrix Searching的相关文章

ZOJ 1859 Matrix Searching(二维线段树)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seconds      Memory Limit: 32768 KB Given an n*n matrix A, whose entries Ai,j are integer numbers ( 1 <= i <= n, 1 <= j <= n ). An operation FIND t

ZOJ 题目2859 Matrix Searching(二维RMQ)

Matrix Searching Time Limit: 10 Seconds      Memory Limit: 32768 KB Given an n*n matrix A, whose entries Ai,j are integer numbers ( 1 <= i <= n, 1 <= j <= n ). An operation FIND the minimun number in a given ssub-matrix. Input The first line o

zoj 2589 Matrix Searching 二维线段树

题目链接 给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值. 代码基本上和上一题相同... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

待刷题目分类

各大OJ题目归类 Posted on 2012 年 8 月 8 日 by admin ---------–最优化问题------------- --------动态规划 SOJ1162 I-Keyboard SOJ2096 Maximum Submatrix SOJ2111 littleken bg SOJ2505 The County Fair SOJ2818 QQ音速 SOJ2469 Exploring Pyramids SOJ1833 Base Numbers SOJ2009 Zeros

Searching for Approximate Nearest Neighbours

Searching for Approximate Nearest Neighbours Nearest neighbour search is a common task: given a query object represented as a point in some (often high-dimensional) space, we want to find other objects in that space that lie close to it. For example,

Programmer Competency Matrix

Note that the knowledge for each level is cumulative; being atlevel n implies that you also know everything from thelevels lower than n. Computer Science   2n (Level 0) n2 (Level 1) n (Level 2) log(n) (Level 3) Comments data structures        Doesn’t

hdu 5015 233 Matrix (矩阵快速幂)

题意: 有一种矩阵,它的第一行是这样一些数:a  0,0 = 0, a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333... 除此之外,在这个矩阵里, 我们有 a i,j = a i-1,j +a i,j-1( i,j ≠ 0).现在给你 a 1,0,a 2,0,...,a n,0, 你能告诉我a n,m 是多少吗? n,m(n ≤ 10,m ≤ 10 9)输出 a n,m mod 10000007. 思路:首先我们观察n和m的取值范围,会发现n非常小而m却非常大,如果

【数组】Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路: 本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵.同理,我们也是逐个环