##### 1262A - Math Problem

Writer: RDDCCD

**Tutorial**

##### 1262B - Box

Writer: RDDCCD

**Tutorial**

##### 1261A - Messy

Writer: RDDCCD

**Tutorial**

##### 1261B1 - Optimal Subsequences (Easy Version)

Writer: MikeMirzayanov

**Tutorial**

##### 1261B2 - Optimal Subsequences (Hard Version)

Writer: MikeMirzayanov

**Tutorial**

##### 1261C - Arson In Berland Forest

**Tutorial**

##### 1261D1 - Wrong Answer on test 233 (Easy Version)

Writer: RDDCCD

**Tutorial**

##### 1261D2 - Wrong Answer on test 233 (Hard Version)

Writer: RDDCCD

**Tutorial**

##### 1261E - Not Same

**Tutorial**

##### 1261F - Xor-Set

Writer: nocriz

**Tutorial**

Auto comment: topic has been updated by nocriz (previous revision, new revision, compare).The tutorial for 1261B — Optimal Subsequences is not ready, but I feel that it is better I post the tutorial soon! That problem is not by me and I hope the tutorial will be available soon.

I can just post the brief solution:

Qualifying round for Technocup (competitions for CIS schoolchildren), but tutorial in English only. hmm

Editorial will be available in Russian soon.

obviously, its' easy and similar words are not appropriate for editorials. I understand that once you know the solution it obvious. There are thousands of people who were didn't found it obvious.

Which part exactly do you need us to explain? I'll be glad to answer your question.

A-Messy

`It's easy to construct a valid bracket sequence,`

I am puzzled if we can construct any valid sequence? If so probably we put all left brackets to left and right to right side, but I think we need to construct K outer brackets. maybe code solution example would help65626236 I would recommend you to see tourist's solution.

I think "valid" is supposed to mean "with exactly K outer brackets". The example given in the tutorial is

`()()()()((((()))))`

for say K=5; so you just put K-1 empty brackets first, then all the left brackets, and finally all the right brackets. This will always give you a valid bracket sequence with K outer brackets.My $$$(n + q) \ log \ n$$$ solution for 1261B2 - Optimal Subsequences (Hard Version)

GreedyThe optimal subsequence of length k

Must have maximum sum -> it must use k largest value of the array

Lexicographically minimal -> we only care about smallest value elements in k choosen value

-> Sort given array by minimum value first, if value is the same, element with larger index first, last k element is the optimal subsequence of length k

Binary Indexed Tree(BIT)The problem ask you to print the index pos in the optimal subsequence of length k, we know the index of all k element in the original array -> $$$answer = a[x]$$$ where x is the $$$pos-th$$$ smallest out of all index

Maintain a set, iterate through all k from 1 to n, keep insert element, for all query with k, to find $$$pos-th$$$ smallest index in $$$log(n)$$$ we can :

Use a BIT such that index in array = value in set and let the value in array be count[value] so that we can get number of element smaller than x

Binary lifting : iterate from highest bit possible, we can check if we can make current bit = 1, thus we can get the maximum value such that number of smaller element is < x (using the fact that $$$sum(x, x - (x & -x) + 1)$$$ can get in $$$O(1)$$$ in BIT), then the answer we need is ans + 1

Code : 65736937

I didn't get the sol for 1261B, can anyone explain plz?

Why the following statement in Problem F is true? Shouldn't it be $$$4 \times n^2$$$?

`We can prove that the number of both "real" and "auxiliary" segments of any size is not greater than 4 x n.`

Consider a access to [l,r] on the segment tree, we would call the segments we accessed and found that the segment is completely in [l,r] real segments, and all segments we visited are auxiliary segments. Since we tried visiting n intervals (All intervals in A or B), and for each visit the segments (nodes) we touched of each depth is not greater than 4,

`the number of both "real" and "auxiliary" segments of any size is not greater than 4 x n`

is proved.This is the same with the proof of the complexity of the segment tree, I found this on internet only 4 nodes are processed in a level.

Sorry, but I have no idea why it could possibly be $$$4n^2$$$.

Check out my solution for more information:65749995

I can’t see your solution. QwQ

Pasted on pastern.ubuntu

Oh... finally I understand what the sentences. The reason I didn't understand before is just my English is bad... I mean the size of vector fin in your code should be about $$$60 \times 4 \times n^2$$$.

