lightoj 1081 - Square Queries 【二维线段树】
来源:互联网 发布:大数据 数据标准化 编辑:程序博客网 时间:2024/05/20 17:06
题目链接:lightoj 1081 - Square Queries
题意:给定一个矩阵,每次查询以
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 500 + 10;const int pN = 1e6;// <= 10^7const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void add(LL &x, LL y) { x += y; x %= MOD; }struct Nodey { int ly, ry; int Max;};int posx[MAXN], posy[MAXN];int n;struct Nodex { int lx, rx; Nodey treey[MAXN<<2]; void Build_y(int o, int l, int r) { treey[o].ly = l; treey[o].ry = r; treey[o].Max = 0; if(l == r) { posy[l] = o; return ; } int mid = (l + r) >> 1; Build_y(ll, l, mid); Build_y(rr, mid+1, r); } int Query_y(int o, int y1, int y2) { if(treey[o].ly == y1 && treey[o].ry == y2) { return treey[o].Max; } int mid = (treey[o].ly + treey[o].ry) >> 1; if(y2 <= mid) return Query_y(ll, y1, y2); else if(y1 > mid) return Query_y(rr, y1, y2); else return max(Query_y(ll, y1, mid), Query_y(rr, mid+1, y2)); }};Nodex treex[MAXN<<2];void Build_x(int o, int l, int r){ treex[o].lx = l; treex[o].rx = r; treex[o].Build_y(1, 1, n); if(l == r) { posx[l] = o; return ; } int mid = (l + r) >> 1; Build_x(ll, l, mid); Build_x(rr, mid+1, r);}int Query_x(int o, int x1, int x2, int y1, int y2) { if(treex[o].lx == x1 && treex[o].rx == x2) { return treex[o].Query_y(1, y1, y2); } int mid = (treex[o].lx + treex[o].rx) >> 1; if(x2 <= mid) return Query_x(ll, x1, x2, y1, y2); else if(x1 > mid) return Query_x(rr, x1, x2, y1, y2); else return max(Query_x(ll, x1, mid, y1, y2), Query_x(rr, mid+1, x2, y1, y2));}void PushUp(int nx, int ny, int v) { treex[nx].treey[ny].Max = max(treex[nx].treey[ny].Max, v);}void Update(int x, int y, int v) { int sx = posx[x], sy = posy[y]; for(int i = sx; i; i >>= 1) { for(int j = sy; j; j >>= 1) { PushUp(i, j, v); } }}int main(){ int t, kcase = 1; scanf("%d", &t); while(t--) { int m; scanf("%d%d", &n, &m); Build_x(1, 1, n); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { int v; scanf("%d", &v); Update(i, j, v); } } printf("Case %d:\n", kcase++); while(m--) { int op, x ,y; scanf("%d%d%d", &x, &y, &op); printf("%d\n", Query_x(1, x, x + op - 1, y, y + op - 1)); } } return 0;}
0 0
- lightoj 1081 - Square Queries 【二维线段树】
- LightOJ - 1081 Square Queries(二维RMQ)
- lightoj 1081 - Square Queries(二维RMQ)
- LightOj 1081 二维线段树
- lightoj 1164 - Horrible Queries 线段树入门
- lightOj 1164 Horrible Queries (线段树)
- LightOJ Array Queries 1082【线段树求区间最值】
- lightOJ 1082 - Array Queries 【线段树(求最小值)】
- LightOJ 1164 Horrible Queries 线段树区间更新求和
- LightOJ - 1164 Horrible Queries 线段树+懒惰标记
- LightOJ 1082 - Array Queries【线段树最值】
- LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)
- LightOJ-2991-线段树
- LightOJ-1087-线段树
- LightOJ-1093-线段树
- LightOJ-1083-线段树
- LightOJ-1097-线段树
- codeforces817F MEX Queries -- 线段树
- 随笔1
- 前端路由
- Mac Spotlight搜索快捷键
- Trick (八)——list 模拟 stack(Python)
- 队列原理详解
- lightoj 1081 - Square Queries 【二维线段树】
- 面试感悟:3年工作经验程序员应有的技能
- 南阳题目56-阶乘因式分解(一)
- ZooKeeper解惑(转)
- 笔记1handler/广播/布局/AIDL
- 第四周【项目3-随机数函数应用于游戏】
- QR code API
- CSS行高——line-height 文本的垂直居中(display:block;情况下)
- Robot Motion