Lost in desert


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Two jinns were playing in the sands of Rig-e Jenn. While they laughed and chased after each other, one of them lost its way and may have gotten lost in the salt marshes. The second jinn, worried, goes to find its friend but soon got lost as well. Jeff has divine vision so he will tell you where the two jinns are. Can you guess if they are still together in the same place or they have lost each other in the desert?

input🔗

input consists of one line that contains two names separated by one space and the names of the places of the jinns. each consists of two parts. names can be "Rig-e Jenn" or "salt marshes"

output🔗

your output should consist of one string. "together" if they are in the same place and "lost" if they have lost each other.

example🔗

sample input 1🔗

salt marshes salt marshes
Plain text

sample output 1🔗

together
Plain text

sample input 2🔗

salt marshes Rig-e Jenn
Plain text

sample output 2🔗

lost
Plain text

Queue for restroom


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

A bunch of students had booked a train for the SEMANTIC competition. The train consists of several carriages and each carriage has some passengers in it, with a restroom located between each two carriages. There are also restrooms before the first carriage and after the last carriage. After the train stops, suddenly everyone decides to go to the restroom before getting off the train. and long queues have been formed. We know that each person either chooses the restroom on the right or on the left side of their carriage and waits until he/she can go in the restroom. we know each person spends exactly one minute in the restroom. Among all the possible ways that each passenger can choose to stand in one of two adjacent restroom queues, determine the minimum waiting time t for the train that everyone can get off the train after t minutes.

input🔗

the first line consist nn the number of train carriages. the next line consist of n numbers. aia_i shows the number of passengers in carriage number i.

1n1000,0001 \le n \le 1000,000

1ai1000,000,0001 \le a_i \le 1000,000,000

output🔗

your output should consist a single number t that shows the minimum waiting time for the train in best case.

example🔗

sample input 1🔗

4
1 2 1 1
Plain text

sample output 1🔗

1
Plain text

sample input 2🔗

4
1 2 2 1
Plain text

sample output 2🔗

2
Plain text

Regrets


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Jeff is very indecisive. Whenever an opportunity arises, he rarely decides to pursue it. Each opportunity in Jeff’s life has two characteristics:

1.1. VV: the amount of happiness Jeff would gain if he chooses to take this opportunity.

2.2. WW: the amount of energy it would cost Jeff to take this opportunity.

Jeff has a limited amount of energy, KK.

Over QQ moments in Jeff's life, opportunities either appear or disappear:

  • In the i-th moment, an opportunity with values ViV_i​ (happiness) and WiW_i​ (energy cost) either becomes available or, if it was already available, disappears.

Now, at the end of his life, Jeff is filled with regrets. For each moment 1iQ1 \le i \le Q, he wonders what the maximum happiness he could have achieved would have been if he had chosen to take some of the available opportunities, using no more than his available energy KK.

Since Jeff doesn’t have the time to solve this problem himself, he’s asking for your help.

input🔗

the first line contains two integers QQ and KK, the number of moments in Jeff's life and the amount of energy Jeff has.

for the next QQ lines, the i-th line contains two integers each ViV_i and WiW_i the amount of happiness and the energy cost of the opportunity.

if Jeff previously had the opportunity with the same values the opportunity is no longer available otherwise the opportunity becomes available.

1Q2500 1 \le Q \le 2500 0K25000 \le K \le 2500 0Vi2500 0 \le V_i \le 25001Wi2500 1 \le W_i \le 2500

output🔗

output should contain QQ numbers the i-th number should be the maximum amount of happiness Jeff could've achieved if he had chosen to take some of the available opportunities at the i-th moment such that the amount of energy he loses is at most KK.

example🔗

sample input 1🔗

8 7
4 4
9 2
9 2
7 6
7 6
2 7
4 4
7 4
Plain text

sample output 1🔗

4
13
4
7
4
4
2
7
Plain text
  • in moment 1 available opportunities are [(4,4)][(4, 4)] Jeff can take opportunity 11 and gain 44 happiness
  • in moment 2 available opportunities are [(4,4),(9,2)][(4, 4), (9, 2)] Jeff can take opportunities 1,21, 2 and gain 9+4=139+4=13 happiness
  • in moment 3 opportunity (9,2)(9, 2) disappears so opportunities are [(4,4)][(4, 4)] Jeff can take opportunity 11 and gain 44 happiness
  • in moment 4 available opportunities are [(4,4),(7,6)][(4, 4), (7, 6)] Jeff can take opportunity 22 and gain 77 happiness, he can't take both opportunities since his available energy KK is equal to 77 but taking both opportunities would require energy of at least 7+4=137+4=13

