Red dreamland, red dreamland

Source: Internet
Author: User

Red dreamland, red dreamland
Background

.

Description

After the last failure, lemilia decided to launch another red mist variant, but to prevent the ghost dream from regressing, she decided to release the red fog in a strange time.

We regard the dreamland as a square area with n * m. At first, no area was covered by red fog. Every time she stood in a certain area, she sent an infinitely long red fog to the four directions in the Southeast and northwest, which could affect the whole row/whole column, but would not affect the area where she stood. If the two red clouds collide, the settlement will disappear because of the high density. Ling Meng noticed this change and decided to solve it. However, before resolving the problem, Ling Meng wanted to know the density of the red fog in a range. You can briefly describe two operations:

1 x y lemilia is standing at the coordinates (x, y) to release infinite red fog in four directions.

2x1 y1 x2 y2 ask about the number of areas covered by red fog in the upper left vertex (x1, y1) and lower right vertex (x2, y2.

Input/Output Format

Input Format:

 

The three integers n, m, and q in the first line indicate that the size of the fantasy Township is n * m and there are q inquiries.

In the next row, there are 3 or 5 integers in each line separated by spaces. For the meanings, see the topic description.

 

Output Format:

 

For each operation 2, an integer is output to indicate the answer to the query.

 

Input and Output sample input sample #1:
4 4 31 2 21 4 42 1 1 4 4
Output sample #1:
8
Description

Example:

Use o to indicate that there is no red fog, and x to indicate that there is a red fog. After the red fog is released twice, the dreamland map is 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

The application of the Line Segment tree.

Use two line segment trees to represent rows and columns, maintain row x, and check whether column y has passed the fog.

Because the two pieces of red fog are offset, it is equivalent to each modification, corresponding row ^ = 1, corresponding column ^ = 1.

Each query, that is, the sum of intervals. Make x = Sigma c1 [x2-x1], y =Σ c2 [y2-y1];

According to the principle, we can know that ans = x * (y2-y1 + 1) + y * (x2-x1 + 1)-x * y * 2;

Remember to open long

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int c1[400001],c2[400001],n,m,q;void addh(int rt,int l,int r,int x){if (l==r){c1[rt]^=1;return;}int mid=(l+r)/2;if (x<=mid) addh(rt*2,l,mid,x);else addh(rt*2+1,mid+1,r,x); c1[rt]=c1[rt*2]+c1[rt*2+1];}void addl(int rt,int l,int r,int x){if (l==r){c2[rt]^=1;return;}int mid=(l+r)/2;if (x<=mid) addl(rt*2,l,mid,x);else addl(rt*2+1,mid+1,r,x); c2[rt]=c2[rt*2]+c2[rt*2+1];}int geth(int rt,int l,int r,int L,int R){if (l>=L&&r<=R){return c1[rt];} int mid=(l+r)/2; int s=0; if (L<=mid) s+=geth(rt*2,l,mid,L,R); if (R>mid) s+=geth(rt*2+1,mid+1,r,L,R);return s;}int getl(int rt,int l,int r,int L,int R){if (l>=L&&r<=R){return c2[rt];} int mid=(l+r)/2; int s=0; if (L<=mid) s+=getl(rt*2,l,mid,L,R); if (R>mid) s+=getl(rt*2+1,mid+1,r,L,R);return s;}int main(){int i,j,ch,x,y,x1,x2,y1,y2;   scanf("%d%d%d",&n,&m,&q);    for (i=1;i<=q;i++)    {    scanf("%d",&ch);    if (ch==1)    {    scanf("%d%d",&x,&y);    addh(1,1,n,x);    addl(1,1,n,y);    }    if (ch==2)    {    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                int x=geth(1,1,n,x1,x2);   int y=getl(1,1,n,y1,y2);   printf("%lld\n",y*(long long)(x2-x1+1)+x*(long long)(y2-y1+1)-(long long)x*y*2);    }    }}

  

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.