"bzoj4785" [Zjoi2017] Tree Array segment tree Set segment tree

Source: Internet
Author: User
Tags pow

Title Description

In the dark night, nine poor lying in bed tossing and turning. Unable to sleep, she remembered her tragic Oi match experience several years ago. That's a basic tree-like array problem. Gives an array of length n, with an initial value of 0, followed by M operations with two operations:

1 x, which means to turn ax into (ax + 1) mod 2. 2 L R, asking for Sigma (Ai) mod 2,l<=i<=r Although the poor and simple at that time, she still found that the problem can be done in a tree-like array. At the time, very young, she wrote the following algorithm: 1:function Add (x) 2:while x > 0 do3:ax← (Ax + 1) mod 24:x←x-lowbit (x) 5:end while6:end Fun Ction7:8: Function Find (x) 9:if x = = 0 Then10:return 011:end if12:ans←013:while x≤n do14:ans← (ans + Ax) mod 215 : X←x + lowbit (x) 16:end While17:return ans18:end function19:20:function Query (l, R) 21:ansl←find (L-1) 22:ansr← Find (R) 23:return (ANSR? Ansl + 2) mod 224:end function where Lowbit (x) represents the highest non 0 bits of the number x, such as lowbit (5) = 1, lowbit (12) = 4. Call ADD (x) When doing the first type of operation, and the answer to the second type of operation is Query (L, R). If you're familiar with a tree array, it's easy to find that the poor tree-like array is incorrectly written: The x change in Add and find is in the opposite direction. So the program exploded 0 in the final test. Oddly, however, at the time, the program passed the bulk example given by the person who gave the question-and that's why it was pathetic. Now, poor want to calculate, this program answer to each of the probability of the inquiry, so that she can again feel that she is a very non-person. However, the time has passed for many years, even the poor have no way to fully recall the bulk case. Fortunately, she recalls most of the content, the only thing forgotten is the value of x for each of the first operations, so she assumes that the X of this operation is random in the range of [Li, RI]. Specifically, the poor gave an array of length n, an initial of 0, followed by M operations: 1 L R, which indicates the medium probability of selecting an x in the interval [L, R] and performing the ADD (x)。 2 L R, which indicates the probability of asking the query (L, R) to get the correct number of results.

Input

The first line inputs two integers n, M. The next m line describes an operation, formatted as shown in the topic. N<=10^5,m<=10^5,1<=l<=r<=n

Output

For each set of queries, output an integer to indicate the answer. If the answer is in the shape of the simplest score, such as X/y, then you only need to output the value of X*y^?1 mod 998244353. (i.e. output answer modulo 998244353).

Sample input

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

Sample output

1
0
665496236

Exercises

Line segment tree Set segment tree

"If you are familiar with the tree array, it is not difficult to find" the tree array in the subject is the suffix and.