Bored


  • محدودیت زمان: ۳ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Jeff got bored in Semnan and decided to pass his time thinking about gcd. We define gcd(a,b)gcd(a, b) as the greatest common divisor of two natural numbers aa and bb. Jeff defines F(X,Y)F(X, Y) between two non empty lists X1X_1, X2X_2, ..., XnX_n and Y1Y_1, Y2Y_2, ..., YmY_m like this:

F(X,Y)=max(gcd(Xi,Yj))1in,1jm F(X, Y) = max(gcd(X_i, Y_j)) \quad 1 \leq i \leq n, 1 \leq j \leq m

In other words, F(X,Y)F(X, Y) is the greatest gcd between one member of the XX-list and another member from YY-list. if one of the XX or YY is empty consider F(X,Y)=1F(X, Y) = 1.

Sehri decides to make Jeff's life harder by inserting and deleting numbers in Jeff's lists QQ times. more formally for each 1iQ1 \le i \le QSehri chooses one of lists XX or YY and a number nin_i, then decides to add nin_i to the chosen list or remove one of the occurrences of nin_i in the chosen list. After each of Sehri's attempts to make Jeff's life harder, Jeff wonders what is F(X,Y)F(X, Y) and since his life is hard enough already, he asks you to calculate F(X,Y)F(X, Y) for him. the lists XX, YY are empty at first. note that it is garanteed that Sehri will always remove an existing nn.

input🔗

the first line consists QQ number of Sehri's attempts. for the next Q lines, i-th line consists of a character '+' or '-' to denote Sehri decided to insert or delete a number. then another character 'X' or 'Y' shows which list Sehri has chosen and then the number nin_i.

1Q100,0001 \le Q \le 100,000

1ni1000,0001 \le n_i \le 1000,000

output🔗

output should consist of Q number F(X, Y) after each of Sehri's attempts.

example🔗

sample input 1🔗

6
+ X 3
+ Y 1
+ Y 6
+ X 12
- Y 1
+ Y 12
Plain text

sample output 1🔗

1
1
3
6
6
12
Plain text

Semnan's Divison


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Jeff and Sehri decided to divide Semnan among themselves. for simplicity let's assume Semnan is a tree consisting of nn nodes. A tree with nn nodes is a undirected connected graph with with n1n - 1 edges. We also define an array FF of length NN of integers between 1 to 100,000. Amin gives each of Semnan's tree nodes to either Jeff and Sehri, after Amin divides Semnan each of Jeff or Sehri will have a forest, where forest is a disjoint union of trees. if Jeff's forests consists of kk connected components and each of them have c1c_1, c2c_2, ..., ckc_k number of nodes, his happiness is i=1kFCi\sum_{i=1}^{k} F_{C_i}

Sehri's happiness is calculated in the same way.

and after that, the amount of happiness of Amin will be the XOR of happiness of Jeff and Sehri. Amin wonders what is the sum of his happiness over all 2n2^n possible divisons of Semnan? Help Amin with this problem.

input🔗

the first line of input consists of the number nn the number of nodes in Semnan. the next line will be n1n - 1 numbers p2p_2, p3p_3, ..., pnp_n where pip_i is an edge between node number i and pip_i. then in the next line there are N numbers denoting array F where the i-th number is FiF_i.

1n261 \le n \le 26 1pii+11 \le p_i \le i + 1 1Fi100,0001 \le F_i \le 100,000

output🔗

output should consist of a single number the sum of Amin's happiness over all possible 2n2^n divisions of Semnan.

example🔗

sample input 1🔗

3
1 1
10 5 15
Plain text

sample output 1🔗

150
Plain text
  • If Jeff gets no nodes Jeff's happiness would be zero but Sehri would get all 3 nodes then Sehri's forest would consist of one component of size 3 then Sehri's happiness would be equal to F3=15F_3=15 since 15xor0=1515 xor 0 = 15 Amin's happiness would be equal to 15.
  • If Jeff gets one node, Jeff's forest would consist of one connected component of size 1 so Jeff's happiness would be equal to F1=10F_1=10 but if the node Jeff gets is node number 1 Sehri's forest would consist of 2 connected components of size one so Sehri's happiness would be F1+F1=20F_1+F_1=20 and Amin's happiness would be 10xor20=3010 xor 20 = 30 but if Jeff gets is the node 2 or 3 Sehri's forest would consist of one connected component of size 2 so Sehri's happiness would be F2=5F_2=5 and Amin's happiness would be equal to 10xor5=1510 xor 5 = 15.
  • If Jeff gets two or three nodes it is similar to the previous cases so we have 15+30+15+15+30+15+15+15=15015+30+15+15+30+15+15+15=150

