码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 11297 Census (二维线段树)

时间:2017-10-05 23:10:40      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:int   puts   unsigned   ring   cto   ons   scanf   names   sizeof   

题意:给定上一个二维矩阵,有两种操作

第一种是修改 c x y val 把(x, y) 改成 val

第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值。

析:二维线段树裸板。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("in.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 500 + 10;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r > 0 && r <= n && c > 0 && c <= m;
}

int minv[maxn<<2][maxn<<2];
int maxv[maxn<<2][maxn<<2];
int mmin, mmax;

void push_upy(int x, int rt){
  minv[x][rt] = min(minv[x][rt<<1], minv[x][rt<<1|1]);
  maxv[x][rt] = max(maxv[x][rt<<1], maxv[x][rt<<1|1]);
}
void push_upx(int rt, int x){
  minv[rt][x] = min(minv[rt<<1][x], minv[rt<<1|1][x]);
  maxv[rt][x] = max(maxv[rt<<1][x], maxv[rt<<1|1][x]);
}

void buildy(int x, int l, int r, int rt, bool ok){
  if(l == r){
    if(ok){ scanf("%d", minv[x]+rt); maxv[x][rt] = minv[x][rt]; return ; }
    push_upx(x, rt);
    return ;
  }
  int m = l + r >> 1;
  buildy(x, lson, ok);
  buildy(x, rson, ok);
  push_upy(x, rt);
}

void buildx(int l, int r, int rt){
  if(l == r){ buildy(rt, all, 1); return ; }
  int m = l + r >> 1;
  buildx(lson);
  buildx(rson);
  buildy(rt, all, 0);
}

void updatey(int x, int Y, int val, int l, int r, int rt, bool ok){
  if(l == r){
    if(ok){  minv[x][rt] = maxv[x][rt] = val;  return ; }
    push_upx(x, rt);
    return ;
  }
  int m = l + r >> 1;
  if(Y <= m)  updatey(x, Y, val, lson, ok);
  else  updatey(x, Y, val, rson, ok);
  push_upy(x, rt);
}

void updatex(int X, int Y, int val, int l, int r, int rt){
  if(l == r){ updatey(rt, Y, val, all, 1);  return ;  }
  int m = l + r >> 1;
  if(X <= m)  updatex(X, Y, val, lson);
  else  updatex(X, Y, val, rson);
  updatey(rt, Y, val, all, 0);
}

void queryy(int x, int L, int R, int l, int r, int rt){
  if(L <= l && r <= R){
    mmin = min(mmin, minv[x][rt]);
    mmax = max(mmax, maxv[x][rt]);
    return ;
  }
  int m = l + r >> 1;
  if(L <= m)  queryy(x, L, R, lson);
  if(R > m)   queryy(x, L, R, rson);
}

void queryx(int L, int R, int Y1, int Y2, int l, int r, int rt){
  if(L <= l && r <= R){
    queryy(rt, Y1, Y2, all);
    return ;
  }
  int m = l + r >> 1;
  if(L <= m)  queryx(L, R, Y1, Y2, lson);
  if(R > m)   queryx(L, R, Y1, Y2, rson);
}

int main(){
  while(scanf("%d", &n) == 1){
    buildx(1, n, 1);
    scanf("%d", &m);
    char op[5];
    int x1, y1, x2 ,y2;
    while(m--){
      scanf("%s %d %d %d", op, &x1, &y1, &x2);
      if(op[0] == ‘c‘)  updatex(x1, y1, x2, all);
      else{
        scanf("%d", &y2);
        mmin = INF;  mmax = 0;
        queryx(x1, x2, y1, y2, all);
        printf("%d %d\n", mmax, mmin);
      }
    }
  }
  return 0;
}

  

UVa 11297 Census (二维线段树)

标签:int   puts   unsigned   ring   cto   ons   scanf   names   sizeof   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7630198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!