[luogu P3801] Red Fantasy Township [line segment tree] [tree array]

Source: Internet
Author: User
Tags x2 y2

Topic Background

Rachel Mealea's red fog changed after the failure, very unwilling.

Title Description

After the last failure, Renee Mealea decided to start the red mist again, but in order to prevent her from being rejected by the reimu, she decided to release the red Fog in a strange place.

We regard Gensokyo as a n*m square area where no area was covered by red fog at First. Each time Lei Mealea stood in a certain area, each of the four directions in the direction of an infinitely long red fog, can affect the whole row/column, but will not affect the area she is standing. If the two arrays of red fog collide, the settlement disappears because of excessive density. Reimu sensed this change and decided to solve it. But before the solution, the spiritual dream is to understand the density of a range of red Fog. Can be described as two operations:

1 x y Lei Mealea stands in the coordinates (x, y) position to release an infinitely long red fog in four Directions.

2 x1 y1 x2 Y2 asked the upper-left point for (x1,y1), the lower-right point is (x2,y2) in the rectangular range, the number of areas covered by red Fog.

input/output Format

Input Format:

The first line of three integers n,m,q, representing the fantasy township size of n*m, there are Q Inquiries.

Next Q line, 3 or 5 integers per line, separated by a space, meaning see the title Description.

Output format:

For each operation 2, the output line is an integer representing the answer to the Query.

Input/output Sample Input Sample # #:
4 4 31 2 21 4 42 1 1 4 4
Sample # # of Output:
8
Description

Sample Explanation:

Use o to indicate no red fog, x indicates red fog, two times after the release of red Fog Fantasy Township map as Follows:

Oxox

xoxo

Oxox

xoxo

Data range:

For 20% of data, 1<=n,m,q<=200

For 40% of data, 1<=n,m,q<=1000

For 100% of data, 1<=n,m,q<=100000

1<=x1,x2,x<=n x1<=x2

1<=y1,y2,y<=m Y1<=y2

by-Orangebird

Orangebird Each game out of line Tree. Qaq

Added a little bit of the line tree problem

actually, it's faster with a tree-shaped array.

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6typedefLong Longll;7 8InlineintRead () {9     Charch;Ten     intRe=0; one     BOOLflag=0; a      while((ch=getchar ())! ='-'&& (ch<'0'|| Ch>'9')); -ch=='-'? flag=1: re=ch-'0'; -      while((ch=getchar ()) >='0'&&ch<='9') re=re*Ten+ch-'0'; the     returnflag?-re:re; - } -  - structsegment{ +     intl,r,num; - }; +  a Const intmaxn=100001; at  -Segment tre[2][maxn<<2]; - intn,m,q; -  - voidPUSH_UP (intXintC) { -tre[c][x].num=tre[c][x<<1].num+tre[c][x<<1|1].num; in } -  to voidBuildintXintLintRintC) { +tre[c][x].l=l; Tre[c][x].r=r; -     if(l==r)return; the     intMid= (l+r) >>1; *Build (x<<1, l,mid,c); Build (x<<1|1, mid+1, r,c); $ }Panax Notoginseng  - voidUpdateintXintPosintC) { the     if(tre[c][x].l==Tre[c][x].r) { +tre[c][x].num^=1; a         return; the     } +      -     intMid= (tre[c][x].l+tre[c][x].r) >>1; $     if(MID&GT;=POS) Update (x<<1, pos,c); $     ElseUpdate (x<<1|1, pos,c); - push_up (x,c); - } the  - intQueryintXintLintRintC) {Wuyi     if(l<=tre[c][x].l&&tre[c][x].r<=r)returntre[c][x].num; the      -     intMid= (tre[c][x].l+tre[c][x].r) >>1; wu     if(r<=mid)returnQuery (x<<1, l,r,c); -     if(l>mid)returnQuery (x<<1|1, l,r,c); about     returnQuery (x<<1, L,mid,c) +query (x<<1|1, mid+1, r,c); $ } -  - intmain () { -     //freopen ("temp.in", "r", stdin); aN=read ();  M=read (); q=Read (); +Build1,1N1); Build1,1M0); the     intopt,x1,x2,y1,y2; - ll ans1,ans2; $      for(intI=0; i<q;i++){ theopt=Read (); the         Switch(opt) { the              case 1:{ theX1=read (); y1=Read (); -Update1, x1,1); Update1, y1,0); in                  break; the             } the              case 2:{ aboutX1=read ();  Y1=read ();  X2=read (); Y2=Read (); theAns1=query (1, x1,x2,1); Ans2=query (1, y1,y2,0); theprintf"%lld\n", ans1* (ll) (y2-y1+1) +ans2* (ll) (x2-x1+1) -2ll*ans1*ans2); the                  break; +             } -         } the     }Bayi}
I forgot to be on the islandforgetting tears is just a jokeForget the Hundred years silent sloganI can't forget you .

[luogu P3801] Red Fantasy Township [line segment tree] [tree array]

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.