"Bzoj3110" [zjoi2013]k large number query integral binary + Tree array interval modification

Source: Internet
Author: User

Title Description

There are n positions, m operations. There are two types of operations, each of which is represented as a 1 a B C in the form A to position B, and a number C for each position. In the case of a 2 a B C, it is asked to ask from position A to position B, and the number of C large.

Input

First line N,m
Next m line, each line shaped like 1 a b C or 2 a B C

Output

Output the results of each query

Sample input

2 5
1 1 2 1
1 1 2 2
2 1 1 2
2 1 1 1
2 1 2 3

Sample output

1
2
1

Exercises

Integral binary + Tree array interval modification

The tree-set tree of naive in that year

Two days ago because to speak the whole two points, so I yy out of this with modified integral two-point notation:

Since the weights of each insertion are fixed, it is possible to put the changes and queries together in the whole two points.

Set $solve (B,E,L,R) $ To resolve changes and inquiries in $[b,e]$, where: The weight of the change, the answer to the inquiry is within the $[l,r]$.

Then if $l =r$, the answer to all queries within the interval is equal to $l $.

Otherwise set $mid =\frac{l+r}2$, and then scan through the time sequence to modify and inquire.

For modification: If the weight of the $[l,mid]$ is dropped to the left interval, otherwise the whole interval of +1 and dropped to the right interval.

For inquiries: If the interval and (the actual meaning of the number of $[mid+1,r]$ within the interval of the time of the inquiry) is greater than or equal to the query $c $, indicating that there are enough numbers within the $[mid+1,r]$, the answer is within the $[mid+1,r]$, and dropped to the right interval; otherwise $c $ Subtract this interval and drop it to the left interval.

Empty the data structure, recursive left and right intervals. Note that the maintenance time sequence is also required for the left and right intervals.

Need to implement: interval modification, Interval summation, you can use the tree-like array to modify the implementation of the interval. Refer to "bzoj3132" for the seven minutes of God's problem.

Time complexity $O (N\LOG^2N) $, more than 10 times times faster than tree nesting trees ... (Of course, it may be that I did not write well)

Note that the subject explodes int and therefore requires unsigned int (slightly faster than a long long).

#include <cstdio> #define N 50010typedef unsigned int uint;struct data{int opt, a, B, C, id;} Q[n], T[n];int flag[n], ans[n], n;struct bit{uint v[n];inline void Add (int x, int a) {int i;for (i = x; I <= N; i + = i & i) v[i] + = A; inline UINT Query (int x) {int i;uint ans = 0;for (i = x; i; i-= i & i) ans + = V[i];return ans;}} A, b;inline void Modify (int x, int a) {A.add (x, a), B.add (x, A * x);} inline UINT Ask (int x) {return (x + 1) * A.query (x)-b.query (x);} void Solve (int b, int e, int l, int r) {if (L = = r) {int i;for (i = b; I <= e; i + +) if (q[i].opt = = 2) ans[q[i].id] = L ; return;} int mid = (L + r) >> 1, I, LP = b-1, RP = e + 1;uint v;for (i = b; I <= e; i + +) {if (q[i].opt = 1) {if (Q[i] . C > Mid) Modify (Q[I].A, 1), modify (q[i].b + 1,-1), T[--RP] = q[i];else T[++LP] = q[i];} Else{v = Ask (q[i].b)-Ask (Q[i].a-1), if (UINT) q[i].c <= v) t[--rp] = Q[i];else q[i].c = V, t[++lp] = Q[i];}} for (i = b; I <= e; i + +) if (q[i].opt = = 1&& q[i].c > Mid) Modify (Q[I].A,-1), modify (q[i].b + 1, 1); for (i = b; i <= LP; i + +) q[i] = T[i];for (i = RP; I <= e; i + +) q[i] = t[e + rp-i];solve (b, LP, L, mid), solve (RP, E, mid + 1, r);} int main () {int m, i;scanf ("%d%d", &n, &m); for (i = 1; I <= m; i + +) {scanf ("%d%d%d%d", &q[i].opt, &am P;Q[I].A, &q[i].b, &q[i].c), q[i].id = i;if (q[i].opt = = 2) flag[i] = 1;} Solve (1, M,-N, N), for (i = 1; I <= m; i + +) if (Flag[i]) printf ("%d\n", Ans[i]); return 0;}

"Bzoj3110" [zjoi2013]k large number query integral binary + Tree array interval modification

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.