[Ultraviolet A] 11992-Fast Matrix Operations (line segment tree template)

Source: Internet
Author: User

The basic line segment tree should be noted that, due to the set and add operations, when the push-down is marked as lazy, the set is given priority, and then the ADD is given priority. Each time you execute the set operation, the ADD is marked as 0.

Wa has been used several times because of a problem during the Computing period. The funny thing is that I found the template for more than an hour.

# Include <cstdio> # include <cmath> # include <queue> # include <stack> # include <map> # include <algorithm> using namespace STD; # define lson POS <1 # define rson POS <1 | 1 typedef unsigned long ull; typedef long ll; const int maxn = 1111111; const int INF = 1 <30; struct node {int left, right; int max_value, min_value, sum_value; int Col; int col2;} tr [maxn <2]; struct ans {int min_value; int max_value; int Sum_value; ans (int A = inf, int B = 0, int C = 0): min_value (A), max_value (B), sum_value (c ){};}; int n, m, K; void Pushdown (int pos) {If (TR [POS]. COL) {int len1 = tr [lson]. right-Tr [lson]. left + 1; int len2 = tr [rson]. right-Tr [rson]. left + 1; tr [lson]. sum_value + = (TR [POS]. col * len1); tr [lson]. max_value + = tr [POS]. col; tr [lson]. min_value + = tr [POS]. col; tr [rson]. sum_value + = (TR [POS]. col * len2); tr [RSO N]. max_value + = tr [POS]. col; tr [rson]. min_value + = tr [POS]. col; tr [lson]. col + = tr [POS]. col; tr [rson]. col + = tr [POS]. col; tr [POS]. col = 0;} return;} void pushdown2 (int pos) {If (TR [POS]. col2> = 0) {int len1 = tr [lson]. right-Tr [lson]. left + 1; int len2 = tr [rson]. right-Tr [rson]. left + 1; tr [lson]. sum_value = (TR [POS]. col2 * len1); tr [lson]. max_value = tr [POS]. col2; tr [lson]. min_value = tr [POS]. Col2; tr [rson]. sum_value = (TR [POS]. col2 * len2); tr [rson]. max_value = tr [POS]. col2; tr [rson]. min_value = tr [POS]. col2; tr [lson]. col2 = tr [POS]. col2; tr [lson]. col = 0; // set recursion requires clearing the sum tr [rson]. col2 = tr [POS]. col2; tr [rson]. col = 0; tr [POS]. col2 =-1;} return;} void pushup (int pos) {tr [POS]. sum_value = tr [lson]. sum_value + Tr [rson]. sum_value; tr [POS]. max_value = max (TR [lson]. max_value, TR [rson]. max_v Alue); tr [POS]. min_value = min (TR [lson]. min_value, TR [rson]. min_value); return;} void buildtree (int l, int R, int POS) {tr [POS]. left = L; tr [POS]. right = r; tr [POS]. col = 0; tr [POS]. col2 =-1; tr [POS]. max_value = 0; tr [POS]. min_value = 0; tr [POS]. sum_value = 0; If (L = r) return; int M = (L + r)> 1; buildtree (L, M, lson); buildtree (m + 1, r, rson); return;} void treeadd (int l, int R, int add, int POS) {if (L <= tr [POS]. left & tr [POS]. right <= r) {int Len = tr [POS]. right-Tr [POS]. left + 1; tr [POS]. max_value + = add; tr [POS]. min_value + = add; tr [POS]. sum_value + = (add * Len); tr [POS]. col + = add; return;} pushdown2 (POS); Pushdown (POS); int M = (TR [POS]. left + Tr [POS]. right)> 1; if (L <= m) treeadd (L, R, add, lson); If (r> m) treeadd (L, R, add, rson); pushup (POS);} void treeset (int l, int R, int value, int P OS) {If (L <= tr [POS]. left & tr [POS]. right <= r) {int Len = tr [POS]. right-Tr [POS]. left + 1; tr [POS]. max_value = value; tr [POS]. min_value = value; tr [POS]. sum_value = (value * Len); tr [POS]. col2 = value; tr [POS]. col = 0; return;} pushdown2 (POS); Pushdown (POS); int M = (TR [POS]. left + Tr [POS]. right)> 1; if (L <= m) treeset (L, R, value, lson); If (r> m) treeset (L, R, value, rson); pushup (POS);} ans quer Y (int l, int R, int POS) {If (L <= tr [POS]. left & tr [POS]. right <= r) {return ans (TR [POS]. min_value, TR [POS]. max_value, TR [POS]. sum_value) ;}pushdown2 (POS); Pushdown (POS); int M = (TR [POS]. left + Tr [POS]. right)> 1; ans a, B; If (L <= m) A = query (L, R, lson); If (r> m) B = query (L, R, rson); pushup (POS); Return ans (min (. min_value, B. min_value), max (. max_value, B. max_value),. sum_value + B. sum_value);} int Main () {While (scanf ("% d", & N, & M, & K )! = EOF) {buildtree (1, N * m, 1); While (k --) {int t; scanf ("% d", & T ); if (t = 1) {int X1, Y1, X2, Y2, V; // Add scanf ("% d", & X1, & Y1, & X2, & Y2, & V); For (INT I = x1; I <= x2; I ++) {int L = (I-1) * m + Y1; int r = (I-1) * m + y2; treeadd (L, R, V, 1) ;}} else if (t = 2) {int X1, Y1, X2, Y2, V; // Add scanf ("% d", & X1, & Y1, & X2, & Y2, & V); For (INT I = x1; I <= x2; I ++) {int L = (I-1) * m + Y1; int r = (I-1) * m + y2; treeset (L, R, V, 1) ;}} else if (t = 3) {int X1, Y1, x2, Y2; ans t; int max = 0, min = inf, sum = 0; scanf ("% d", & X1, & Y1, & X2, & Y2); For (INT I = x1; I <= x2; I ++) {int L = (I-1) * m + Y1; int r = (I-1) * m + y2; t = query (L, R, 1); sum + = T. sum_value; max = max (max, T. max_value); min = min (Min, T. min_value);} printf ("% d \ n", sum, Min, max) ;}} return 0 ;}

[Ultraviolet A] 11992-Fast Matrix Operations (line segment tree template)

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.