[Two-dimensional tree array] counting problem

Source: Internet
Author: User

The title describes a n*m square with an integer weight at the beginning of each lattice. Next time there are 2 actions:
Change the weights of a lattice;
The number of occurrences of a particular weight in a sub-matrix. Enter the first line with two number n,m.
The next n rows, the number of m per line, and the number of I+1 J indicates the initial weight of the lattice (i,j).
Next, enter an integer Q.
Next the Q line, each line describes an operation.
Action 1: "1 x y C" (with no double quotes). Indicates that the weight of the lattice (x, y) is changed to C (1<=x<=n,1<=y<=m,1<=c<=100).
Action 2: "2 x1 x2 y1 y2 C" (without double quotes, x1<=x2,y1<=y2). The number of squares (x, y) that satisfies the lattice weight of C and X1<=x<=x2,y1<=y<=y2 is queried. Output for each operation 2, follow the order in which they appear in the input, and then output one line at a single integer to indicate the number of rows evaluated. Sample input
3 31 2 33 2 12 1 332 1 2 1 2 11 2 3 22 2 3 2 3 2
Sample output
12
Tips

For 30% of data n,m<=30,q<=10000
For 100% of data n,m<=300,q<=100000,1<=c<=100

Idea: Ask the number of occurrences of a certain value in the interval; single point update: If a number k is marked as 1 (three-bit array c[i][j][k] in a certain location (I,J), the original number is marked as 0, the interval sum: The number of k occurrences in the interval is called Getsum (I,j, k) sum c[...] [..] [K] Array to get (I,J) the number of K occurrences, the result is getsum (xx,yy,k)-getsum (xx,y-1,k)-getsum (x-1,yy,k) +getsum (x-1,y-1,k);(the principle of Repulsion) AC code:
#include <iostream>#include<cstdio>#defineLowbit (x) x& (-X)using namespacestd;intn,m;intmap[310][310];intc[310][310][ the];voidAddintXintYintKintval) {    for(inti=x;i<=n;i+=lowbit (i)) {      for(intj=y;j<=m;j+=Lowbit (j)) {C[i][j][k]+=Val; }   }}intGetsum (intXintYintk) {   intret=0;  for(intI=x;i>0; i-=lowbit (i)) {      for(intJ=y;j>0; j-=Lowbit (j)) {ret+=C[i][j][k]; }   }   returnret;}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++){         for(intj=1; j<=m;j++) {scanf ("%d",&Map[i][j]); Add (I,j,map[i][j],1); }    }    intQ; scanf ("%d",&q);  while(q--){        intop; scanf ("%d",&op); if(op==1){            intx,y,k; scanf ("%d%d%d",&x,&y,&k); Add (X,y,map[x][y],-1); Add (X,y,k,1); Map[x][y]=K; }        if(op==2){            intx,xx,y,yy,k; scanf ("%d%d%d%d%d",&x,&xx,&y,&yy,&k); printf ("%d\n", Getsum (xx,yy,k)-getsum (X-1, yy,k)-getsum (xx,y-1, k) +getsum (X-1, Y1, K)); }    }    return 0;}

[Two-dimensional tree array] counting problem

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.