sample input 2🔗

5
1 1 2 2
10 2 15 3 1
Plain text

sample output 2🔗

566
Plain text

In or out


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

The contest area for the programming competition at Semnan University is a convex polygon with nn sides. You will be asked qq questions. In each question, the coordinates of a student are given, and you need to determine whether the student is inside the contest area or not.

input🔗

The first line of input contains a positive integer nn, representing the number of vertices of the polygon that defines the contest area in counter clockwise order.

The i-th line of the next nn lines contain two integers, xix_i and yiy_i, representing the coordinates of i-th vertex of the polygon in order.

The following line contains the integer qq, indicating the number of students.

again In next qq lines, the i-th line contains two integers, xix_i and yiy_i, representing the coordinates of a student.

1n1051 \leq n \leq 10^5 1q1061 \leq q \leq 10^6 xi,yi109|x_i, y_i| \leq 10^9

output🔗

For each student, print "IN" if they are within the contest area, or "OUT" if they are not.

example🔗

sample input 1🔗

4
0 0
10 0
10 10
0 10
2
5 5
15 15
Plain text

sample output 1🔗

IN
OUT
Plain text

Farmers of the god


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Two farmers, Ahura and Mazda, want to plant vegetable crops on a shared land. Ahura always plants cucumbers, while Mazda always plants tomatoes.

They divide the land into n separate rectangles, with each rectangle aligned parallel to the xx and yy coordinate axes. Each of them then marks specific rectangles where they want plant their crops.

Ahura has chosen n1n_1​ rectangles for planting cucumbers, and Mazda has chosen n2n_2​ rectangles for planting tomatoes. we know we cant plant both cucumber and tomato in exactly one place. because of that, After marking their rectangles, they check for any overlapping areas on the land. To avoid conflicts, they agree not to plant anything in these overlapping areas. They will only plant cucumbers and tomatoes in the non-overlapping parts of their designated rectangles.

The goal is to calculate the difference in the area planted by each farmer. Let:

  • S1S_1​ represent the total area where Ahura will plants cucumbers,
  • S2S_2​ represent the total area where Mazda will plants tomatoes.

Your task is to compute S1S2S_1-S_2​ and output this value

input🔗

The first line contains a positive integer n1n_1​, the number of rectangles Ahura has chosen for planting cucumbers at first. The next n1n_1​ lines each contains the two opposite corners of each rectangles, x1x_1 and y1y_1, the bottom left corner and then x2x_2 ​, y2y_2​ , representing the upper right corner coordinate of the rectangle designated by Ahura.

The following line contains a positive integer n2n_2​, the number of rectangles Mazda has chosen for planting tomatoes at first. The next n2n_2​ lines each contains the two opposite corners of each rectangles, x1x_1 and y1y_1, the bottom left corner and then x2x_2 ​, y2y_2​ , representing the upper right corner coordinate of the rectangle designated by Mazda.

  • It is guaranteed that Ahura's rectangles do not overlap with each other.
  • It is guaranteed that Mazda's rectangles do not overlap with each other.

1n1,n21001 \leq n_1, n_2 \leq 100 1000x1x21000-1000 \leq x_1 \leq x_2 \leq 1000 1000y1y21000-1000 \leq y_1 \leq y_2 \leq 1000

output🔗

Output a single integer, the value of S1S2S_1 - S_2, which is the difference in the areas of land planted by Ahura and Mazda.

example🔗

sample input 1🔗

1
0 0 10 10
1
5 5 15 15
Plain text

Ahura and Mazda have overlapping areas in their marked rectangles. Since they avoid planting in these overlapping areas, the non-overlapping areas for both farmers result in an equal area, making S1​−S2​=0.

sample output 1🔗

0
Plain text

sample input 2🔗

2
0 0 2 4
3 5 4 7
1
1 3 2 9
Plain text

sample output 2🔗

4
Plain text

Score


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