Then when the $l-1\neq 0$ (equals 0 o'clock), the result is $\sum\limits_{i=l-1}^{r-1}a_i$, if it is equal to $\sum\limits_{i=l}^ra_i$, then $a_{l-1}=a_r$ is required. So just ask for the probability of $a_{l-1}=a_r$.

We think, for the modification operation [L,r], if the left endpoint T and the right endpoint K have been determined, how do you update the probability that T is equal to K (k>t)?

Must be discussed in a situation, of course, only when $t$ or $k\in[l,r]$ will have an impact.

1. When $t\in[1,l-1]$, $k \in[l,r]$, the probability of not affecting is 1-p

2. When $t\in[l,r]$, $k \in[l,r]$, the probability of not affecting is 1-2p

3. When $t\in[l,r]$, $k \in[r+1,n]$, the probability of not affecting is 1-p.

If T is identified, we can obviously use the segment tree to maintain these three sections. As for the probability of the problem, if the original equal probability of p, does not affect the probability of Q, then the new equal probability is obviously $p q-(1-p) (1-Q) $. And this expression satisfies the commutative law and the binding law, so the order of updates is not to be considered (and can be tagged permanently).

Because the existence of T is also a continuous interval, so we also need a line tree to maintain the left endpoint T, so we need line segment tree, that is, two-dimensional line segment tree.

Implementation: The use of a similar to the idea of permanent, the selection of a layer of outer and inner interval, the (outer layer corresponding to the outer node) corresponding (the inner interval corresponding to the inner node) is updated.

As for query [L,r], the corresponding node in the outer segment tree (l-1 corresponding to the nodes in the inner segment tree) is found. Because the tags are permanently marked, all the nodes that pass through will need to be recorded in the answer (especially the outer segment tree).

The above is the situation of $l\neq 1$, as to the situation of l=1, in the same way, to ensure that the R prefix and equals suffix and, using the same idea to maintain a bit better, see the code in the external layer line tree 0 node operation.

The code is really not long ~

#include <cstdio> #include <cstring> #include <algorithm> #define N 100010using namespace Std;typedef Long Long ll;const ll mod = 998244353;int root[n << 2], Ls[n << 8], Rs[n << 8], tot, N;ll sum[n < < 8];ll Cal (ll X, ll y) {return (x * y + (1-x + MoD) * (1-y + mod))% MoD;} ll Pow (ll X, ll y) {ll ans = 1;while (y) {if (Y & 1) ans = ans * x% mod;x = x * x% mod, y >>= 1;} return ans;} void update (int b, int e, ll v, int l, int r, int &x) {if (!x) x = ++tot, sum[x] = 1;if (b <= l && r &lt ; = e) {sum[x] = cal (Sum[x], v); return;}  int mid = (L + R) >> 1;if (b <= mid) update (b, E, V, L, Mid, ls[x]); if (E > Mid) update (b, E, V, mid + 1 , R, Rs[x]);} ll query (int p, int l, int r, int x) {if (!x) return 1;if (L = = r) Return sum[x];int mid = (L + R) >> 1;if (P <= m ID) return cal (Sum[x], query (P, L, Mid, ls[x])), Else return Cal (Sum[x], query (p, mid + 1, R, Rs[x]);} void Modify (int p, int q, LL V, int b, int e, int l, int r, int x) {if (P <= l && r <= q) {update (b, E, V, 1, N, Root[x]); return;  }int mid = (L + R) >> 1;if (P <= mid) modify (p, q, V, B, E, L, Mid, X << 1), if (Q > Mid) modify (p, Q, V, B, E, Mid + 1, r, x << 1 | 1);} ll solve (int p, int q, int l, int r, int x) {if (L = = r) return query (q, 1, N, root[x]); int mid = (L + r) >> 1;  if (P <= mid) return cal (Query (q, 1, N, Root[x]), solve (p, Q, L, Mid, X << 1)), Else return cal (query (q, 1 , N, Root[x]), solve (p, Q, mid + 1, r, x << 1 | 1));} int main () {int m, opt, L, r;ll p;scanf ("%d%d", &n, &m), while (M--) {scanf ("%d%d%d", &opt, &l, &amp ; r); if (opt = = 1) {p = Pow (r-l + 1, mod-2), if (L > 1) Modify (1, l-1, (1-p + MoD)% mod, L, r, 0, N, 1), M Odify (0, 0, 0, 1, l-1, 0, N, 1); if (R < N) Modify (L, R, (1-p + MoD)% mod, r + 1, N, 0, N, 1), modify (0, 0, 0, R + 1, N, 0, n , 1); modify (L, R, (1-(P << 1)% mod + MoD)% mod, L, r, 0, N, 1), modify (0, 0, p, l, r, 0, N, 1);} else printf ("%lld\n", Solve (L-1, R, 0, N, 1));} return 0;}

"bzoj4785" [Zjoi2017] Tree Array segment tree Set segment tree

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.