For anyone looking for the solution of 1261B2 - Optimal Subsequences (Hard Version) in

Java, you can refer to my solution. I used the same approach as mentioned in the tutorial and usedSegment Treeto find the kth smallest element in the set. 65750497Btw no need to binary search on seg tree queries, you can use this trick, (ctrl-f for find_kth)

Thanks a lot, exactly what I was looking for. It decreased the time complexity from log(n)^2 to log(n). Thanks a lot.

Can Anyone Explain Full Logic Of Box Problem??

Thank you so much for the contest and editorials!

I'm just wondering if anyone has a solution which runs under $$$O(n^2)$$$ for Div1A/Div2C (Messy)? Or alternatively, is there a way to show that it is not possible to do this with better time complexity?

Thanks!

I was wondering the same.

After spending 5 mins thinking about a solution, I noticed n^2 can also pass but it looked like it can be solved in better complexity too.

There are several ways to implement a solution that runs under $$$O(n^2)$$$.

Both operations explained in the editorial can be implemented in $$$O(log(n))$$$ using treap: code

Also, you can notice that in this problem, reversing a range is only going to change the value of two positions (start and end of the range). So you can implement the reverse operation with a simple segment tree (by updating both ends of the range only). Finding the first position to the right equal to a value can be done in more than one way using the same segment tree. For example:

I didn't explain every detail in case someone wants to think about them. I'm not sure whether a $$$O(n)$$$ solution exists. Feel free to ask anything :)

Actually it can be done in $$$O(n)$$$. Here is my code: 65767515

The main idea is to create pointers

`first_opened`

and`first_closed`

for first`(`

and`)`

in`s`

and just update them. The solution is similar to the solution in the editorial. But every time when $$$s[i] \neq t[i]$$$, look at the substring`s[i..max(first_opened, first_closed)]`

. It is either`(..()`

or`)..)(`

. Reverse can be done in $$$O(1)$$$ and after that you have to fix pointers, it is $$$O(n)$$$ total (details in the code).Also, I'm pretty sure that there is no $$$o(n)$$$ solution :)

My solution works in O(n) time which I think is a bit different than yours. 67442807

I saw solutions for B1, B2 using Segment Tree and BIT. How can we find kth smallest element in set using those 2 data structures? I solved it using ordered_set (PBDS in GNU).

Also, is insertion operation also supported with this trick (with the use of BIT/SegTree)?

find_kth

Thank you :)

You can do the hard version of optimal subsequences with online queries using sortings and a mergesort tree. Here is my solution: https://codeforces.com/contest/1262/submission/65675557

Also you can compute persistent segment tree for each prefix that will store for each value count of it. And you will just go down in this tree in O(log n) to find min prefix with sum >= k.

The issue is solved.

Can somebody please explain why we should multiply the answer by

k^(n-t)indiv1D/div2Fi think the n-t other answers have fewer possibilitiesFor Div 2 problem A (Math problem), how can we say that

(rmin — lmax)will give us the segment that will connect each and every segment (ie. common point for all the segment)?Could anyone suggest why this solution for Div2 C is wrong ? It makes string of pattern "()()(()())" of this sort. It is different from tutorial (and quite complicated) but since m can be upto n, I don't get what is wrong ?

Sorry, but I have tried hard but still can't understand the given solution of

1261E,I wonder what's the meaning ofoperations for {4,1}and the later stuff? Looking for your reply (:I'm sorry if the editorial is hard to understand. {4,1} means that we currently iterated through two elements in A, namely 4 and 1. If the next element in A is 1, then there are three numbers we handled, {4,1,1}. Be careful not to mix it up with the "compressed set".

У меня есть некое подозрение , того , что задача С которую я написал за N^3 работала , а именно она работает за N^3 ! Но только абстрактно понял почему ! Был бы очень благодарен тому ктом сможет объяснить почему задача С которая описано в разборе работает за N^2 А не за N^3 !

Вот мое решение , кому интересно !

## include<bits/stdc++.h>

## define ll long long

## define sz size

## define pb push_back

## define pf push_front

## define mod 1000000007

using namespace std;

