二维矩阵 动态求最小和求和问题 分块+线段树乱搞

对于列,直接分块,用线段树维护纵坐标
时间复杂度 O ( n l o g 2 W + m W l o g 2 W ) O(nlog_2W+m\sqrt Wlog_2W)
n个查询操作,m个修改单点操作
当修改操作十分稀疏时候,我们可以给线段树加上lazy优化,对于一个size只有1的子树,我们可以不用建出来,而是把要建的节点保存在该点。
优化可以直接过 [BOI2007]Mokia 摩基亚

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
using namespace std;
inline void read(int &x){
    x = 0; int f = 1; char ch = getchar();
    while (!(ch >= '0' && ch <= '9')){if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
const int N = 1e7;
const int M = 2e6 + 10;
struct Node{int l, r, lz, val;} T[N];
int tot, blo, bel[M], w, opt, rt[N];
inline void pushdown(int p, int l, int r){
    if (!T[p].lz || l == r) return;
    if (T[p].lz <= mid){
        T[p].l = ++tot; T[tot].lz = T[p].lz; T[tot].val = T[p].val;
    }else{
        T[p].r = ++tot; T[tot].lz = T[p].lz; T[tot].val = T[p].val;
    }
    T[p].lz = 0;
}
inline void modify(int &p, int x, int v, int l = 1, int r = w){
    if (!p){p = ++tot; T[p].lz = x; T[p].val = v; return;}
    pushdown(p, l, r); T[p].val += v;//注意顺序
    if (l == r) return;
    if (x <= mid) modify(T[p].l, x, v, l, mid);
    else modify(T[p].r, x, v, mid + 1, r);
}
inline int query(int p, int x, int l = 1, int r = w){
    if (!p || !x) return 0;
    if (l == r) return T[p].val;
    if (T[p].lz) return x >= T[p].lz ? T[p].val : 0;
    int ans = T[T[p].l].val * (x >= mid);
    if (x < mid) ans += query(T[p].l, x, l, mid);
    else if (x > mid) ans += query(T[p].r, x, mid + 1, r);
    return ans;
}
int main(){
    read(opt), read(w);
    blo = sqrt(w) * 1.5 + 2;
    for (int i = 1; i <= w; i++) bel[i] = (i - 1) / blo + 1;
    tot = w + bel[w] + 2;
    while (scanf("%d", &opt)){
        if (opt == 3) break;
        if (opt == 1){
            int x, y, A; read(x), read(y), read(A);
            modify(rt[x], y, A);
            modify(rt[w + bel[x]], y, A);
        }else{
            int x1, y1, x2, y2, ans = 0; read(x1), read(y1), read(x2), read(y2);
            int mn = min(bel[x1] * blo, x2);
            for (int i = x1; i <= mn; i++)
                ans += query(rt[i], y2) - query(rt[i], y1 - 1);
            if (bel[x1] != bel[x2])
            for (int i = (bel[x2] - 1) * blo + 1; i <= x2; i++)
                ans += query(rt[i], y2) - query(rt[i], y1 - 1);
            for (int i = bel[x1] + 1; i <= bel[x2] - 1; i++)
                ans += query(rt[w + i], y2) - query(rt[w + i], y1 - 1);
            printf("%d\n", ans);
        }
    }
    return 0;
}

poj上Mobile phones差不多,只是可以有(0,0)节点

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mid ((l + r) >> 1)
using namespace std;
inline void read(int &x){
    x = 0; int f = 1; char ch = getchar();
    while (!(ch >= '0' && ch <= '9')){if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
const int N = 1e6;
const int M = 2e6 + 10;
struct Node{int l, r, lz, val;} T[N];
int tot, blo, bel[M], w, opt, rt[N];
inline void pushdown(int p, int l, int r){
    if (!T[p].lz || l == r) return;
    if (T[p].lz <= mid){
        T[p].l = ++tot; T[tot].lz = T[p].lz; T[tot].val = T[p].val;
    }else{
        T[p].r = ++tot; T[tot].lz = T[p].lz; T[tot].val = T[p].val;
    }
    T[p].lz = 0;
}
inline void modify(int &p, int x, int v, int l = 1, int r = w){
    if (!p){p = ++tot; T[p].lz = x; T[p].val = v; return;}
    pushdown(p, l, r); T[p].val += v;//注意顺序
    if (l == r) return;
    if (x <= mid) modify(T[p].l, x, v, l, mid);
    else modify(T[p].r, x, v, mid + 1, r);
}
inline int query(int p, int x, int l = 1, int r = w){
    if (!p || !x) return 0;
    if (l == r) return T[p].val;
    if (T[p].lz) return x >= T[p].lz ? T[p].val : 0;
    int ans = T[T[p].l].val * (x >= mid);
    if (x < mid) ans += query(T[p].l, x, l, mid);
    else if (x > mid) ans += query(T[p].r, x, mid + 1, r);
    return ans;
}
int main(){
    read(opt), read(w); ++w;
    blo = sqrt(w) * 1.5 + 2;
    for (int i = 1; i <= w; i++) bel[i] = (i - 1) / blo + 1;
    tot = w + bel[w] + 2;
    while (scanf("%d", &opt)){
        if (opt == 3) break;
        if (opt == 1){
            int x, y, A; read(x), read(y), read(A); ++x, ++y;
            modify(rt[x], y, A);
            modify(rt[w + bel[x]], y, A);
        }else{
            int x1, y1, x2, y2, ans = 0; read(x1), read(y1), read(x2), read(y2);
            ++x1, ++y1, ++x2, ++y2;
            int mn = min(bel[x1] * blo, x2);
            for (int i = x1; i <= mn; i++)
                ans += query(rt[i], y2) - query(rt[i], y1 - 1);
            if (bel[x1] != bel[x2])
            for (int i = (bel[x2] - 1) * blo + 1; i <= x2; i++)
                ans += query(rt[i], y2) - query(rt[i], y1 - 1);
            for (int i = bel[x1] + 1; i <= bel[x2] - 1; i++)
                ans += query(rt[w + i], y2) - query(rt[w + i], y1 - 1);
            printf("%d\n", ans);
        }
    }
    return 0;
}

静态子矩阵最小值 Matrix Searching

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mid ((l + r) >> 1)
using namespace std;
inline void read(int &x){
    x = 0; int f = 1; char ch = getchar();
    while (!(ch >= '0' && ch <= '9')){if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
inline void Min(int &x, int y){if (y < x) x = y;}
const int N = 1e6;
const int M = 300 + 10;
const int MAX = 0x7fffffff;
struct Node{int l, r, mn;} T[N];
int n, m, rt[330], TT, blo, bel[M], cnt;
inline int New(){++cnt; T[cnt].l = T[cnt].r = 0; T[cnt].mn = MAX; return cnt;}
inline void modify(int &p, int x, int v, int l = 1, int r = n){
    if (!p) p = New();
    Min(T[p].mn, v);
    if (l == r) return;
    if (x <= mid) modify(T[p].l, x, v, l, mid);
    else modify(T[p].r, x, v, mid + 1, r);
}
inline int query(int p, int ll, int rr, int l = 1, int r = n){
    if (!p) return 0;
    if (l == ll && r == rr) return T[p].mn;
    if (rr <= mid) return query(T[p].l, ll, rr, l, mid);
    else if (ll > mid) return query(T[p].r, ll, rr, mid + 1, r);
    else return (min(query(T[p].l, ll, mid, l, mid), query(T[p].r, mid + 1, rr, mid + 1, r)));
}
int main(){
    read(TT);
    while (TT--){
        cnt = 0;
        memset(rt, 0, sizeof(rt));
        read(n); blo = sqrt(n) * 1.5 + 2;
        for (int i = 1; i <= n; i++) bel[i] = (i - 1) / blo + 1;
        for (int i = 1, x; i <= n; i++)
            for (int j = 1; j <= n; j++) read(x), modify(rt[i], j, x), modify(rt[n + bel[i]], j, x);
        read(m);
        while (m--){
            int x1, y1, x2, y2, ans = MAX; read(x1), read(y1), read(x2), read(y2);
            int mn = min(bel[x1] * blo, x2);
            for (int i = x1; i <= mn; i++) Min(ans, query(rt[i], y1, y2));
            if (bel[x1] != bel[x2])
            for (int i = (bel[x2] - 1) * blo + 1; i <= x2; i++) Min(ans, query(rt[i], y1, y2));
            for (int i = bel[x1] + 1; i <= bel[x2] - 1; i++) Min(ans, query(rt[i + n], y1, y2));
            printf("%d\n", ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44627639/article/details/88542501
0条评论
添加一条新回复