Topic Link: Click to open the link
Test instructions: A two-dimensional planar w operation for a given n*m
1, 0 (x1,y1) (x2,y2) value
For i:x1 to X2
For J:y1 to Y2
MP[I][J] + = value;
2, 1 (x1, y1) (x2 y2)
ANS1 = total number of ordinate between y1,y2
ANS2 = The total number of horizontal axis not x1,x2 between
Puts (ANS1-ANS2);
Because the n maximum is 4e6, the segment is replaced by a tree-shaped array to replace segment tree
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream>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 ');} Using namespace Std;typedef long long ll;const int N = 4e6 + 100;template<class t>struct tree{t c[2][n];int maxn;voi d init (int x) {maxn = x+10; memset (c, 0, sizeof c);} inline int lowbit (int x) {return x&-x;} T sum (t *b, int x) {T ans = 0;if (x = = 0) ans = b[0];while (x) ans + = b[x], X-= Lowbit (x); return ans; void Change (t *b, int x, t value) {if (x = = 0) b[x] + = value, X++;while (x <= maxn) b[x] + = value, x + = Lowbit (x);} T GET_PRE (int r) {return sum (c[0], R) * r + sum (c[1], r);} void Add (int l, int r, T value) {change (c[0], L, value); Change (c[0], R + 1,-value); Change (c[1], L, Value * (-L + 1)); Chang E (c[1], R + 1, value * r);} T get (int l, int r) {return Get_pre (R)-Get_pre (L-1);}}; Tree<ll> x, Y;int main () {int n, m, W;rd (n); Rd (M); Rd (W); X.init (n); Y.init (m); ll all = 0;while (w--) {int op, x1, x2, Y1, Y2; ll value;rd (OP); RD (X1); RD (Y1); RD (x2); RD (Y2), if (op = = 0) {rd (value); all + = value * (x2-x1 + 1) * (y2-y1 + 1); X.add (x1, x2, Value * (Y2-y1 + 1)); Y.add (Y1, Y2, Value * (x2-x1 + 1));} Else{pt (Y.get (1, y2)-y.get (1, y1-1)-(All-x.get (1, x2) + x.get (1, x1-1)); puts ("");}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 390E Inna and Large Sweet Matrix tree array for segment refinement