ll T = 1, i, n, m, k, p, cnt, j; string s, t; deque<pair<ll, ll> >v; dequenm; int main() { cin >> T; while(T--) { cin >> n >> k ; cin >> s ; p = 1 — k; for(auto to : s) { if(to == ')')p ++;

}

опечатка второй раз я хотел написать , что она работала за N^2

I solved problem E using the concept entropy, you can see it here.

Can you give a brief explanation of your solution? How have you used entropy here?

Problem : Arson in Berland Forest

How to do "add value on a rectangle" (using prefix sums)" ?

.

What if we want to minimize number of operations in 1261A?

Someone please explain how to use segment tree in B2... I read submitted codes but was unable to understand.. Thanks:)

How to solve 1261 C if we should also minimize the initial number of burnt trees for a particular maximum value?

1261C (Arson in Berland Forest) is doable in $$$O(nm)$$$ time by finding the largest square with lower-right corner at each cell, and then applying the skyline algorithm twice (once vertically, once horizontally on the result of the vertical). Normally the skyline problem is solvable only in $$$O(n\log n)$$$ time, however in this case we don't have any "nested" segments (i.e. a pair of segments s.t. one is higher the other and also contained within it), so I was able to solve it using a deque rather than a priority queue (as would normally be used to solve skyline). This allows us to generate a table

`M`

s.t.`M[i][j]`

is the size of the largest square which contains the cell`(i, j)`

. Now just find the smallest positive element of`M`

and that's the answer. (From here, finding a valid set of initial points is trivial)My code here: 74775877

emorgan5289 Can you explain how have you used skyline to create M[i,j]? Hard to understand from your code.

First, I build a table

`sz`

where`sz[i][j]`

is the size of the largest square with lower-right corner at cell`(i, j)`

. This is a classical problem solved by dynamic programming.Then, I consider each row one at a time. For each index

`j`

, I would like to update the values of`sz`

to the left of`j`

, in a way that makes it such that the value in each cell corresponds to the size of the largest square containing that point, with lower-right corner somewhere in that row. To do this, I simply consider all such squares (i.e. all values in that row), and perform range max updates on that specific row on the interval covered by each square. The skyline algorithm actually allows us to do this in $$$O(n)$$$ time. The way it works is quite complicated, and I don't think I know how to explain it at a basic level.Then, we just do the same thing on columns instead of rows, and

`sz`

now holds the values of the desired array`M`

which i mentioned in my previous comment.Thanks emorgan5289! Your this line makes sense to me —

"update the values of sz to the left of j, in a way that makes it such that the value in each cell corresponds to the size of the largest square containing that point, with lower-right corner somewhere in that row".Some print statements in your code helped me to understand the while loops in skyline algo. As per my understanding, use of the first, second while loop is as per image1, image2 respectively.image 1 — https://www.dropbox.com/s/ixjh6pbje3tbv0j/pic1.jpg?dl=0

image2 — https://www.dropbox.com/s/571zalnyq5mzs8m/pic2.jpg?dl=0

But why are we doing skyline columnwise as well? After rowwise operation we already have max square which contains (i,j).

After the row-wise operation,

`sz[i][j]`

stores the size of the largest square which contains`(i, j)`

, and also has lower-right corner in that same row. However, we need to consider squares from lower rows as well. So we perform a second column-wise pass to propagate the values from lower rows upwards. The end result is that each cell storing value $$$s$$$ updates an $$$s$$$ by $$$s$$$ square up and to the left, rather than just to the left along the row.Can you explain about dynamic programming method in 1261D1 - Wrong Answer on test 233 (Easy Version) ?

D2 Video tutorial:https://youtu.be/f_oRjNtuxnY

D2 can be solved very easily using ordered_set (for c++) https://codeforces.com/contest/1227/submission/88584680

I did Arson in Berland Forest by using (inventing ^_^) 2-dimensional sweepline algorithm. Have a look : 92480981

In DIV2E/DIV1 C, the wording of this statement could have been better

You are sure that all burnt trees are shown on the map. All the trees outside the map are undamaged. "are undamaged" should have been replaced with "should be undamaged"I tried to solve problem D2 of Div-2 using segment tree, where I am just assigning new indexes discovered in queries(sorted) to 1 and finding the pos-th 1. But it is giving WA on test 10. Can anyone please help me in finding what I did wrong. Thanks.

https://codeforces.com/contest/1262/submission/110093210