POJ1195 Mobile Phones "tree-shaped Array" "two-dimensional"

Source: Internet
Author: User
Tags x2 y2

Topic Links:

http://poj.org/problem?id=1195


Main topic:

A n*n matrix is given, the initialization is 0, and the coordinates start at (0,0). There are three operations:

Command 1:1 x y w; increase the point value at coordinates (x, y) by W

Command 2:2 x1 y1 x2 y2; Ask the lower-left corner coordinates (X1,Y1), the upper-right coordinate (x2,y2), and the number of matrices

command 3:3; No action required, exit.


Ideas:

Two-bit tree array single point update, interval evaluation of the simple problem. You can do it directly. At the end of the Matrix, the theorem of the repulsion is considered.

That is, ans = Query (x1-1,y1-1)-query (x1-1,y2)-query (x2,y1-1) +query (x2,y2).


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define LL __ int64using namespace Std;const int maxn = 1100;int Tree[maxn][maxn],n = 1025;int lowbit (int x) {return x & (-X);} void Update (int x,int y,int W) {for (int i = x; i <= n; i + = Lowbit (i)) for (int j = y; J <= N; j + = Lowbit ( j)) Tree[i][j] + = w;}    ll Query (int x,int y) {ll sum = 0;    for (int i = x; i > 0; I-= Lowbit (i)) for (int j = y; j > 0; J-= Lowbit (j)) sum + = Tree[i][j]; return sum;}    int main () {int op,n;        while (~SCANF ("%d%d", &op, &n)) {memset (tree,0,sizeof (Tree));                while (~SCANF ("%d", &op) && op! = 3) {if (op = = 1) {int x,y,w;                scanf ("%d%d%d", &x,&y,&w);            Update (X+1,Y+1,W);                } else if (op = = 2) {int x1,y1,x2,y2; scanf ("%d%d%d%d", &x1, &y1,&x2,&y2);                x1++,y1++,x2++,y2++;            printf ("%i64d\n", Query (x1-1,y1-1)-query (x1-1,y2)-query (x2,y1-1) +query (x2,y2)); }}} return 0;}


POJ1195 Mobile Phones "tree-shaped Array" "two-dimensional"

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.