"2018 National multi-school algorithm winter training Camp Practice Competition (fifth)-E" Valentine's Day light bulb (two-dimensional tree array single point update + interval query)

Source: Internet
Author: User
Tags ming

Question Link: Https://www.nowcoder.com/acm/contest/77/E

Title DescriptionValentine's Day, small Fang and Xiao-Ming hand-in-hand, intends to have a perfect Valentine's Day, but the small just came, when a shining light bulb, Xiao Ming is very embarrassed, and Xiao just said, I give you a task, you finished we will take you to play, otherwise you will go home. Xiao Gang has the consciousness of being a single dog, he is determined not to let Xiao Ming have a good Valentine's Day, the same as a single dog can you help him? Now there is a NXN (1 <= n <= 1000) of the lattice, each lattice has a light bulb, may be bright, may also be extinguished (1 is light, 0 means out), now there are two operations, one is to give you a coordinate, for that coordinate on the light bulb, if he is bright, then extinguish him, Conversely, if he is extinguished, open it. The second operation is to give you two coordinates, the first coordinate represents the upper-left corner of a sub-matrix, and the other coordinate is the lower-right corner, please find out how many bulbs in the current sub-matrix are lit. Dry up!!! Single dogs!!!! Input Description:
The first line is two integers, n (1 <= n <= 1000) and M (1 <= m <= 100000), representing the number of Bingchang queries for the square lattice, respectively.
Next n rows, each row has n bool-shaped digits (0 or 1), which represent the status of the bulb.
The next M-line, the first number F (1 or 2) for each line represents the type of operation, and if F is 1, then enter a coordinate (x, y) (1 <= x, y <= N), change the bulb status of the current position, and if 2, then enter two coordinates (x1, y1) (1 <= x1, y1 <= N), (x2, y2) (1 <= x2, y2 <= N), determines the position of the sub-matrix, outputs the number of light bulbs in the sub-matrix, and wraps the line.
Output Description:
For each 2 operation, the number of light bulbs in the output sub-matrix is changed and the line is wrapped.
Input
6 40 0 1 0 1 01 0 1 1 0 11 0 0 0 0 01 1 0 0 1 00 0 0 0 1 10 0 0 0 1 02 2 2 4 51 1 12 1 1 6 61 2 6
Output
414
Code
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<cmath>using namespacestd;Const intN =1004; typedefLong LongLL;intC[n][n], N;intLowbit (intt) {    returnt& (-t);}voidUpdateintXintYintm) {     for(intI=x; i<=n; i+=lowbit (i)) for(intJ=y; j<=n; j+=Lowbit (j)) C[i][j]+=m;}intQueryintXinty) {    intsum=0;  for(intI=x; I>0; i-=lowbit (i)) for(intJ=y; J>0; j-=Lowbit (j)) Sum+=C[i][j]; returnsum;}intMain () {inti,j,m,k; scanf ("%d%d",&n,&m);  for(i=1; i<=n; i++)         for(j=1; j<=n; J + +) {scanf ("%d",&k);        Update (I,J,K); }     while(m--)    {        intT,x,y; scanf ("%d", &t); if(t==1) {scanf ("%d%d",&x,&y); intS1=query (x, y)-query (X-1, y)-query (x,y-1) +query (x1, Y1); if(S1) Update (x,y,-1); ElseUpdate (x, Y,1); }        Else        {            intX1,x2,y1,y2; scanf ("%d%d%d%d", &x1,&y1,&x2,&y2); printf ("%d\n", query (x2,y2)-query (x1-1, y2)-query (x2,y1-1) +query (x1-1, y1-1)); }    }    return 0;}

"2018 National multi-school algorithm winter training Camp Practice Competition (fifth)-E" Valentine's Day light bulb (two-dimensional tree array single point update + interval query)

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.