UVa 11297 Census 2 차원 선분 트 리 템 플 릿
2 차원 선분 트 리 템 플 릿 문제, 단점 수정 과 구간 조회, 여 기 는 유 여가 가 말 한 대로 build 함 수 를 추가 하여 어느 정도 최적화 효 과 를 낼 수 있 습 니 다.
#include
#include
#include
using namespace std;
const int INF = (1<<30);
const int maxn = 2000 + 10;
struct IntervalTree2D {
int Max[maxn][maxn], Min[maxn][maxn], n, m;
int xo, xleaf, x1, y1, x2, y2, x, y, v, vmax, vmin;
void build1D(int o, int L, int R) {
if(L == R) {
if(xleaf) { scanf("%d", &v); Max[xo][o] = Min[xo][o] = v; return; }
Max[xo][o] = max(Max[xo*2][o], Max[xo*2+1][o]);
Min[xo][o] = min(Min[xo*2][o], Min[xo*2+1][o]);
}
else {
int M = (L + R) >> 1;
build1D(o*2, L, M);
build1D(o*2+1, M+1, R);
Max[xo][o] = max(Max[xo][o*2], Max[xo][o*2+1]);
Min[xo][o] = min(Min[xo][o*2], Min[xo][o*2+1]);
}
}
void build2D(int o, int L, int R) {
if(L == R) {
xleaf = 1;
xo = o;
build1D(1, 1, m);
}
else {
int M = (L + R) >> 1;
build2D(o*2, L, M);
build2D(o*2+1, M+1, R);
xleaf = 0; xo = o; build1D(1, 1, m);
}
}
void query1D(int o, int L, int R) {
if(y1 <= L && R <= y2) {
vmax = max(vmax, Max[xo][o]);
vmin = min(vmin, Min[xo][o]);
}
else {
int M = (L + R) >> 1;
if(y1 <= M) query1D(o*2, L, M);
if(M < y2) query1D(o*2+1, M+1, R);
}
}
void query2D(int o, int L, int R) {
if(x1 <= L && R <= x2) {
xo = o;
query1D(1, 1, m);
}
else {
int M = (L + R) >> 1;
if(x1 <= M) query2D(o*2, L, M);
if(M < x2) query2D(o*2+1, M+1, R);
}
}
void modify1D(int o, int L, int R) {
if(L == R) {
if(xleaf) { Max[xo][o] = Min[xo][o] = v; return; }
Max[xo][o] = max(Max[xo*2][o], Max[xo*2+1][o]);
Min[xo][o] = min(Min[xo*2][o], Min[xo*2+1][o]);
}
else {
int M = (L + R) >> 1;
if(y <= M) modify1D(o*2, L, M);
else modify1D(o*2+1, M+1, R);
Max[xo][o] = max(Max[xo][o*2], Max[xo][o*2+1]);
Min[xo][o] = min(Min[xo][o*2], Min[xo][o*2+1]);
}
}
void modify2D(int o, int L, int R) {
if(L == R) {
xleaf = 1; xo = o; modify1D(1, 1, m);
}
else {
int M = (L + R) >> 1;
if(x <= M) modify2D(o*2, L, M);
else modify2D(o*2+1, M+1, R);
xo = o; xleaf = 0; modify1D(1, 1, m);
}
}
void modify() { modify2D(1, 1, n); }
void query() { vmax = -INF, vmin = INF; query2D(1, 1, n); }
};
IntervalTree2D t;
int main() {
int n, m, Q, x1, y1, x2, y2, x, y, v;
char op[10];
scanf("%d", &n);
m = n;
t.n = n; t.m = n;
t.build2D(1, 1, n);
scanf("%d", &Q);
while(Q--) {
scanf("%s", op);
if(op[0] == 'q') {
scanf("%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2);
t.query();
printf("%d %d
", t.vmax, t.vmin);
}
else {
scanf("%d%d%d", &t.x, &t.y, &t.v);
t.modify();
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
UVa 110 순환 정렬 프로그램 없음제목: Pascal의 정렬 프로그램을 구성합니다.처음에 보면 Pascal 프로그램을 썼는데 모르는 것은 어려울 줄 알았지만 사실은 프로그램의 대부분이 고정되어 있고 직접printf를 쓰면 된다. 주로 비교적인if-e...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.