[Ynoi2014] In the name of justice for all (explanations)

We do not fight code, we just thought porters.

The meaning of problems

[Ynoi2014] In the name of justice for all

背景
略
题目描述
你需要帮珂朵莉维护一个长为n的01序列a,有m个操作:
1 l r : 把区间[l,r]的数变成0
2 l r : 把区间[l,r]的数变成1
3 l r : [l,r-1]内所有数a[i],变为a[i]与a[i+1]按位或的值,这些数同时进行这个操作
4 l r : [l+1,r]内所有数a[i],变为a[i]与a[i-1]按位或的值,这些数同时进行这个操作
5 l r : [l,r-1]内所有数a[i],变为a[i]与a[i+1]按位与的值,这些数同时进行这个操作
6 l r : [l+1,r]内所有数a[i],变为a[i]与a[i-1]按位与的值,这些数同时进行这个操作
7 l r : 查询区间[l,r]的和

输入格式
第一行两个数表示n和m
第二行n个数表示序列a
之后m行,每行三个数opt l r,表示是哪一种操作以及操作对应的区间

输出格式
对于每个查询操作输出一行一个数表示答案

输入输出样例
输入 #1 复制
5 5
0 1 0 0 1
3 2 5
5 2 5
2 2 2
6 1 5
7 1 5
输出 #1 复制
1
说明/提示
Idea:nzhtl1477,Solution:nzhtl1477,Code:nzhtl1477,Data:nzhtl1477

序列每次的样子:
0 1 0 0 1
0 1 0 1 1
0 0 0 1 1
0 1 0 1 1
0 0 0 0 1
0 0 0 0 1

对于30%的数据,n,m<=1000

对于50%的数据,n,m<=100000

对于另外20%的数据,操作和序列均随机生成

对于100%的数据,n,m<=1000000

The meaning of problems

Here the code is not given, is given only thought.

HYY giant thought to think most guys from the same room.I have been caught up in the frenzy differential, HYY giant or giant Yeah.

We follow every point position is inserted into the balance weights inside the tree, but we found one thing, we can put a string of 1 or 0 as the interval node is inserted into it.

Then we continue to push the law:

\(00111000\)

We operate on the entire range of about 4, becomes: \ (00011000 \)

We have found a length of \ (1 \) string \ (L ++ \) , length \ (0 \) string \ (R & lt ++ \) .

That is for the 4 operation, all within the block section \ (. 1 \) \ (L ++ \) , \ (0 \) block \ (R & lt ++ \) , and the boundary, for example if the child knows located in block, it is no impact, which is independent boundary process, and in order to correct answers, we need to change it borders, reduced to a range of nodes, because we can not start from the middle of maintenance.

Here Insert Picture Description

Other operations can also be like this child, of course border judge their own special push it. . .

Among us mark on the line playing, the recording information is 1 or 0, respectively two different labels, \ (L \) plus the number, \ (R & lt \) plus the number, then the sub-tree for each of us, there are three a message that the number of a sub-tree \ (0 \) range, the number \ (1 \) sections and sub-tree and these are balanced tree can be solved.

But the interval bulldozed how to do it?

First, if the boundary is located in the middle of a piece (not the border), such as: 11100 The [2,4] becomes 1, then we look at the same set of blocks is not a color, is it simply contains the whole block, if not, put the point interval is divided into two, and then out of this range of operation, and up to two new points for each operation, so \ (log \) through violence.

Here Insert Picture Description
Of course, there is a special case of the border issue, is situated at the border if the words, and the entire border situated opposite to the modified value, but also to add in that block next door. (Next you'll know why.)

Here Insert Picture Description
Then we traverse the tree, to merge the nodes in the tree into a large range of nodes (which may explain why the border, we want to jump to the next, because we want to ensure that the value of the two adjacent intervals of maintenance must be different.).

But this operation is not \ (O (n) \) do?

You think, ah, each operation add up to two intervals, that is, the number of interval from start to finish plus extra, are \ (O (n) \) level, we have gone every time a merger, then the total operating time is up to \ (O (n) \) , and shared equally \ (O (1) \) , but because there are operating balance of the tree, it is \ (O (logn) \) is.

Two operations are true.

Sahua end.

You want peaches.

I have not noticed the point that when performing \ (3,4,5,6 \) operation, some of the length \ (1 \) interval minus click on each of!

Then there is the interval merger?

So we have to find how these intervals it.

我们首先要知道如何找到一个区间左右区间的所在的位置,那么对于每个区间,我们都可以维护一个\(head,tail\)指针,表示左右区间的下标,然后对于每个合并、添点、分裂、删点,我们都可以极快的维护,对时间复杂度不造成影响。

那么我们对于每个节点,再维护一个子树内区间长度最小值,然后看看最小值是不是为\(0\),为\(0\)就删除,并且把他左右区间合并,并且再继续看看新的最小值是不是\(0\)

而对于左右区间合并,我们要把左右区间节点分离出原来的子树,然后合并,再添回去,而且,删除的区间和这两个区间维护的值相反,所以这两个区间的长度不会减少的,不用担心这两个区间也是被删除的对象,不过需要注意的是这两个区间节点不一定在\([l,r]\)范围内(虽然影响应该不是很大)。。。

但是你这样子我可以卡到\(nlogn\)呀。

我们继续证明均摊。

因为每次合并少\(1\)个区间,而我们单次合并的复杂度是\(O(logn)\),且是\(O(n)\)级别的区间个数,那么总复杂度最多\(O(nlogn)\),均摊\(log\)

那么我们的时间复杂度总的就是\(O(nlogn)\)

注意:此篇题解建议用fhq treap实现,以及这里n,m同阶。

There is no code code This question is too big, wrong in the comments area please noted, thank you O (∩_∩) O.Pianchipianhe cheat Comments

Guess you like

Origin www.cnblogs.com/zhangjianjunab/p/11728549.html