Uva 11297 Census two-dimensional line segment tree

Source: Internet
Author: User

Topic Link: Click to open the link

Long time no More,

The first dimension of the segment tree is updated to the end, the leaf nodes build a tree line.


#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <map># include<vector> #include <set> #include <string> #include <algorithm>using namespace std; Template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-') && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} const int N = 550;const int inf = $ * + * 1000;int a[n][n];class segment_tree{public:class node{public:int l, R, Max , Min, lazy; Node () {}node (int ll, int rr, int mmax, int mmin) {L = ll; r = RR; max = Mmax; min = Mmin;lazy = inf;}}; Node t[n << 2];int pos = 0, Max, min;int L (int x) {return x << 1;} int R (inT x) {return x << 1 | 1;} void up (int id) {T[id].max = max (t[l (ID)].max, t[r (ID)].max); t[id].min = min (t[l (ID)].min, t[r (ID)].min); t[id].lazy = inf ;} void down (int id) {if (T[id].lazy! = inf) {t[l (id)].max = t[r (id)].max = t[id].lazy;t[l (id)].min = t[r (id)].min = t[id].lazy ; t[l (id)].lazy = t[r (id)].lazy = T[id].lazy;t[id].lazy = inf;}} void build (int l, int r, int id) {T[id] = Node (l, r, 0, 0); if (L = = r) {t[id].min = T[id].max = A[pos][l];return;} int mid = (L + R) >> 1;build (L, Mid, L (ID)); Build (mid + 1, R, R (ID)), up (ID);} void Update (int l, int r, int val, int id) {if (L = = T[id].l && r = = T[ID].R) {T[id].max = T[id].min = T[id].lazy = Val;return;} Down (ID), int mid = (t[id].l + t[id].r) >> 1;if (r <= Mid) update (L, R, Val, L (ID)), else if (Mid < L) Update (L, R , Val, R (ID)), Else{update (L, Mid, Val, L (ID)); Update (mid + 1, R, Val, R (ID));} Up (ID);} void query (int l, int r, int id) {if (L = = T[id].l && r = = t[id].r) {min = min (min, t[id].min); max = max (max, T[ID].MAX); return;} Down (ID), int mid = (t[id].l + t[id].r) >> 1;if (r <= Mid) query (L, R, L (ID)), else if (Mid < L) query (L, R, R (ID) ); Else{query (L, Mid, L (ID)); query (mid + 1, R, R (ID));}} void Q (int l, int r) {Max =-inf; Min = Inf;query (L, R, 1);} void P (int l, int r, int val) {update (L, R, Val, 1);}}; Class Two_dimension_segment_tree{public:class Node{public:int L, R; Node () {}node (int ll, int rr) {L = ll; r = RR;}}; Node t[n << 2]; Segment_tree t[n];int Max, Min, m;int L (int x) {return x << 1;} int R (int x) {return x << 1 | 1;} void build (int l, int r, int id) {T[id] = Node (l, R), if (L = = r) {t[l].pos = l; T[l].build (1, M, 1); return;} int mid = (L + R) >> 1;build (L, Mid, L (ID)); Build (mid + 1, R, R (ID));} void Update (int l, int r, int x, int y, int val, int id) {if (L = = r) {T[l]. P (x, Y, Val); return;} int mid = (t[id].l + t[id].r) >> 1;if (r <= Mid) update (L, R, X, Y, Val, l (ID)), else if (Mid < L) Update (L, R, X , Y, Val, R (id)); Else{update (L, Mid, X, Y, Val, l (ID)); Update (mid + 1, r, X, Y, Val, R (ID));}} void query (int l, int r, int x, int y, int id) {if (L = = r) {T[l]. Q (x, y); max = max (max, T[l]. MAX); min = min (min, t[l]. (Min); return;} int mid = (t[id].l + t[id].r) >> 1;if (r <= Mid) query (L, R, X, Y, L (ID)), else if (Mid < L) query (L, R, X, Y, R ( ID)); Else{query (L, Mid, X, Y, L (ID)); query (mid + 1, r, X, Y, R (ID));}} void Q (int l, int r, int x, int y) {Max =-inf; Min = Inf;query (L, R, X, y, 1);} void P (int l, int r, int x, int y, int val) {update (L, R, X, Y, Val, 1);}}; int n, m; Two_dimension_segment_tree tree;void Input () {rd (n); Rd (M); for (int i = 1, i <= N; i++) for (int j = 1; j <= M; j + +) Rd (A[i][j]);} int main () {input (), tree. M = M;tree.build (1, N, 1); int q; RD (q); string s; int L, r, X, Y, val;while (q--> 0) {cin >> s;if (s[0] = = ' Q ') {rd (L); Rd (x); Rd (R); Rd (y); tree. Q (L, R, X, y);p rintf ("%d%d\n", tree. Max, tree. Min);} else {rd (x); Rd (y); Rd (val); tree. P (x, x, Y, Y, val);}} return 0;}

Java:

