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

n个查询操作，m个修改单点操作

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
using namespace std;
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(){
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){
modify(rt[x], y, A);
modify(rt[w + bel[x]], y, A);
}else{
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;
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(){
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){
modify(rt[x], y, A);
modify(rt[w + bel[x]], y, A);
}else{
++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;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mid ((l + r) >> 1)
using namespace std;
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(){
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);
while (m--){
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;
}


0条评论