A kk-partition of an array means dividing the array into groups of kk elements. The first kk elements go in the first group, the next kk elements in the second group, and so on... If there are fewer than kk elements left at the end, place all remaining elements in the final group.

For example, if the initial array is [3,0,2,1,2][3, 0, 2, 1, 2] and k=2k=2, the grouping would be [[3,0],[2,1],[2]][[3, 0], [2, 1], [2]]

The score of a group is defined as the bitwise XOR sum of its elements. For example, the score of the group [3,0] is 33, the score of the group [2,1] is 33, and the score of the group [2] is 22. The score of the entire array is the bitwise AND of the scores of all its groups. For the grouping above, the total score of the array is: 3 AND 3 AND 2 = 2.

for each kk as 1,2,3,...,n1, 2, 3, ..., n, calculate the score for a fixed array.

input🔗

The first line of input contains a positive integer n, the number of elements in the array. 1n1061 \leq n \leq 10^6 The second line contains n integers a1,a2,,ana_1, a_2, \dots, a_n\, separated by spaces.

0ai1090 \leq a_i \leq 10^9

output🔗

Print a single line containing the total scores of the array for k=1,2,,nk = 1, 2, \dots, n separated by spaces.

example🔗

sample input 1🔗

4
1 2 3 4
Plain text

sample output 1🔗

0 3 0 4
Plain text

sample input 2🔗

5
3 0 2 1 2
Plain text

sample output 2🔗

0 2 1 0 2
Plain text

Best gift


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

When the server was in serious trouble, Erfan was a great help. Since Erfan is a fan of palindromes, Amin wanted to gift him a palindrome array. However, Erfan felt discouraged by the complexity of that idea and instead requested just a sorted array as a gift.

Initially, you have an array of length 2n2n that consists of 2n2n integers. In each step, you pick the first n+1n+1 elements of the array and move them to the end of the array in the same order.

For example, if the initial array is [3, 2, 5, 4, 1, 1], after one step,

it transforms into [1, 1, 3, 2, 5, 4].

Now, Amin wants to know if, after some number of steps, he can sort the array and make Erfan happy, or if Erfan will remain frustrated and never help with another server trouble again.

input🔗

the first line consist nn that 2n is the length of the array. the next line consist of 2n2n integers. aia_i the array elements.

1n100,0001 \le n \le 100,000

1ai1000,000,0001 \le a_i \le 1000,000,000

output🔗

your output should consist one string. "happy erfan" if Amin can sort the array after finite number of steps. "sad erfan" if Amin can't sort the array with any number of steps.

example🔗

sample input 1🔗

2
1 2 3 4
Plain text

sample output 1🔗

happy erfan
Plain text

sample input 2🔗

2 
4 3 2 1
Plain text

sample output 2🔗

sad erfan
Plain text

Reality of Semnan


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

In Semnan, n houses are arranged in a row. Since everyone doubts that Semnan might be imaginary and doesn’t really exist, Sehri, a famous witch, has hired k experts to verify the existence of the houses in Semnan. The i-th expert has a starting position indiind_i. where Sehri appears the i-th expert exactly on house indiind_i. After the starting house is verified by the i-th expert, now they can either see and verify all the houses to the right of them, or all the houses to the left of them. in other words, the i-th expert can verify houses indi,indi+1,...,nind_i, ind_i+1, ..., n or verify the the houses 1,2,...,indi1, 2, ..., ind_i note that Sehri will appear experts in different starting houses.

to verify the existence of the i-th house from the right, at least aia_i experts must have verify it.

Sehri wants to know if, among all the possible cases where each expert chooses either the left or the right side, there is a situation where we can be sure that all the houses in Semnan exist?

input🔗

the first line consist nn and kk the number of houses in Semnan that arranged in a row and the number of experts. the next line consist of n numbers. aia_i shows the number of experts should verify house number i. then in the next line of the input there is k different numbers ind_i that shows the starting houses.

1kn200,0001 \le k \le n \le 200,000 1aik1 \le a_i \le k 1indin1 \le ind_i \le n

output🔗

your output should consist YES if experts can verify Semnan houses or NO if they cant.

example🔗

sample input 1🔗

7 3
0 0 3 0 3 0 1
3 4 7
Plain text

sample output 1🔗

NO
Plain text

sample input 2🔗

7 3
0 0 2 0 3 0 1
3 4 7
Plain text

sample output 2🔗

YES
Plain text