G. Magic multisets
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons.
Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer 22 to the multiset {1,2,3,3}{1,2,3,3}, you will get {1,1,2,2,3,3,3,3}{1,1,2,2,3,3,3,3}.
If you try to add an integer that is not presented in the multiset, it is simply added to it. For example, if you try to add the integer 44 to the multiset {1,2,3,3}{1,2,3,3}, you will get {1,2,3,3,4}{1,2,3,3,4}.
Also consider an array of nn initially empty magic multisets, enumerated from 11 to nn.
You are to answer qq queries of the form "add an integer xx to all multisets with indices l,l+1,…,rl,l+1,…,r" and "compute the sum of sizes of multisets with indices l,l+1,…,rl,l+1,…,r". The answers for the second type queries can be large, so print the answers modulo 998244353998244353.
Input
The first line contains two integers nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of magic multisets in the array and the number of queries, respectively.
The next qq lines describe queries, one per line. Each line starts with an integer tt (1≤t≤21≤t≤2) — the type of the query. If tt equals 11, it is followed by three integers ll, rr, xx (1≤l≤r≤n1≤l≤r≤n, 1≤x≤n1≤x≤n) meaning that you should add xx to all multisets with indices from ll to rrinclusive. If tt equals 22, it is followed by two integers ll, rr (1≤l≤r≤n1≤l≤r≤n) meaning that you should compute the sum of sizes of all multisets with indices from ll to rr inclusive.
Output
For each query of the second type print the sum of sizes of multisets on the given segment.
The answers can be large, so print them modulo 998244353998244353.
Examples
input
Copy
4 4 1 1 2 1 1 1 2 2 1 1 4 1 2 1 4
output
Copy
10
input
Copy
3 7 1 1 1 3 1 1 1 3 1 1 1 2 1 1 1 1 2 1 1 1 1 1 2 2 1 1
output
Copy
4 8
Note
In the first example after the first two queries the multisets are equal to [{1,2},{1,2},{},{}][{1,2},{1,2},{},{}], after the third query they are equal to [{1,1,2,2},{1,1,2,2},{1},{1}][{1,1,2,2},{1,1,2,2},{1},{1}].
In the second example the first multiset evolves as follows:
{}→{3}→{3,3}→{2,3,3}→{1,2,3,3}→{1,1,2,2,3,3,3,3}{}→{3}→{3,3}→{2,3,3}→{1,2,3,3}→{1,1,2,2,3,3,3,3}.
解题思路:先把N个集合的大小用一个线段树维护,这个线段树支持区间乘法,区间加法,区间求和。关键在于更新操作。什么时候区间要*2或+1,而且是哪些区间要*2或+1.
那么我们就要维护每一个集合内有哪些数,如果我们用主席树来做发现有点难度。那么就考虑暴力。
每次更新区间,我们都暴力查询每一个集合,看看是否存在那个数,这样子做明显不可取。
那么我们不妨用另外一种方式思考,我们用一个集合数组S[i],维护第i个数字在哪些区间出现过。
当我们有更新操作时,我们直接查询S[w]集合,然后判断这个集合里面的所有区间有哪些是跟查询的【L,R】相交的,相交的部分就用*2,不相交的部分就+1,这种做法看似优了很多,但是最坏复杂的还是很大。所以我们要高效的查询有哪些区间是相交的。所以S[w]集合不能保存区间,应该保存区间的端点,这样我们就可以用set自带的lower_bound操作,高效的更新区间和集合了。具体看代码的操作。
学习一下set维护区间。
#include
#include
#include
#include
#include
#include
#include