Import Java.io.printwriter;import java.util.arraylist;import Java.util.arrays;import Java.util.Collections;import  Java.util.iterator;import Java.util.scanner;import Java.util.treeset;public class Main {static int N = 550;static int inf = 1000*1000*1000;int[][] A = new Int[n][n];class Segment_tree{class node{int L, R, Max, Min, lazy; Node () {}node (int l, int r, int max, int min) {this.l = l; this.r = r; this.max = max; this.min = Min;lazy = inf;}} Public node[] t = new node[n<<2];p ublic int pos = 0, Max, min;int L (int x) {return x<<1;} int R (int x) {return x<<1|1;} void up (int id) {T[id].max = max (t[l (ID)].max, t[r (ID)].max); t[id].min = min (t[l (ID)].min, t[r (ID)].min); t[id].lazy = inf ;} void down (int id) {if (T[id].lazy! = inf) {t[l (id)].max = t[r (id)].max = t[id].lazy;t[l (id)].min = t[r (id)].min = t[id].lazy; T[l (id)].lazy = t[r (id)].lazy = T[id].lazy;t[id].lazy = inf;}} public void Build (int l, int r, int id) {T[id] = new Node (l, r, 0, 0); if (L = = r) {t[id].min = T[id].max = A[pos][l]; return;} int mid = (l+r) >>1;build (L, Mid, L (ID)); Build (Mid+1, R, R (ID)), up (ID);} void Update (int l, int r, int val, int id) {if (L = = T[id].l && r = = T[ID].R) {T[id].max = T[id].min = T[id].lazy = V Al;return;} Down (ID), int mid = (t[id].l + T[ID].R) >>1;if (r <= Mid) update (L, R, Val, L (ID)), else if (Mid < L) Update (L, R, VA L, R (ID)); Else{update (L, Mid, Val, L (ID)); Update (mid+1, R, Val, R (ID));} Up (ID);} void query (int l, int r, int id) {if (L = = T[id].l && r = = t[id].r) {min = min (min, t[id].min); max = max (max, T[id].max); return;} Down (ID), int mid = (t[id].l + T[ID].R) >>1;if (r <= Mid) query (L, R, L (ID)), else if (Mid < L) query (L, R, R (ID)); El Se{query (L, Mid, L (ID)); query (Mid+1, R, R (ID));}} public void Q (int l, int r) {Max =-inf; Min = Inf;query (L, R, 1);} public void P (int l, int r, int val) {update (L, R, Val, 1);}} Class Two_dimension_segment_tree{class Node{int L, R; Node () {}node (int l, int r) {THIS.L = l; this.r = r;}} node[] t = new node[n<<2]; Segment_tree[] T = new Segment_tree[n];int Max, Min, m;int L (int x) {return x<<1;} int R (int x) {return x<<1|1;} void build (int l, int r, int id) {T[id] = new Node (l, R),//system.out.println (+ "" +r), if (L = = r) {T[l] = new Segment_tree () ; T[l].pos = l; T[l].build (1, M, 1); return;} int mid = (l+r) >>1;build (L, Mid, L (ID)); Build (Mid+1, R, R (ID));} void Update (int l, int r, int x, int y, int val, int id) {if (L = = r) {T[l]. P (x, Y, Val); return;}  int mid = (t[id].l + T[ID].R) >>1;if (r <= Mid) update (L, R, X, Y, Val, l (ID)), else if (Mid < L) Update (L, R, X, Y, Val, R (ID)); Else{update (L, Mid, X, Y, Val, l (ID)); Update (mid+1, R, X, Y, Val, R (ID));}} void query (int l, int r, int x, int y, int id) {if (L = = r) {T[l]. Q (x, y); max = max (max, T[l]. MAX); min = min (min, t[l]. (Min); return;} int mid = (t[id].l + T[ID].R) >>1;if (r <= Mid) query (L, R, X, Y, L (ID)), else if (Mid < L) query (L, R, X, Y, R (ID)) ; Else{query (L, Mid, X, Y, L (ID)); query (mid+1, R, X, Y, R (ID));}} void Q (int l, int r, int x, int Y) {Max =-inf; Min = Inf;query (L, R, X, y, 1);} void P (int l, int r, int x, int y, int val) {update (L, R, X, Y, Val, 1);}} int n, m; Two_dimension_segment_tree Tree = new Two_dimension_segment_tree (), void input () {n = cin.nextint (); m = Cin.nextint (); for (int i = 1; I <= n; i++) for (int j = 1; j <= M; j + +) A[i][j] = Cin.nextint ();} void work () {input (); tree. M = M;tree.build (1, N, 1); int q; Q = Cin.nextint (); String s; int L, r, X, Y, val;while (q--> 0) {s = Cin.next (), if (S.charat (0) = = ' Q ') {L = Cin.nextint (); x = Cin.nextint (); r = cin. Nextint (); Y =cin.nextint (); tree. Q (L, R, X, y); out.println (tree). max+ "" +tree. Min);} else {x = Cin.nextint (); y = Cin.nextint (); val = Cin.nextint (); tree. P (x, x, Y, Y, val);}}} Main () {cin = new Scanner (system.in); out = new PrintWriter (System.out);} public static void Main (string[] args) {main E = new main (); E.work (); Out.close ();} Public Scanner cin;public static PrintWriter out;class queue {int[] queue = new Int[100000];int front, rear;void clear () { Front = Rear = 0;} Boolean empty () {return front = = rear;} int size () {return rear-front;} void push (int x) {queue[rear++] = x;} int Top () {return queue[front];} void Pop () {front++;}} int max (int x, int y) {return x > y x:y;} int min (int x, int y) {return x < y x:y;} Double Max (double x, double y) {return x > y x:y;} Double min (double x, double y) {return x < y x:y;} static double EPS = 1e-8;double abs (double x) {return x > 0 ×:-X;} Boolean zero (double x) {return abs (x) < EPS;}}


Uva 11297 Census two-dimensional line segment tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.