P3801 red fantasy township

Source: Internet
Author: User

Basic Line Segment tree and rejection Principle

Good food for me!

In fact, there was a slight turn in the question: she separated the two fog sets and settled and placed them in the same place.

However, you only need to draw a graph and you will find that, once there is a cross between the marked rows and columns, this point is useless.

Putting fog on one point is equivalent to putting fog on one line and one column.

The fog in a region is equivalent to the total area occupied by all marked rows and columns and then the area that falls.

It is not hard to find: when it is in the row\ (X \)Fog, put in the column\ (Y \)When it's foggy\ (Xy \).

In fact, it is not difficult to calculate it. You will know it at a glance.

Code:

# Include <cstdio> const int maxn = 100005; struct segtree {int sum [maxn <2]; # define lson (root <1) # define rson (root <1 | 1) void pushup (INT root) {sum [root] = sum [lson] + sum [rson];} void Update (INT root, int L, int R, int p) {If (L = r) sum [root] ^ = 1; else {int mid = (L + r)> 1; if (P <= mid) Update (lson, L, mid, P); else Update (rson, Mid + 1, R, P); pushup (Root );}} int query (INT root, int L, int R, int X, int y) {If (r <X | Y <L) return 0; if (x <= L & R <= y) return sum [root]; int mid = (L + r)> 1; return query (lson, L, mid, x, Y) + query (rson, Mid + 1, R, x, y) ;}# UNDEF lson # UNDEF rson} seg1, seg2; int n, m, Q; int read () {int ans = 0, S = 1; char CH = getchar (); While (CH> '9' | ch <'0 ') {If (CH = '-') S =-1; CH = getchar () ;}while (CH> = '0' & Ch <= '9 ') ans = ans * 10 + CH-'0', CH = getchar (); Return S * ans;} int main () {n = read (), M = read (), q = read (); While (Q --) {int opt = read (); If (OPT = 1) {int x = read (), y = read (); seg1.update (1, 1, n, x); seg2.update (1, 1, n, Y);} else if (OPT = 2) {int x = read (), Y = read (), xx = read (), YY = read (); int RES1 = seg1.query (1, 1, n, x, XX); int RES2 = seg2.query (1, 1, n, Y, YY); printf ("% LLD \ n", 1ll * RES1 * (yy-y + 1) + 1ll * RES2 * (XX-x + 1)-2 * RES1 * RES2);} return 0 ;}

p3801 red fantasy township

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.