Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def checkKey(dict, key): if key in dict: return True return False # def helper(s): # l=len(s) # if (l==1): # l=[] # l.append(s) # return l # ch=s[0] # recresult=helper(s[1:]) # myresult=[] # myresult.append(ch) # for st in recresult: # myresult.append(st) # ts=ch+st # myresult.append(ts) # return myresult # mod=1000000000+7 # def helper(s,n,open,close,i): # if(i==2*n): # for i in s: # print(i,end='') # print() # return # if(open<n): # s[i]='(' # helper(s,n,open+1,close,i+1) # if(close<open): # s[i]=')' # helper(s,n,open,close+1,i+1) # def helper(arr,i,n): # if(i==n-1): # recresult=[arr[i]] # return recresult # digit=arr[i] # recresult=helper(arr,i+1,n) # myresult=[] # for i in recresult: # myresult.append(i) # myresult.append(i+digit); # myresult.append(digit) # return myresult # import copy # n=int(input()) # arr=list(map(int,input().split())) # ans=[] # def helper(arr,i,n): # if(i==n-1): # # for a in arr: # # print(a,end=" ") # # print() # l=copy.deepcopy(arr) # ans.append(l) # return # for j in range(i,n): # if(i!=j): # if(arr[i]==arr[j]): # continue # else: # arr[i],arr[j]=arr[j],arr[i] # helper(arr,i+1,n) # arr[j],arr[i]=arr[i],arr[j] # else: # arr[i],arr[j]=arr[j],arr[i] # helper(arr,i+1,n) # def helper(sol,n,m): # for i in range(n+1): # for j in range(m+1): # print(sol[i][j],end=" ") # print() # def rat_in_a_maze(maze,sol,i,j,n,m): # if(i==n and j==m): # sol[i][j]=1 # helper(sol,n,m) # exit() # if(i>n or j>m): # return False # if(maze[i][j]=='X'): # return False # maze[i][j]='X' # sol[i][j]=1 # if(rat_in_a_maze(maze,sol,i,j+1,n,m)): # return True # elif(rat_in_a_maze(maze,sol,i+1,j,n,m)): # return True # sol[i][j]=0 # return False n,m,s,f=map(int,input().split()) d={} for i__ in range(m): t,l,r=map(int,input().split()) d[t]=(l,r) if(f>s): ans="" cp=s time=1 while(cp!=f): if time in d: start=d[time][0] end=d[time][1] if(cp>=start and cp<=end or cp+1>=start and cp+1<=end): ans+='X' time+=1 else: ans+='R' time+=1 cp+=1 else: ans+='R' time+=1 cp+=1 print(ans) elif(f<s): ans="" cp=s time=1 while(cp!=f): if time in d: start=d[time][0] end=d[time][1] if(cp>=start and cp<=end or cp-1>=start and cp-1<=end): ans+='X' time+=+1 else: ans+='L' time+=1 cp-=1 else: ans+='L' time+=1 cp-=1 print(ans)
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, second, first; cin >> n >> m >> second >> first; int t[100001][3]; for (int i = 0; i < m; i++) { cin >> t[i][0] >> t[i][1] >> t[i][2]; } int step = 1, cur = second, i = 0; string res; while (cur != first) { if (step == t[i][0]) { if (cur >= t[i][1] && cur <= t[i][2]) { res += 'X'; } else if (cur > first && !(cur - 1 >= t[i][1] && cur - 1 <= t[i][2])) { cur--; res += 'L'; } else if (cur < first && !(cur + 1 >= t[i][1] && cur + 1 <= t[i][2])) { cur++; res += 'R'; } else res += 'X'; i++; } else { if (cur > first) { cur--; res += 'L'; } else { cur++; res += 'R'; } } step++; } cout << res << endl; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int a[100006][3]; int main() { int n, m, s, f, dir, i; scanf("%d%d%d%d", &n, &m, &s, &f); if (s < f) dir = 1; else dir = -1; for (i = 0; i < m; i++) { scanf("%d%d%d", &a[i][0], &a[i][1], &a[i][2]); } i = 0; int t = 1; while (f != s) { if (a[i][0] == t) { if ((a[i][1] <= s and a[i][2] >= s) or (a[i][1] <= s + dir and a[i][2] >= s + dir)) { printf("X"); } else { if (dir > 0) printf("R"); else printf("L"); s += dir; } i++; } else { if (dir > 0) printf("R"); else printf("L"); s += dir; } t++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; map<int, pair<int, int> > ma; int h = -1; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; h = max(h, a); ma[a] = {b, c}; } for (int i = 1; i <= h; i++) { if (s >= ma[i].first && s <= ma[i].second) { cout << "X"; } else { if ((s == ma[i].first - 1 && s < f) || (s == ma[i].second + 1 && s > f)) { cout << "X"; continue; } if (s < f) { cout << "R"; s++; if (s == f) break; } else if (s > f) { cout << "L"; s--; if (s == f) break; } if (s == f) break; } } if (s < f) { while (s < f) { cout << "R"; s++; } } else if (s > f) { while (s > f) { cout << "L"; s--; } } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class P342B { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); boolean swap = false; if (s > f) { swap = true; s = n + 1 - s; f = n + 1 - f; } int[] t = new int[m]; int[] l = new int[m]; int[] r = new int[m]; for (int i = 0; i < m; i++) { t[i] = in.nextInt(); if (!swap) { l[i] = in.nextInt(); r[i] = in.nextInt(); } else { r[i] = n + 1 - in.nextInt(); l[i] = n + 1 - in.nextInt(); } } int ct = 1; int ti = 0; int cp = s; StringBuilder sb = new StringBuilder(); while (cp != f && ti < m) { int tr = t[ti] - ct; if (tr > 0) { while (cp != f && ct < t[ti]) { sb.append(swap ? "L" : "R"); cp++; ct++; } } else {//tr=0 if (!(l[ti] <= cp && cp <= r[ti]) && !(l[ti] <= cp + 1 && cp + 1 <= r[ti])) { cp++; sb.append(swap ? "L" : "R"); } else { sb.append("X"); } ct++; ti++; } } while (cp != f) { sb.append(swap ? "L" : "R"); cp++; } out.println(sb.toString()); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(false); int n, m, s, f, ti, li, ri; cin >> n >> m >> s >> f; vector<pair<int, pair<int, int>>> v; for (int i = 0; i < m; ++i) { cin >> ti >> li >> ri; v.push_back(make_pair(ti, make_pair(li, ri))); } int curt = 1, vptr = 0; string res; while (s != f) { if (vptr == m) { if (s < f) { s++; res += "R"; } else { s--; res += "L"; } curt++; continue; } if (v[vptr].first > curt) { if (s < f) { s++; res += "R"; } else { s--; res += "L"; } } else { int to = s < f ? s + 1 : s - 1; int lbound = v[vptr].second.first; int rbound = v[vptr].second.second; if ((lbound <= s && s <= rbound) || (lbound <= to && to <= rbound)) { res += "X"; } else { if (s < f) { s++; res += "R"; } else { s--; res += "L"; } } vptr++; } curt++; } cout << res; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main{ public static void main(String[]args)throws IOException{ BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); String x=bf.readLine(); String[]xa=x.split(" "); int n=Integer.parseInt(xa[0]); int m=Integer.parseInt(xa[1]); int s=Integer.parseInt(xa[2]); int f=Integer.parseInt(xa[3]); int last=0; int i=0; while(i<m && s!=f){ x=bf.readLine(); xa=x.split(" "); int t=Integer.parseInt(xa[0]); if(f>s){ int st=t; while(t-last>1 && s!=f){ System.out.print("R"); s++; t--; } last=st; if(s==f) break; int l=Integer.parseInt(xa[1]); int r=Integer.parseInt(xa[2]); if((s<l || s>r) &&(s+1<l || s+1>r)){ System.out.print("R"); s++; } else System.out.print("X"); }else{ int st=t; while(t-last>1 && s!=f){ System.out.print("L"); s--; t--; } last=st; if(s==f) break; int l=Integer.parseInt(xa[1]); int r=Integer.parseInt(xa[2]); if((s<l || s>r) &&(s-1<l || s-1>r)){ System.out.print("L"); s--; } else System.out.print("X"); } i++; } for(int j=0;j<Math.abs(f-s);j++){ System.out.print((f>s)? "R" : "L"); } System.out.println(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f; int t, l, r; int tt, now; void deal() { if (f > s) { now++; printf("R"); } else { now--; printf("L"); } } void work() { tt = 0; now = s; for (int i = 0; i < m; i++) { scanf("%d %d %d", &t, &l, &r); if (now == f) continue; for (int i = tt + 1; i < t; i++) { if (now == f) break; deal(); } if (now == f) continue; if (f > s) { if ((now >= l && now <= r) || ((now + 1) >= l && (now + 1) <= r)) { printf("X"); } else { deal(); } } else { if ((now >= l && now <= r) || ((now - 1) >= l && (now - 1) <= r)) { printf("X"); } else { deal(); } } tt = t; } while (now != f) { deal(); } printf("\n"); } int main() { while (scanf("%d %d %d %d", &n, &m, &s, &f) != EOF) { work(); } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int on_bit(int x, int pos) { x |= (1 << pos); return x; } int off_bit(int x, int pos) { x &= ~(1 << pos); return x; } bool is_on_bit(int x, int pos) { return ((x & (1 << pos)) != 0); } int flip_bit(int x, int pos) { x ^= (1 << pos); return x; } int lsb(int x) { return x & (-x); } int on_bit_all(int x, int pos) { x = (1 << pos) - 1; return x; } const double EPS = 1e-9; const double PI = 2 * acos(0.0); const int INF = 0x3f3f3f3f; const long long LLINF = 1e18; const int MOD = 1e9 + 7; int add(long long a, long long b) { return ((a % MOD) + (b % MOD)) % MOD; } int sub(long long a, long long b) { return ((a % MOD) - (b % MOD)) % MOD; } int mult(long long a, long long b) { return ((a % MOD) * (b % MOD)) % MOD; } long long powmod(long long a, long long b) { long long res = 1; assert(b >= 0); a %= MOD; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); vector<pair<int, pair<int, int> > > v; string res; res.reserve((int)1e5 + 10); char c = (s < f ? 'R' : 'L'); map<int, pair<int, int> > ma; for (int i = 0; i < (int)m; i++) { int qwe, asd, zxc; scanf("%d%d%d", &qwe, &asd, &zxc); ma[qwe] = make_pair(asd, zxc); } int cur_time = 1; if (s < f) { while (s != f) { if (((ma).find((cur_time)) != (ma).end())) { int asd = ma[cur_time].first; int zxc = ma[cur_time].second; int nx = (c == 'R' ? s + 1 : s - 1); if ((asd <= nx && nx <= zxc) || (asd <= s && s <= zxc)) { res.push_back('X'); } else { res.push_back(c); s++; } } else { res.push_back(c); s++; } cur_time++; } } else { while (s != f) { if (((ma).find((cur_time)) != (ma).end())) { int asd = ma[cur_time].first; int zxc = ma[cur_time].second; int nx = (c == 'R' ? s + 1 : s - 1); if ((asd <= nx && nx <= zxc) || (asd <= s && s <= zxc)) { res.push_back('X'); } else { res.push_back(c); s--; } } else { res.push_back(c); s--; } cur_time++; } } puts(res.c_str()); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, add = 0, rund = 1; cin >> n >> m >> s >> f; if (s < f) add = 1; else add = -1; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; while (rund < t) { if (s == f) break; if (add == 1) cout << 'R'; else if (add == -1) cout << 'L'; s += add; rund++; } if (s == f) continue; if (t == rund) { rund++; if (s >= l && s <= r || s + add >= l && s + add <= r) cout << 'X'; else if (add == 1) { cout << 'R'; s += add; } else if (add == -1) { cout << 'L'; s += add; } } } while (s != f) { if (add == 1) cout << 'R'; else if (add == -1) cout << 'L'; s += add; } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int spies, moments, from, to; scanf("%d %d", &spies, &moments); scanf("%d %d", &from, &to); int sec = 0, current = from, time, left, right; for (int i = 0; i < (int)(moments); i++) { sec++; scanf("%d %d %d", &time, &left, &right); if (current != to) { while (time > sec) { if (current < to) { printf("R"); current++; } else if (current > to) { printf("L"); current--; } sec++; } if (current != to) { if (current < left && current < right) { if (current < to) { if (current + 1 < left && current + 1 < right) { printf("R"); current++; } else printf("X"); } else if (current > to) { if (current - 1 < left && current - 1 < right) { printf("L"); current--; } else printf("X"); } } else if (left < current && right < current) { if (current < to) { if (current + 1 > left && current + 1 > right) { printf("R"); current++; } else printf("X"); } else { if (current - 1 > left && current - 1 > right) { printf("L"); current--; } else printf("X"); } } else printf("X"); } } } if (current != to) { while (current != to) { if (current < to) { current++; printf("R"); } else if (current > to) { current--; printf("L"); } } } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct look { int t, l, r; }; look a[100000]; int n, m, s, f; int main() { cin >> n >> m >> s >> f; for (int i = 0; i < m; ++i) cin >> a[i].t >> a[i].l >> a[i].r; int curLook = 0; for (int t = 1; s != f; ++t) { if (curLook < m && a[curLook].t == t) { int l = a[curLook].l, r = a[curLook].r; int next = (s < f) ? s + 1 : s - 1; ++curLook; if ((l <= s && s <= r) || (l <= next && next <= r)) { cout << 'X'; continue; } } if (s < f) { cout << 'R'; ++s; } else { cout << 'L'; --s; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; import java.math.*; public class Q1 { static ArrayList<Integer> adj[],adj2[]; static int color[],cc; static long mod=1000000007; static TreeSet<Integer> ts; static boolean b[],visited[],possible,ans1,ans2; static Stack<Integer> s; static int totalnodes,colored,minc; static int c[],a[],n,parent[],ans[],size[]; static long sum[]; static HashMap<Integer,Integer> hm; public static void main(String[] args) throws IOException { //Scanner sc=new Scanner(System.in); in=new InputReader(System.in); out=new PrintWriter(System.out); int n=in.nextInt(); int m=in.nextInt(); int s=in.nextInt(); int f=in.nextInt(); char ans[]=new char[1000000]; int last=0; int pos=0; loop :while(m--!=0) { int t=in.nextInt(); int l=in.nextInt(); int r=in.nextInt(); int temp=last+1; while(temp!=t) { if(s<f) { ans[pos++]='R';s++;temp++;last=temp; } else if(s>f) { ans[pos++]='L';s--;temp++;last=temp; } else { continue loop; } } last=t; if(s==f) continue; if(l<=s && s<=r) ans[pos++]='X'; else { if((s==l-1 && s<f) || (s==r+1 && f<s)) ans[pos++]='X'; else if(s<f) { ans[pos++]='R';s++;} else if(s>f) {ans[pos++]='L';s--;} } } if(s<f) { while(s<f) {ans[pos++]='R'; s++; } } else if(s>f) { while(s>f){ ans[pos++]='L'; s--;} } for(int i=0;i<pos;i++) out.print(ans[i]); out.close(); } static int check(int m) { int c=0; for(int i=0;i<n-1;i++) { int l=i; int r=n-1; while(l<r) { int mid=(l+r)/2; if(a[i]+m+1>a[mid]) l=mid+1; else r=mid; } //debug(" l****** "+l); if(!(a[l]-a[i]<=m)) while(a[l]==a[l-1]) l--; // debug((l-1-(i))); if(l==n-1 && !(a[l]-a[i]>m) ) c=c+l-i; else c=c+(l-1-(i)); } return c; } static InputReader in; static PrintWriter out; static void dfs(int i) { size[i]=1;int badabeta=0; for(int j=0;j<adj[i].size();j++) { int son=adj[i].get(j); dfs(son); size[i]+=size[son]; if(size[son]>size[badabeta]) badabeta=son; } if(size[badabeta]*2>size[i]) { int dada = ans[badabeta]; while((size[i]-size[dada])*2>size[i]) dada=parent[dada]; ans[i]=dada; } } /*public static void seive(long n){ b = new boolean[(int) (n+1)]; Arrays.fill(b, true); for(int i = 2;i*i<=n;i++){ if(b[i]){ sum[i]=count[i]; // System.out.println(sum[i]+" wf"); for(int p = 2*i;p<=n;p+=i){ b[p] = false; sum[i]+=count[p]; //System.out.println(sum[i]); } } } }*/ static class Pair implements Comparable<Pair> { int i; int j; int index; public Pair(){ } public Pair(int u, int v,int index) { this.i = u; this.j= v; this.index=index; } public int compareTo(Pair other) { return this.i-other.i; } /*public String toString() { return "[u=" + u + ", v=" + v + "]"; }*/ } static class Node2{ Node2 left = null; Node2 right = null; Node2 parent = null; int data; } //binaryStree static class BinarySearchTree{ Node2 root = null; int height = 0; int max = 0; int cnt = 1; ArrayList<Integer> parent = new ArrayList<>(); HashMap<Integer, Integer> hm = new HashMap<>(); public void insert(int x){ Node2 n = new Node2(); n.data = x; if(root==null){ root = n; } else{ Node2 temp = root,temp2 = null; while(temp!=null){ temp2 = temp; if(x>temp.data) temp = temp.right; else temp = temp.left; } if(x>temp2.data) temp2.right = n; else temp2.left = n; n.parent = temp2; parent.add(temp2.data); } } public Node2 getSomething(int x, int y, Node2 n){ if(n.data==x || n.data==y) return n; else if(n.data>x && n.data<y) return n; else if(n.data<x && n.data<y) return getSomething(x,y,n.right); else return getSomething(x,y,n.left); } public Node2 search(int x,Node2 n){ if(x==n.data){ max = Math.max(max, n.data); return n; } if(x>n.data){ max = Math.max(max, n.data); return search(x,n.right); } else{ max = Math.max(max, n.data); return search(x,n.left); } } public int getHeight(Node2 n){ if(n==null) return 0; height = 1+ Math.max(getHeight(n.left), getHeight(n.right)); return height; } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static String rev(String s) { StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } static long lcm(long a, long b) { return a * (b / gcd(a, b)); } static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; // % is remainder a = temp; } return a; } public static long max(long x, long y, long z){ if(x>=y && x>=z) return x; if(y>=x && y>=z) return y; return z; } static int[] sieve(int n,int[] arr) { for(int i=2;i*i<=n;i++) { if(arr[i]==0) { for(int j=i*2;j<=n;j+=i) arr[j]=1; } } return arr; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.Arrays; import java.util.ArrayList; import java.lang.Math; import java.util.Arrays; import java.util.Comparator; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int binarySearch(int a[],int k,int l,int h){ while(l<=h){ int mid = (l+h)/2; if(a[mid]==k) return mid; else if(a[mid]>k) h=mid-1; else if(a[mid]<k) l =mid+1; } return -1; } static String reverse(String input) { char[] a = input.toCharArray(); int l, r = 0; r = a.length - 1; for (l = 0; l < r; l++, r--) { // Swap values of l and r char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } static int gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static int lcm(int a, int b) { return (a*b)/gcd(a, b); } static int solve(int A, int B) { int count = 0; for (int i = 0; i < 21; i++) { if (((A >> i) & 1) != ((B >> i) & 1)) { count++; } } return count; } static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(int n) { long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static long count(long k) { return k * (k - 1) / 2; } static boolean isPrime(int n) { // if(n==1) return false; if(n==2) return true; if (n%2==0) return false; for(int i=3;i<=Math.sqrt(n);i+=2) { if(n%i==0) return false; } return true; } static int negMod(int n){ int a = (n % 1000000007 + 1000000007) % 1000000007; return a; } static String value(int val) { String bin = ""; while (val > 0) { if (val % 2 == 1) { bin += '1'; } else bin += '0'; val /= 2; } bin = reverse(bin); return bin; } public static int sum(long x) { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return sum; } static int mod=1000003; public static void main(String[] args) throws Exception { OutputStream outputStream = System.out; PrintWriter w = new PrintWriter(outputStream); FastReader sc = new FastReader(); // Scanner sc = new Scanner(new File("input.txt")); // PrintWriter out = new PrintWriter(new File("output.txt")); int i,j=0; int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); StringBuffer str = new StringBuffer(); int ab[][] = new int[m][3]; for(i=0;i<m;i++){ ab[i][0] = sc.nextInt(); ab[i][1] = sc.nextInt(); ab[i][2] = sc.nextInt(); } int prev = 1; if(s<f){ i=0; while(i<m){ int t = ab[i][0]; int a = ab[i][1]; int b = ab[i][2]; if(s == f) break; if(prev == t){ if((s>=a && s<=b) || (s+1>=a && s+1<=b)) { str.append("X"); } else{ str.append("R"); s=s+1; } prev++; i++; } else if(prev < t){ str.append("R"); s=s+1; prev++; } } if(s<f){ for(i=0;i<f-s;i++) str.append("R"); } w.print(str); } else if(s>f){ i=0; while(i<m){ int t = ab[i][0]; int a = ab[i][1]; int b = ab[i][2]; if(s == f) break; if(prev == t){ if((s>=a && s<=b) || (s-1>=a && s-1<=b)) { str.append("X"); } else{ str.append("L"); s=s-1; } prev++; i++; } else if(prev < t){ str.append("L"); s=s-1; prev++; } } if(s>f){ for(i=0;i<s-f;i++) str.append("L"); } w.print(str); } w.close(); } } // System.out.println();
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class IEEE { public static void main(String[] args) throws IOException { //Scanner sc=new Scanner(System.in); BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer ss=new StringTokenizer(r.readLine()); int n=Integer.parseInt(ss.nextToken()); int m=Integer.parseInt(ss.nextToken()); int s=Integer.parseInt(ss.nextToken()); int f=Integer.parseInt(ss.nextToken()); int[][] st=new int[m][3]; for(int i=0;i<m;i++){ ss=new StringTokenizer(r.readLine()); st[i][0]=Integer.parseInt(ss.nextToken()); st[i][1]=Integer.parseInt(ss.nextToken()); st[i][2]=Integer.parseInt(ss.nextToken()); } //________________ansewer start here__________________\\ char d=(s<f?'R':'L'); int steps=0; int sq=0; if(s<f){ while (s!=f){ if(sq>=st.length){ System.out.print(d); s++; } else if(steps+1==st[sq][0]){ if(s+1<st[sq][1] || s>st[sq][2]){ System.out.print(d); s++; }else System.out.print("X"); sq++; }else{ System.out.print(d); s++; } steps++; } }else{ while(s!=f){ if(sq>=st.length){ System.out.print(d); s--; } else if(steps+1==st[sq][0]){ if(s-1 > st[sq][2] || s < st[sq][1]){ System.out.print(d); s--; }else System.out.print("X"); sq++; }else{ System.out.print(d); s--; } steps++; } } } public static void dis(int a[]){ for(int i:a){ System.out.print(i+" "); } System.out.println(""); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Spies { public static void main(String[] args) throws IOException { InputStreamReader a = new InputStreamReader(System.in); BufferedReader buf = new BufferedReader(a); String[] temp = buf.readLine().split(" "); int n=Integer.parseInt(temp[0]); int m=Integer.parseInt(temp[1]); int s=Integer.parseInt(temp[2]); int f=Integer.parseInt(temp[3]); int t=1,ti,li,ri,curr=s,dir=1; String d= "R"; if (f<s) { d="L"; dir=-1; } for (int i=0; i<m; i++) { temp = buf.readLine().split(" "); ti=Integer.parseInt(temp[0]); li=Integer.parseInt(temp[1]); ri=Integer.parseInt(temp[2]); while(t!=ti && curr!=f) { curr+=dir; System.out.print(d); t++; } if (curr==f) break; if((curr>=li && curr<=ri) || (dir==1 && curr==li-1) || (dir==-1 && curr==ri+1)) System.out.print("X"); else { curr+=dir; System.out.print(d); } t++; } while (curr!=f) { curr+=dir; System.out.print(d); } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; void FAST() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int Rselect(vector<int>&, int, int, int); int partition(vector<int>&, int, int); void scanc(vector<char>& v, long long n) { for (int i = 0; i < n; i++) { char num; cin >> num; v.push_back(num); } } template <typename T> ostream& operator<<(ostream& stream, const vector<T>& vec) { for (auto& i : vec) { stream << i << ' '; } stream << '\n'; return stream; } template <class T> istream& operator>>(istream& stream, vector<T>& vec) { for (auto& i : vec) { stream >> i; } return stream; } void scanN(vector<long long>& v, long long n) { for (int i = 0; i < n; i++) { int num; cin >> num; v.push_back(num); } } void scans(vector<string>& v, long long n) { for (int i = 0; i < n; i++) { string s; cin >> s; v.push_back(s); } } long long modfactorial(long long n, long long p) { if (n >= p) return 0; long long result = 1; for (int i = 1; i < n + 1; i++) { result = result * i; result = result % p; } return result; } long long MI(long long a, long long b, long long s0, long long s1) { long long k = b; if (b == 0) return s0; else { return MI(b, a % b, s1, s0 - s1 * (a / b)); } } long long choose(long long a, long long b, long long c) { if (a < b) return 0; long long x = modfactorial(a, c); long long y = modfactorial(b, c); long long z = modfactorial(a - b, c); long long y_ = MI(y, c, 1, 0); if (y_ < 0) y_ = y_ + c; long long z_ = MI(z, c, 1, 0); if (z_ < 0) z_ = z_ + c; long long mul = (x * y_) % c; mul = (mul * z_) % c; return mul; } long long modpow(long long n, long long p, long long k) { if (p == 0) return 1; long long result = 1; long long i = 1; while (i <= p) { result = result * n; result = result % k; } return result; } bool isPrime(int a) { for (int i = 2; i * i <= a; ++i) if (a % i == 0) return false; return true; } bool useflushdivisible(long long a) { cout << a << endl; fflush(stdout); char sl[10]; scanf("%s", sl); return sl[0] == 'y' || sl[0] == 'Y'; } long long phi(long long n) { long long result = n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n = n / i; result = result - result / i; } } if (n > 1) result = result - result / n; return result; } long long query(long long i, long long j) { cout << "? " << i << " " << j << endl; fflush(stdout); long long num; cin >> num; return num; } void merge(int a[], int head, int mid, int tail, bool increasing = false) { int* c = new int[tail - head + 1]; int i = head, j = mid, k = 0; while (k < tail - head + 1) { if ((((a[i] > a[j] && increasing) || (a[i] < a[j] && !increasing)) || i == mid) && j != tail + 1) { c[k] = a[j]; j++; } else { c[k] = a[i]; i++; } k++; } i = 0; while (i < k) { a[head + i] = c[i]; i++; } delete[] c; } void mergesort(int a[], int head, int tail) { if (head != tail) { mergesort(a, head, (tail - head + 1) / 2 + head - 1); mergesort(a, (tail - head + 1) / 2 + head, tail); merge(a, head, (tail - head + 1) / 2 + head, tail); } } void solve() { int l, r, s, n, m, t, f, pre = 0; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { cin >> t >> l >> r; while (pre < t - 1) { if (f > s) { cout << "R"; s++; } else if (f < s) { cout << "L"; s--; } pre++; } if (s != f) { if (f > s) { if (s > r || s + 1 < l) { s++; cout << "R"; } else cout << "X"; } else if (f < s) { if (s < l || s - 1 > r) { s--; cout << "L"; } else cout << "X"; } } else { break; } pre = t; } while (f > s) { s++; cout << "R"; } while (f < s) { s--; cout << "L"; } } void SOLT() { int test; cin >> test; while (test--) { solve(); } } int main() { FAST(); solve(); return 0; } int Rselect(vector<int>& v, int i, int l, int r) { if (l == r) return v[l]; int pivot = partition(v, l, r); if (pivot == i) return v[pivot - 1]; else if (pivot < i) { return Rselect(v, i, pivot, r); } else { return Rselect(v, i, l, pivot - 2); } } int partition(vector<int>& v, int l, int r) { int pivot_index = rand() % (r - l + 1) + l; swap(v[pivot_index], v[l]); int i = l + 1, j = l + 1; while (j <= r) { if (v[j] < v[l]) { swap(v[j], v[i]); i++; } j++; } swap(v[l], v[i - 1]); return i; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, st, ed, dir, ok; int te[100005], lf[100005], rt[100005]; int main() { scanf("%d%d%d%d", &n, &m, &st, &ed); for (int i = (0); i < (m); i++) scanf("%d%d%d", &te[i], &lf[i], &rt[i]); for (int i = 1, j = 0; st != ed; i++) { while (j < m && te[j] < i) ++j; if (st < ed) dir = 1; else dir = -1; ok = 1; if (te[j] == i) { if (lf[j] <= st && st <= rt[j]) ok = 0; if (lf[j] <= st + dir && st + dir <= rt[j]) ok = 0; } if (ok) st += dir, printf("%c", dir > 0 ? 'R' : 'L'); else printf("X"); } puts(""); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { vector<bool> v; vector<int> ans; int n, t, s, f, a, b, c, curs = 1, curr; scanf("%d%d%d%d", &n, &t, &s, &f); curr = s; if (s < f) { while (t--) { scanf("%d%d%d", &a, &b, &c); if (curr == f) continue; if (curs < a) { if (!v.size() || !v[v.size() - 1]) { v.push_back(1); ans.push_back(min(a - curs, f - curr)); } else if (v[v.size() - 1]) ans[ans.size() - 1] += min(a - curs, f - curr); curr += min(a - curs, f - curr); curs = a; } if ((curr + 1 < b || curr > c) && curr < f) { if (!v.size() || !v[v.size() - 1]) { v.push_back(1); ans.push_back(1); } else ans[ans.size() - 1]++; curr++; } else if (curr < f) { if (!v.size() || v[v.size() - 1]) { v.push_back(0); ans.push_back(1); } else ans[ans.size() - 1]++; } curs++; } } else { while (t--) { scanf("%d%d%d", &a, &b, &c); if (curr == f) continue; if (curs < a) { if (!v.size() || !v[v.size() - 1]) { v.push_back(1); ans.push_back(min(a - curs, curr - f)); } else if (v[v.size() - 1]) ans[ans.size() - 1] += min(a - curs, curr - f); curr -= min(a - curs, curr - f); curs = a; } if ((curr < b || curr - 1 > c) && curr > f) { if (!v.size() || !v[v.size() - 1]) { v.push_back(1); ans.push_back(1); } else ans[ans.size() - 1]++; curr--; } else if (curr > f) { if (!v.size() || v[v.size() - 1]) { v.push_back(0); ans.push_back(1); } else ans[ans.size() - 1]++; } curs++; } } for (int i = 0; i < v.size(); i++) { if (v[i]) { for (int j = 0; j < ans[i] && s < f; j++) printf("R"); for (int j = 0; j < ans[i] && s > f; j++) printf("L"); } for (int j = 0; j < ans[i] && !v[i]; j++) printf("X"); } for (int i = 0; i < abs(f - curr) && s < f; i++) printf("R"); for (int i = 0; i < abs(f - curr) && s > f; i++) printf("L"); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int M = 1000000000 + 7; const double eps = 1e-10; struct node { int t, l, r; } num[100005]; int cmp(node a, node b) { return a.t < b.t; } char ans[1000005]; int main() { int n, m, s, f; while (~scanf("%d %d %d %d", &n, &m, &s, &f)) { for (int i = 1; i <= m; i++) scanf("%d %d %d", &num[i].t, &num[i].l, &num[i].r); int time = 0; int now = s; int in = 1; sort(num + 1, num + m + 1, cmp); while (now != f && in <= m) { int tmp = num[in].t - time; while (tmp > 1) { if (now < f) ans[time++] = 'R', now++; else if (now > f) ans[time++] = 'L', now--; else break; tmp--; } if (now == f) break; if (now < f) { if ((num[in].l > now || num[in].r < now) && (num[in].l > now + 1 || num[in].r < now + 1)) { ans[time++] = 'R'; now++; } else ans[time++] = 'X'; } else { if ((num[in].l > now || num[in].r < now) && (num[in].l > now - 1 || num[in].r < now - 1)) { ans[time++] = 'L'; now--; } else ans[time++] = 'X'; } in++; } while (now != f) { if (now < f) ans[time++] = 'R', now++; else if (now > f) ans[time++] = 'L', now--; } ans[time] = '\0'; printf("%s\n", ans); } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int n, m, s, f; int t, x, y; int step; int main() { scanf("%d %d %d %d", &n, &m, &s, &f); step = 1; int temp; for (int i = 0; i < m; i++) { temp = s; scanf("%d %d %d", &t, &x, &y); while (step != t) { step++; if (temp == f) { printf("\n"); return 0; } if (s < f) { temp++; printf("R"); } else { temp--; printf("L"); } } s = temp; step++; if (s == f) { printf("\n"); return 0; } char ch; if (s < f) { temp++; ch = 'R'; } else { temp--; ch = 'L'; } if ((s >= x && s <= y) || (temp >= x && temp <= y)) { printf("X"); } else { printf("%c", ch); s = temp; } } while (s != f) { if (temp == f) { printf("\n"); return 0; } if (s < f) { s++; printf("R"); } else { s--; printf("L"); } } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class XeniaSpecies implements Runnable { static class State { int t, a, b; public State(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } } private void solve() throws IOException { int n = nextInt(); int m = nextInt(); int s = nextInt(); int f = nextInt(); int from = s; int step = 1; State[] scans = new State[m]; for (int i = 0; i < scans.length; i++) { scans[i] = new State(nextInt(), nextInt(), nextInt()); } if (s == f) { pl(""); return; } StringBuilder sb = new StringBuilder(); int dir = s > f ? -1 : 1; String which = s > f ? "L" : "R"; for (int i = 0; i < m;) { int t = scans[i].t; int a = scans[i].a; int b = scans[i].b; if (t != step) { sb.append(which); from += dir; } else { if ((from >= a && from <= b) || (from + dir >= a && from + dir <= b)) { sb.append("X"); } else { sb.append(which); from += dir; } ++i; } if (from == f) { break; } step++; } while (from != f) { sb.append(which); from += dir; } pl(sb); } public static void main(String[] args) { new XeniaSpecies().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new BufferedReader( new InputStreamReader(System.in))); writer = new PrintWriter(System.out); tokenizer = null; solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } void p(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } void pl(Object... objects) { p(objects); writer.println(); } int cc; void pf() { writer.printf("Case #%d: ", ++cc); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long co; int arp[100004]; int non; int main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, s, f; cin >> n >> m >> s >> f; long long a[m][3]; for (int i = 0; i < m; i++) { cin >> a[i][0]; cin >> a[i][1]; cin >> a[i][2]; } int i, j, x; i = 1; j = 0; x = s; while (1) { if (i == a[j][0]) { if (f > x) { if ((x >= a[j][1] && x <= a[j][2]) || ((x + 1 >= a[j][1]) && (x + 1 <= a[j][2]))) cout << "X"; else { cout << "R"; x++; } } else if (f < x) { if ((x <= a[j][2] && x >= a[j][1]) || (x - 1 <= a[j][2] && x - 1 >= a[j][1])) cout << "X"; else { cout << "L"; x--; } } else if (f == x) { break; } j++; } else { if (f > x) { cout << "R"; x++; } else if (f < x) { cout << "L"; x--; } else if (f == x) { break; } } i++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, m, s, f; int t[MAXN], l[MAXN], r[MAXN]; bool check(long long p, long long l, long long r) { return (p - l) * (p - r) <= 0; } int main(int argc, char const *argv[]) { scanf("%d%d%d%d", &n, &m, &s, &f); for (int i = 0; i < m; ++i) scanf("%d%d%d", t + i, l + i, r + i); int i = 0, j = 1; int dir = f - s; if (dir > 0) dir = 1; else dir = -1; while (s != f) { int nxt = s + dir; if (t[i] == j && (check(nxt, l[i], r[i]) || check(s, l[i], r[i]))) putchar('X'); else { putchar(dir > 0 ? 'R' : 'L'); s = nxt; } ++j; if (j > t[i]) ++i; } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f = map(int,raw_input().split()) flag = 1 direct = { 1 : "R", -1 : "L" } if s > f : flag = -1 res = "" pre_t = 0 for i in range(m) : t,l,r = map(int,raw_input().split()) if s == f: break if t != (pre_t + 1) : num = 0 if ( t - pre_t - 1) > abs(f-s): num = abs(f-s) else : num = (t-pre_t-1) res += direct[flag] * num s = s + flag * num if s == f: break pre_t = t n_s = s+flag if (s <= r and s >= l) or (n_s <= r and n_s >= l) : res += "X" else : s = n_s res += direct[flag] if s != f: res += direct[flag] * abs(f-s) print res
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { FastReader in = new FastReader(); int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); int cs = 1; for(int i = 0; i < m; i++) { int t = in.nextInt(); int l = in.nextInt(); int r = in.nextInt(); while(cs < t) { if(s==f) break; else if(s < f) { s++; System.out.print("R"); } else { s--; System.out.print("L"); } cs++; } if(s==f) break; else { if(f>s) { if(s+1<l || s>r) { s++; System.out.print("R"); } else System.out.print("X"); } else { if(s<l || s-1>r) { s--; System.out.print("L"); } else System.out.print("X"); } } cs++; } while(s<f) { System.out.print("R"); s++; } while(s>f) { System.out.print("L"); s--; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readArray(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; import java.math.*; public class B implements Runnable { static BufferedReader in; static PrintWriter out; static StringTokenizer st; static Random rnd; class Segment { int time, l, r; public Segment(int time, int l, int r) { this.time = time; this.l = l; this.r = r; } public boolean isWatching(int c) { return l <= c && c <= r; } } private void solve() throws IOException { int n = nextInt(), m = nextInt(); int currentId = nextInt() - 1, endId = nextInt() - 1; Segment[] segments = new Segment[m]; for (int i = 0; i < m; i++) segments[i] = new Segment(nextInt(), nextInt() - 1, nextInt() - 1); int time = 1, segmentPtr = 0; while (currentId != endId) { while (segmentPtr < m && segments[segmentPtr].time < time) ++segmentPtr; int diff = Integer.signum(endId - currentId); boolean stay = false; if (segmentPtr < m && segments[segmentPtr].time == time) { if (segments[segmentPtr].isWatching(currentId) || segments[segmentPtr].isWatching(currentId + diff)) stay = true; } if (stay) { out.print('X'); } else { if (diff == 1) { out.print('R'); } else if (diff == -1) { out.print('L'); } else { throw new AssertionError(); } currentId += diff; } ++time; } } public static void main(String[] args) { new B().run(); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); rnd = new Random(); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } private String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct steps { int l, r, step; }; int main() { int n, m, s, f; cin >> n >> m >> s >> f; vector<steps> v(m); char left = 'L', right = 'R', none = 'X'; for (int i = 0; i < m; i++) cin >> v[i].step >> v[i].l >> v[i].r; int i, j, st = 1; if (s < f) { for (i = s, j = 0; i < f && j < m; j++, st++) { if (st == v[j].step && ((v[j].l <= i && v[j].r >= i) || (v[j].l <= i + 1 && v[j].r >= i + 1))) { cout << 'X'; continue; } else if (st == v[j].step) { cout << right, i++; } bool flag = false; if (st < v[j].step) flag = 1; while (st < v[j].step && i < f) cout << right, i++, st++; if (flag) j--, st--; } while (i < f) cout << right, i++; } else { for (i = s, j = 0; i > f && j < m; j++, st++) { if (st == v[j].step && ((v[j].l <= i && v[j].r >= i) || (v[j].l <= i - 1 && v[j].r >= i - 1))) { cout << 'X'; continue; } else if (st == v[j].step) { cout << left, i--; } bool flag = false; if (st < v[j].step) flag = 1; while (st < v[j].step && i > f) cout << left, i--, st++; if (flag) j--, st--; } while (i > f) cout << left, i--; } cout << "\n"; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long int inf = 1e18; long long int p = 998244353; long long int phi(long long int n) { long long int result = n; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int i, j, y, x, k, z, w, g, m, a, b, t, c, key, q, l, r, n; long long int t2, t3, t4, t1; char temp; long long int s, f; cin >> n >> m >> s >> f; string out; map<long long int, pair<long long int, long long int> > ma; while (m--) { cin >> x >> y >> z; ma[x] = make_pair(y, z); } long long int ans = 1; if (s < f) { while (1) { if (s == f) break; t1 = ma[ans].first; t2 = ma[ans].second; if ((s >= t1 && s <= t2) || (s + 1 >= t1 && s + 1 <= t2)) { ans++; out.push_back('X'); } else { ans++; out.push_back('R'); s++; } } } else { while (1) { if (s == f) break; t1 = ma[ans].first; t2 = ma[ans].second; if ((s >= t1 && s <= t2) || (s - 1 >= t1 && s - 1 <= t2)) { ans++; out.push_back('X'); } else { ans++; out.push_back('L'); s--; } } } cout << out; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.concurrent.ArrayBlockingQueue; public class B { String line; StringTokenizer inputParser; BufferedReader is; FileInputStream fstream; DataInputStream in; String FInput=""; void openInput(String file) { if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin else { try{ fstream = new FileInputStream(file); in = new DataInputStream(fstream); is = new BufferedReader(new InputStreamReader(in)); }catch(Exception e) { System.err.println(e); } } } void readNextLine() { try { line = is.readLine(); inputParser = new StringTokenizer(line, " ,\t"); //System.err.println("Input: " + line); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } catch (NullPointerException e) { line=null; } } long NextLong() { String n = inputParser.nextToken(); long val = Long.parseLong(n); return val; } int NextInt() { String n = inputParser.nextToken(); int val = Integer.parseInt(n); //System.out.println("I read this number: " + val); return val; } String NextString() { String n = inputParser.nextToken(); return n; } void closeInput() { try { is.close(); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } } public static void main(String [] argv) { //String filePath="circles.in"; //String filePath="D:\\_d\\learn\\coursera\\algorithms and design I\\data\\HashInt.txt"; String filePath=null; if(argv.length>0)filePath=argv[0]; new B(filePath); } public B(String inputFile) { openInput(inputFile); StringBuilder sb = new StringBuilder(); readNextLine(); int N=NextInt(),M=NextInt(),S=NextInt(),F=NextInt(); int t=1; if(F>S) { for(int i=0; i<M; i++) { readNextLine(); int T=NextInt(); int L=NextInt(); int R=NextInt(); if(S==F)continue; if(t<T) { int x=Math.min(T-t, F-S); for(int j=0; j<x; j++) { sb.append('R'); S++; } t=T; } if(S!=F) { if(S>=L&&S<=R||(S+1>=L&&S+1<=R))sb.append('X'); else { sb.append('R'); S++; } } t++; } for(int j=0; j<F-S; j++) sb.append('R'); } else { for(int i=0; i<M; i++) { readNextLine(); int T=NextInt(); int L=NextInt(); int R=NextInt(); if(S==F)continue; if(t<T) { int x=Math.min(T-t, S-F); for(int j=0; j<x; j++) { sb.append('L'); S--; } t=T; } if(S!=F) { if(S>=L&&S<=R||(S-1>=L&&S-1<=R))sb.append('X'); else { sb.append('L'); S--; } } t++; } for(int j=0; j<S-F; j++) sb.append('L'); } System.out.println(sb); closeInput(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created with IntelliJ IDEA. * User: shiwangi * Date: 9/7/13 * Time: 2:11 PM * To change this template use File | Settings | File Templates. */ public class srm1992 { public static void main(String[] args) throws IOException { InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); String tok[] = (br.readLine()).split(" "); int m,n,s,f; n = Integer.parseInt(tok[0]); m = Integer.parseInt(tok[1]); s = Integer.parseInt(tok[2]); f = Integer.parseInt(tok[3]); int dx; if(f<s) dx=-1; else dx=1; int t,l,r; int k=0; int time_now=1; while(s!=f){ if(k>=m) break; String toks[] = (br.readLine()).split(" "); k++; t = Integer.parseInt(toks[0]); l = Integer.parseInt(toks[1]); r = Integer.parseInt(toks[2]); while(t!=time_now && s!=f) { s=s+dx; if(dx==-1) System.out.print("L"); else System.out.print("R"); time_now++; } if((s!=f) && (!((s>=l && s<=r)||( (s+dx)>=l && (s+dx)<=r)))) { s=s+dx; if(dx==-1) System.out.print("L"); else System.out.print("R"); time_now++; } else if(s!=f){ System.out.print("X"); time_now++; } } while(s!=f) { s=s+dx; if(dx==-1) System.out.print("L"); else System.out.print("R"); } System.out.println(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long n, m, s, f, t; map<long long, long long> r, l; int main() { cin >> n >> m >> s >> f; while (m--) { long long x, y; cin >> t >> x >> y; l[t] = x, r[t] = y; } t = 1; while (s != f) { if (s < f) { if (!r[t] || !((s >= l[t] && s <= r[t]) || (s + 1 >= l[t] && s + 1 <= r[t]))) s++, cout << "R"; else cout << "X"; } else { if (!r[t] || !((s >= l[t] && s <= r[t]) || (s - 1 >= l[t] && s - 1 <= r[t]))) s--, cout << "L"; else cout << "X"; } t++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, i, x, l, r, t; string st; map<int, vector<int> > mp; cin >> n >> m >> s >> f; for (i = 1; i <= m; i++) { cin >> x >> l >> r; mp[x].push_back(l); mp[x].push_back(r); } int pos; if (s < f) { for (t = 1, pos = s; pos < f; t++) { if (mp.find(t) == mp.end()) { st += 'R'; pos++; } else if ((pos < mp[t][0] || pos > mp[t][1]) && (pos + 1 < mp[t][0] || pos + 1 > mp[t][1])) { st += 'R'; pos++; } else st += 'X'; } } else { for (t = 1, pos = s; pos > f; t++) { if (mp.find(t) == mp.end()) { st += 'L'; pos--; } else if ((pos < mp[t][0] || pos > mp[t][1]) && (pos - 1 < mp[t][0] || pos - 1 > mp[t][1])) { st += 'L'; pos--; } else st += 'X'; } } printf("%s\n", st.c_str()); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int t[100002], l[100002], r[100002]; int now = 1; int main() { int n, m, s, f; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i]; int i = 0; int h = -1; if (s < f) h = 1; while (s != f) { if (t[i] != now || (t[i] == now && !(s >= l[i] && s <= r[i]) && !(s + h >= l[i] && s + h <= r[i]))) { if (s < f) { cout << 'R'; s++; } else { s--; cout << 'L'; } now++; if (now > t[i]) i++; } else { cout << 'X'; now++; if (now > t[i]) i++; } } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; public class a { static long mod = 1000000007; public static void main(String[] args) throws IOException { // Scanner input = new Scanner(new File("input.txt")); // PrintWriter out = new PrintWriter(new File("output.txt")); input.init(System.in); PrintWriter out = new PrintWriter((System.out)); int n = input.nextInt(), m = input.nextInt(), s = input.nextInt(), f = input.nextInt(); int[] ts = new int[m], ls = new int[m], rs = new int[m]; for(int i = 0; i<m; i++) { ts[i] = input.nextInt(); ls[i] = input.nextInt(); rs[i] = input.nextInt(); } ArrayList<Character> list = new ArrayList<Character>(); int at = s, ati = 0; int t = 1; while(at!= f) { int to = f > at ? at+1 : at-1; if(ati < m && t == ts[ati]) { if(at >= ls[ati] && at <= rs[ati] || to >= ls[ati] && to <= rs[ati]) { list.add('X'); } else { if(to > at) { list.add('R'); } else { list.add('L'); } at = to; } ati++; } else { if(to > at) { list.add('R'); } else { list.add('L'); } at = to; } t++; } for(char c: list) out.print(c); out.close(); } static long pow(long x, long p) { if (p == 0) return 1; if ((p & 1) > 0) { return (x * pow(x, p - 1)) % mod; } long sqrt = pow(x, p / 2); return (sqrt * sqrt) % mod; } static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { // TODO add check for eof if necessary tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String nextLine() throws IOException { return reader.readLine(); } } public static boolean[] sieve(int n) { boolean[] res = new boolean[n+1]; for(int i = 2; i<=n; i++) res[i] = true; for(int i = 2; i <= Math.sqrt(n); i++) if(res[i]) for(long j = (long)i*i; j<=n; j+=i) res[(int) j] = false; return res; } //GCD - O(log(max(p, q))) //res[0] is gcd of p and q //res[1] and res[2] are values used in extended Euclidean algorithm for inverse mod public static long[] gcd(long p, long q) { if(q==0) return new long[] {p, 1, 0}; long[] vals = gcd(q, p%q); return new long[] {vals[0], vals[2], vals[1] - (p/q)*vals[2]}; } //Inverse Mod - O(log(mod)) //Returns A s.t. x*A = 1(MOD mod) public static long invmod(long x) { long[] vals = gcd(x, mod); if(vals[0]>1) return -1; if(vals[1]>0) return vals[1]; return mod + vals[1]; } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def main(): n, m, s, f = map(int, raw_input().split()) d = dict() for _ in xrange(m): t, l, r = map(int, raw_input().split()) d[t] = [l, r] ans = [] t = 1 mv = 'L' if s > f else 'R' dx = -1 if s > f else 1 while s != f: if t in d and (d[t][0] <= s <= d[t][1] or d[t][0] <= s + dx <= d[t][1]): ans.append('X') else: ans.append(mv) s += dx t += 1 print ''.join(ans) main()
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, st, en; cin >> n >> m >> st >> en; string ans = ""; int tt = 0; while (m--) { int t, l, r; cin >> t >> l >> r; if (t != tt + 1) { while (t != tt + 1) { tt++; if (en > st) { ans += 'R'; st++; } else if (st > en) { ans += 'L'; st--; } } } tt++; if (st == en) { continue; } else if (st > en) { if (st < l || st - 1 > r) { ans += 'L'; st--; } else { ans += 'X'; } } else { if (st > r || st + 1 < l) { ans += 'R'; st++; } else { ans += 'X'; } } } while (st != en) { if (st > en) { en++; ans += 'L'; } else { st++; ans += 'R'; } } cout << ans << "\n"; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def mi(): return map(int, input().split()) ''' 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 ''' n,m,s,f = mi() t = [0]*m l = [0]*m r = [0]*m for i in range(m): t[i],l[i],r[i] = mi() curp = s curt = 1 i = 0 a = '' if f>s: a = (f-s)*'R' else: a = (s-f)*'L' while i<m and curp!=f: if t[i]==curt: if a[0]=='R' and curp+1>=l[i] and curp+1<=r[i]: print ('X', end = '') elif a[0]=='L' and curp-1>=l[i] and curp-1<=r[i]: print ('X', end = '') elif curp>=l[i] and curp<=r[i]: print ('X', end = '') elif a[0]=='R': curp+=1 print(a[0], end = '') else: curp-=1 print (a[0], end = '') i+=1 elif a[0]=='R': curp+=1 print(a[0], end = '') else: curp-=1 print (a[0], end = '') curt+=1 if f>curp: print ((f-curp)*'R') elif curp>f: print ((curp-f)*'L')
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, t, l, r, dir, curStep = 1, ctr = 0; char arr[30]; arr[5 + 1] = 'R', arr[5 - 1] = 'L'; cin >> n >> m >> s >> f; if (f > s) dir = 1; else dir = -1; while (s != f) { if (ctr != m) cin >> t >> l >> r, ++ctr; else t = 1000000001; while (curStep != t && s != f) s = s + dir, cout << arr[5 + dir], ++curStep; if (s == f) break; if ((s >= l && s <= r) || s + dir == l || s + dir == r) cout << "X"; else s = s + dir, cout << arr[5 + dir]; ++curStep; } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; const int MAX_M = 100000; const int MAX_L = MAX_N + MAX_M; char v[MAX_L + 4]; int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); int d; char c; if (s < f) d = 1, c = 'R'; else d = -1, c = 'L'; int t = 0; while (m-- && s != f) { int ti, li, ri; scanf("%d%d%d", &ti, &li, &ri); ti--; while (t < ti && s != f) { v[t++] = c; s += d; } if (s == f) break; int x = s + d; if ((li <= s && s <= ri) || (li <= x && x <= ri)) v[t++] = 'X'; else { v[t++] = c; s += d; } } while (s != f) v[t++] = c, s += d; puts(v); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); int spies = sc.nextInt(); int checks = sc.nextInt(); int start = sc.nextInt(); int finish = sc.nextInt(); int dir = (finish-start)/Math.abs(finish-start); int time = 1; int[] t = new int[checks+1]; int[] L = new int[checks+1]; int[] R = new int[checks+1]; for(int a=0;a<checks;a++){ t[a]=sc.nextInt(); L[a]=sc.nextInt(); R[a]=sc.nextInt(); } L[checks]=R[checks]=-1; t[checks]=Integer.MAX_VALUE; int i=0; while(true){ if(start==finish)return; if(time==t[i]){ if(L[i]<=start+dir&&start+dir<=R[i]){ System.out.print("X"); i++; time++; continue; } else if(L[i]<=start&&start<=R[i]){ System.out.print("X"); i++; time++; continue; } i++; } time++; start+=dir; if(dir==1)System.out.print("R"); else System.out.print("L"); } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) throws Exception { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(br.readLine().trim()); } public int numTokens() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); return numTokens(); } return st.countTokens(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); return next(); } return st.nextToken(); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public float nextFloat() throws Exception { return Float.parseFloat(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public String nextLine() throws Exception { return br.readLine().trim(); } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, s, f, t, l, r; cin >> n >> m >> s >> f; map<long long, long long> L, R; for (int i = 1; i <= m; ++i) { cin >> t >> l >> r; L[t] = l; R[t] = r; } string res = ""; for (int i = 1;; ++i) { l = L[i]; r = R[i]; if (s == f) break; if (s < f) { if (s + 1 >= l && s <= r) res += "X"; else s++, res += "R"; } else if (s > f) { if (s >= l && s - 1 <= r) res += "X"; else s--, res += "L"; } } cout << res << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct node { int t, l, r; }; node a[100005]; char ans[300005]; bool cmp(node a, node b) { return a.t < b.t; } int main() { int n, m, s, f; int i, j; scanf("%d%d%d%d", &n, &m, &s, &f); for (i = 1; i <= m; i++) { scanf("%d%d%d", &a[i].t, &a[i].l, &a[i].r); } sort(a + 1, a + 1 + m, cmp); int ok = 0, time = 0, cnt = 1; char g; int gg; if (s < f) { g = 'R'; gg = 1; } else { g = 'L'; gg = -1; } int pos = s; while (!ok) { time++; int left = 1000000000, right = 0; for (;;) { if (a[cnt].t != time) break; left = min(left, a[cnt].l); right = max(right, a[cnt].r); cnt++; } if ((pos >= left && pos <= right) || ((pos + gg) >= left && (pos + gg) <= right)) { ans[time] = 'X'; } else { ans[time] = g; pos += gg; if (pos == f) ok = 1; } } for (i = 1; i <= time; i++) { printf("%c", ans[i]); } printf("\n"); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f=map(int,input().split()) d={} final=0 for i in range(m): a=list(map(int,input().split())) d[a[0]]=[a[1],a[2]] final=a[0] z="" if s<f: i=0 while s!=f: i+=1 if i not in d: s+=1 z+="R" elif (s not in range(d[i][0],d[i][1]+1)) and (s+1 not in range(d[i][0],d[i][1]+1)): s+=1 z+="R" else: z+="X" if s==f: break else: i=0 while s!=f: i+=1 if i not in d: s -= 1 z += "L" elif (s not in range(d[i][0], d[i][1] + 1)) and (s - 1 not in range(d[i][0], d[i][1] + 1)): s-=1 z+="L" else: z+="X" if s==f: break print(z)
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; struct reverse { bool operator()(const int &left, const int &right) { return (right < left); } }; struct custom { bool operator()(const pair<int, int> &left, const pair<int, int> &right) { return (left.first > right.first); } }; void solve() { int n, m, s, f; cin >> n >> m >> s >> f; s -= 1; f -= 1; string res = ""; int d = f > s ? 1 : -1; int i = 1; int t, l, r; cin >> t >> l >> r; int counter = 1; while (true) { if (s == f) break; if (i > t and counter <= m - 1) { cin >> t >> l >> r; counter++; } if (t == i) { if ((l - 1 <= s and s <= r - 1) or (l - 1 <= s + d and s + d <= r - 1)) res += 'X'; else { s += d; res += d == 1 ? 'R' : 'L'; } } else { s += d; res += d == 1 ? 'R' : 'L'; } i++; } cout << res << endl; } int main(int argc, char const *argv[]) { solve(); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int nm = 100002; int n, m, s, f; int t[nm], x[nm], y[nm]; void nhap() { scanf("%d%d%d%d", &n, &m, &s, &f); int i; for (i = 1; i <= m; ++i) scanf("%d%d%d", &t[i], &x[i], &y[i]); } bool kt(int i, int j) { return (i >= x[j] && i <= y[j]); } void xuli() { int i = s; int time = 0; int j = 1; int u; while (1) { time++; while (j <= m && t[j] < time) j++; if (s < f) u = i + 1; else u = i - 1; if (j <= m && t[j] == time && (kt(i, j) || kt(u, j))) { printf("X"); } else { if (s < f) printf("R"); else printf("L"); i = u; if (i == f) break; } } } int main() { nhap(); xuli(); }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> const int MAX_SIZE = 100100; int f[MAX_SIZE], l[MAX_SIZE], r[MAX_SIZE]; int n, m; int from, to; int main() { scanf("%d%d%d%d", &n, &m, &from, &to); for (int i = 1; i <= m; i++) scanf("%d%d%d", f + i, l + i, r + i); int dir = to - from; int step = 1; int x = 0; f[0] = 0; l[0] = r[0] = -1; for (int cur = from; cur != to;) { if (step == f[x + 1]) ++x; if (f[x] == step && l[x] <= cur && r[x] >= cur) { printf("X"); } else if (f[x] == step && dir > 0 && l[x] <= cur + 1 && r[x] >= cur + 1) printf("X"); else if (f[x] == step && dir < 0 && l[x] <= cur - 1 && r[x] >= cur - 1) printf("X"); else if (dir > 0) { printf("R"); ++cur; } else { printf("L"); --cur; } ++step; } printf("\n"); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; int main() { long long int n, m, s, f, j = 0; cin >> n >> m >> s >> f; vector<long long int> l, r, t; for (long long int i = 0; i < m; i++) { long long int te, le, re; cin >> te >> le >> re; t.push_back(te); l.push_back(le); r.push_back(re); } vector<char> ans; char c = 'L'; if (f > s) { c = 'R'; } long long int cur = s; for (long long int i = 1; i < 1e6; i++) { if (cur == f) { break; } if (t[j] == i) { if (f > s) { if (l[j] > cur + 1 || r[j] < cur) { cur++; ans.push_back(c); } else { ans.push_back('X'); } } else { if (l[j] > cur || r[j] < cur - 1) { cur--; ans.push_back(c); } else { ans.push_back('X'); } } j++; } else { if (s < f) cur++; else { cur--; } ans.push_back(c); } } for (long long int i = 0; i < ans.size(); i++) { cout << ans[i]; } cout << "\n"; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int TT, NP; int L, R, N, M, F, T; int K[MAXN][3]; int main() { cin >> N >> M >> F >> T; memset(K, -1, sizeof(K)); for (int i = 1; i <= M; i++) cin >> K[i][0] >> K[i][1] >> K[i][2]; TT = 0; NP = F; int index = 1; if (F < T) while (NP != T) { ++TT; if (K[index][0] == TT) { if (K[index][1] > NP + 1 || K[index][2] < NP) cout << "R", NP++; else cout << "X"; index++; } else cout << "R", NP++; } else while (NP != T) { ++TT; if (K[index][0] == TT) { if (K[index][1] > NP || K[index][2] < NP - 1) cout << "L", NP--; else cout << "X"; index++; } else cout << "L", NP--; } cout << endl; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class XeniaAndSpies { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n; int m,s,f; n = sc.nextInt(); m = sc.nextInt(); s = sc.nextInt(); f = sc.nextInt(); int[] t = new int[m]; int[] l = new int[m]; int[] r = new int[m]; for(int i=0;i<m;i++) { t[i] = sc.nextInt(); l[i] = sc.nextInt(); r[i] = sc.nextInt(); } char[] ch = new char[1000000]; int outIndex = 0; int tIndex = 0; int T = 1; while(s!=f) { if(s < f) { if(tIndex >= m) { s++; ch[outIndex] = 'R'; } else { if(T < t[tIndex]) { s++; ch[outIndex] = 'R'; } else if(T == t[tIndex]) { if(s >= l[tIndex]-1 && s<= r[tIndex]) { ch[outIndex] = 'X'; } else { ch[outIndex] = 'R'; s++; } tIndex++; } } } else { if(tIndex >= m) { s--; ch[outIndex] = 'L'; } else { if(T < t[tIndex]) { s--; ch[outIndex] = 'L'; } else if(T == t[tIndex]) { if(s >= l[tIndex] && s<= r[tIndex]+1) { ch[outIndex] = 'X'; } else { ch[outIndex] = 'L'; s--; } tIndex++; } } } outIndex++; T++; } for(int i=0;i<outIndex;i++) System.out.print(ch[i]); System.out.println(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; long long dp[100000 + 25]; struct node { int iz, der; long long tiempo; node() {} node(long long t, int i, int d) { tiempo = t; iz = i; der = d; } }; struct otro { int proct, pos; long long tiemp; otro() {} otro(int ps, long long time, int timprt) { pos = ps; tiemp = time; proct = timprt; } bool operator<(const otro& algo) const { return algo.tiemp < tiemp; } }; vector<node> v; int n, m, ini, fin; int valoreleg; char eleg; char ch[500000]; map<long long, int> mp; void f() { long long tiemp = 1; int c = 0; int pos = ini; while (true) { if (v[mp[tiemp]].iz <= pos && v[mp[tiemp]].der >= pos) { ch[c++] = 'X'; tiemp++; continue; } pos += valoreleg; ch[c] = eleg; if (v[mp[tiemp]].iz <= pos && v[mp[tiemp]].der >= pos) { pos -= valoreleg; ch[c] = 'X'; } c++; tiemp++; if (pos == fin) { for (int a = 0; a < c; a++) cout << ch[a]; cout << endl; return; } } } int main() { cin >> n >> m >> ini >> fin; int li, ri; long long tim; v.push_back(node(0, 0, 0)); for (int a = 0; a < m; a++) { cin >> tim >> li >> ri; mp[tim] = a + 1; v.push_back(node(tim, li, ri)); } if (ini < fin) { valoreleg = 1; eleg = 'R'; } else { valoreleg = -1; eleg = 'L'; } f(); }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.Vector; public class Main { /** * @param args * @throws IOException */ class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString(int maxSize) { byte[] ch = new byte[maxSize]; int point = 0; try { byte c = read(); while (c == ' ' || c == '\n' || c=='\r') c = read(); while (c != ' ' && c != '\n' && c!='\r') { ch[point++] = c; c = read(); } } catch (Exception e) {} return new String(ch, 0,point); } public int nextInt() { int ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } public long nextLong() { long ret = 0; boolean neg; try { byte c = read(); while (c <= ' ') c = read(); neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; } catch (Exception e) {} return ret; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (Exception e) {} if (bytesRead == -1) buffer[ 0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner pr=new Scanner(System.in); int n,m,s,f; n=pr.nextInt(); m=pr.nextInt(); s=pr.nextInt(); f=pr.nextInt(); int t,l,r; int p=1; String res=new String(""); boolean done=false; for(int i=0; i<m; i++) { t=pr.nextInt(); l=pr.nextInt(); r=pr.nextInt(); if(!done) { while(t!=p) { p++; if(s<f) { s++; System.out.print('R'); }else if(s>f) { s--; System.out.print('L'); } if(s==f) { done=true; break; } } if(!done) if(s<f&&(s>r||s<l-1)) { s++; System.out.print('R'); }else if(s>f&&(s>r+1||s<l)) { s--; System.out.print('L'); }else if(s!=f) System.out.print('X'); } p++; } if(s!=f) { if(s>f) for(int i=f; i<s; i++) System.out.print('L'); else for(int i=s; i<f; i++) System.out.print('R'); } System.out.print(res); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
/*package com.fancita.codeforces;*/ import java.io.*; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Queue; /** * Created by ashutosh on 26/12/16. */ public class b199 extends FastIO { public static void main(String[] args) { int n = reader.readInt(); int m = reader.readInt(); int s = reader.readInt(); int f = reader.readInt(); Queue<DetectiveStep> queue = new LinkedList<>(); for (int i = 0; i < m; i++) { queue.add(new DetectiveStep(reader.readInt(), reader.readInt(), reader.readInt())); } StringBuilder ret = new StringBuilder((f > s ? f - s : s - f) + m); char toInsert = f > s ? 'R' : 'L'; char def = 'X'; int presentStep = 1; int presentPosition = s; while (presentPosition != f) { DetectiveStep detectiveStep = null; if (!queue.isEmpty()) { detectiveStep = queue.peek(); } int nextPosition = f > s ? presentPosition + 1 : presentPosition - 1; if (detectiveStep != null && detectiveStep.step == presentStep) { queue.remove(); if ((presentPosition >= detectiveStep.l && presentPosition <= detectiveStep.r) || (nextPosition >= detectiveStep.l && nextPosition <= detectiveStep.r)) { ret.append(def); presentStep++; } else { ret.append(toInsert); presentStep++; presentPosition = nextPosition; } } else { ret.append(toInsert); presentStep++; presentPosition = nextPosition; } } writer.printLine(ret.toString()); writer.flush(); writer.close(); } } class DetectiveStep { int step, l, r; public DetectiveStep(int step, int l, int r) { this.step = step; this.l = l; this.r = r; } } class FastIO { public static InputReader reader = new InputReader(System.in); public static OutputWriter writer = new OutputWriter(System.out); public static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } public static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } /** * Read items into array starting from a given point * @param in * @param arr * @param i */ public static void readIntArray(InputReader in, int[] arr, int i) { int n = arr.length; for (int j = i; j < n; j++) { arr[j] = in.readInt(); } } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m, s, f; cin >> n >> m >> s >> f; long long int i, j; long long int t[m], l[m], r[m]; long long int curr = s; long long int step = 0; for (i = 0; i < m; i++) { cin >> t[i] >> l[i] >> r[i]; } for (i = 0; i < m; i++) { if (curr == f) break; if (t[i] != step + 1) { if (s > f) { curr--; cout << 'L'; } else { curr++; cout << "R"; } i--; } else if (s > f) { if (curr < l[i] || curr - 1 > r[i]) { curr--; cout << 'L'; } else cout << 'X'; } else { if (curr + 1 < l[i] || curr > r[i]) { curr++; cout << "R"; } else cout << 'X'; } step++; } while (curr != f) { if (s > f) { curr--; cout << 'L'; } else { curr++; cout << "R"; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) { } new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); int t[] = new int[100005]; int l[] = new int[100005]; int r[] = new int[100005]; private void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n,m,s,f; n = nextInt(); m = nextInt(); s = nextInt(); f = nextInt(); for(int i = 0;i < m;i++){ t[i] = nextInt(); l[i] = nextInt(); r[i] = nextInt(); } int now = s; int k = 1; int c = 0; while(now!=f){ //System.err.println(k +" "+c); int fl = 0; if(s < f){ if(c<m){ if(t[c] == k){ if(((now >=l[c])&&(now <=r[c]))||((now+1 >=l[c])&&(now+1 <=r[c]))){ fl = 1; } c++; k++; if(fl == 0){ now++; out.print("R"); } else{ out.print("X"); } } else{ int mini = min(t[c]-k,f-now); for(int i =0;i < mini;i++){ out.print("R"); } now+=mini; k = t[c]; } } else{ for(int i =0;i < f-now;i++){ out.print("R"); } now=f; } } else{ if(c<m){ if(t[c] == k){ if(((now >=l[c])&&(now <=r[c]))||((now-1 >=l[c])&&(now-1 <=r[c]))){ fl = 1; } c++; k++; if(fl == 0){ now--; out.print("L"); } else{ out.print("X"); } } else{ int mini = min(t[c]-k,now-f); for(int i =0;i < mini;i++){ out.print("L"); } now-=mini ; k = t[c]; } } else{ for(int i =0;i < now-f;i++){ out.print("L"); } now=f; } } } in.close(); out.close(); } void chk(boolean b) { if (b) return; System.out.println(new Error().getStackTrace()[1]); exit(999); } void deb(String fmt, Object... args) { System.out.printf(Locale.US, fmt + "%n", args); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int step = 1; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; if (s == f) return false; while (step < t) { if (f > s) { s++; cout << "R"; } else if (f < s) { s--; cout << "L"; } else return false; step++; } if (f > s && (s + 1 > r || s + 1 < l) && (s > r || s < l)) { s++; cout << "R"; } else if (f < s && (s - 1 > r || s - 1 < l) && (s > r || s < l)) { s--; cout << "L"; } else if (s != f) cout << "X"; step++; } while (s != f) { if (f > s) { s++; cout << "R"; } else if (f < s) { s--; cout << "L"; } } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import sys spies, turns, note, final = map(int, input().split()) if note < final: next = 1 else: next = -1 answer = [] def pass_note(current_turn, next_watch, left, right, note): # print(f'\n{current_turn}), next watch: {next_watch}, left: {left}, right: {right} has note: {note}, goal: {final}') if current_turn == next_watch: # print('Watched this turn') if note in range(left, right + 1) or note + next in range(left, right + 1): # if note in range(left, right + 1): # print(f'Im being watched') # else: # print(f'Guy to pass it to is being watched') return note, 'X' # print('Not watched, passing note to ', end='') if note < final: note += next # print('right') return note, 'R' else: note += next # print('left') return note, 'L' current_turn = 0 for turn in range(turns): next_watch, left, right = map(int, input().split()) while current_turn < next_watch: current_turn += 1 note, ans = pass_note(current_turn, next_watch, left, right, note) answer.append(ans) if note == final: print(''.join(answer)) sys.exit() #print("ran out of turns") while note != final: # print(f'Note at: {note}, goal is {final}') current_turn += 1 note, ans = pass_note(current_turn, 0, -1, -1, note) answer.append(ans) print(''.join(answer))
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; scanf("%d", &n); ; scanf("%d", &m); ; scanf("%d", &s); ; scanf("%d", &f); ; vector<pair<int, pair<int, int> > > a(m); for (int i = 0; i < m; i++) { scanf("%d", &a[i].first); ; scanf("%d", &a[i].second.first); ; scanf("%d", &a[i].second.second); ; } char move; int dir; if (s < f) { move = 'R'; dir = 1; } else { move = 'L'; dir = -1; } string ss; int x = 1, ind = 0; while (1) { if (f == s) break; if (a[ind].first == x) { if ((s <= a[ind].second.second && s >= a[ind].second.first) || (s + dir >= a[ind].second.first && s + dir <= a[ind].second.second)) { ss += "X"; } else { ss += move; s += dir; } ind++; } else { ss += move; s += dir; } x++; } cout << ss << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long pos; long long n, m, s, f; cin >> n >> m >> s >> f; map<long long, pair<long long, long long> > mp; for (long long i = 0; i < m; i++) { int x, y, z; cin >> x >> y >> z; mp.insert(make_pair(x, make_pair(y, z))); } if (s < f) { pos = s; long long st = 1; map<long long, pair<long long, long long> >::iterator it; for (it = mp.begin(); it != mp.end(); ++it) { while (st < it->first) { st++; cout << "R"; pos++; if (pos == f) return 0; } if (it->second.first > pos + 1 || it->second.second < pos) { cout << "R"; pos++; } else cout << "X"; if (pos == f) break; st++; } } else if (s > f) { pos = s; long long st = 1; map<long long, pair<long long, long long> >::iterator it; for (it = mp.begin(); it != mp.end(); ++it) { while (st < it->first) { st++; cout << "L"; pos--; if (pos == f) return 0; } if (it->second.second < (pos - 1) || it->second.first > pos) { cout << "L"; pos--; } else cout << "X"; if (pos == f) break; st++; } } if (pos < f) { while (pos < f) { cout << "R"; pos++; } } else if (pos > f) { while (pos > f) { cout << "L"; pos--; } } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Dylan */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { static int n, m, s, f; public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); m = in.nextInt(); s = in.nextInt(); f = in.nextInt(); Limit[] limits = new Limit[m]; for (int i = 0; i < m; i++) { limits[i] = new Limit(in.nextInt(), in.nextInt(), in.nextInt()); } int delta = -1; char ch = 'L'; if (s < f) { delta = 1; ch = 'R'; } int p = 0; int time = 1; while (s != f) { if(p < m && time == limits[p].t) { if(check(s, limits[p]) && check(s + delta, limits[p])) { s += delta; out.print(ch); } else { out.print("X"); } p++; } else { s += delta; out.print(ch); } time++; } out.println(); } static boolean check(int x, Limit limit) { return x > limit.r || x < limit.l; } } class Limit { int t, l, r; Limit(int t, int l, int r) { this.t = t; this.l = l; this.r = r; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void print(char i) { writer.print(i); } public void println() { writer.println(); } public void close() { writer.close(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int s, f, n, m, l, r, dir, now, i, t, dif, k, h = 0, g; cin >> n >> m >> s >> f; now = s; if (f > s) dir = 1; else dir = -1; char j; if (dir == 1) j = 'R'; else j = 'L'; i = 1; while (now != f && h < m) { scanf("%d %d %d", &t, &l, &r); h++; if (i != t) { dif = t - i; k = now; now += dir * dif; if ((s < f && now >= f) or (s > f && now <= f)) while (k != f) { printf("%c", j); k += dir; } else for (k = 0; k < dif; k++) printf("%c", j); i = t; } if ((s < f && now >= f) or (s > f && now <= f)) break; if ((now + dir <= r && now + dir >= l) or (now <= r && now >= l)) printf("%c", 'X'); else { now += dir; printf("%c", j); } i++; } if ((s < f && now >= f) or (s > f && now <= f)) ; else while (now != f) { printf("%c", j); now += dir; } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.lang.Math; public class Account { public static void main (String[] args) { Scanner sc= new Scanner(System.in) ; int n= sc.nextInt() ; int m= sc.nextInt() ; int s= sc.nextInt() ; int f= sc.nextInt() ; int[] step= new int[m] ; int[] l= new int[m] ; int[] r= new int[m] ; for(int i=0 ;i<m ;i++ ) { step[i]= sc.nextInt() ; l[i]= sc.nextInt() ; r[i]= sc.nextInt() ; } int st= 1 ,i=0 ; if(s < f) { while(s != f) { if(i >= m) { System.out.print("R"); s++ ; // continue ; }else { if(st == step[i]) { if((l[i]<=s && s<=r[i]) || (l[i]<=(s+1) && (s+1)<=r[i])) { System.out.print("X"); }else { System.out.print("R"); s++ ; } i++ ; }else { System.out.print("R"); s++ ; } st++ ; } } }else { while(s != f) { if(i >= m) { System.out.print("L"); s-- ; }else { if(st == step[i]) { if((l[i]<=s && s<=r[i]) || (l[i]<=(s-1) && (s-1)<=r[i])) { System.out.print("X"); }else { System.out.print("L"); s-- ; } i++ ; }else { System.out.print("L"); s-- ; } st++ ; } } } } } //3 5 3 1 //1 1 2 //2 2 3 //3 3 3 //4 1 1 //10 1 3
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { long n, m, s, f, t, l, r, tAnt; long pos; char dirout; int dir; cin >> n >> m >> s >> f; pos = s; if (s > f) { dir = -1; dirout = 'L'; } else { dir = 1; dirout = 'R'; } tAnt = 0; for (int i = 0; i < m; i++) { cin >> t >> l >> r; for (int k = tAnt + 1; (k < t) && (f != pos); k++) { pos += dir; cout << dirout; } if (f == pos) return 0; if (((l > pos) || (r < pos)) && ((l > pos + dir) || (r < pos + dir))) { pos += dir; cout << dirout; } else { cout << 'X'; } if (f == pos) return 0; tAnt = t; } while (f != pos) { cout << dirout; pos += dir; } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class B { public static void main(String[] args) { B solver = new B(); solver.solve(); } private void solve() { Scanner sc = new Scanner(System.in); // sc = new Scanner("3 5 1 3\n" + // "1 1 2\n" + // "2 2 3\n" + // "3 3 3\n" + // "4 1 1\n" + // "10 1 3\n"); int n = sc.nextInt(); int m = sc.nextInt(); int s = sc.nextInt(); int f = sc.nextInt(); int[] ts = new int[m]; int[] ls = new int[m]; int[] rs = new int[m]; for (int i = 0; i < m; i++) { ts[i] = sc.nextInt(); ls[i] = sc.nextInt(); rs[i] = sc.nextInt(); } int d = s < f ? 1 : -1; int i = 0; int p0 = s; for (int t = 1;; t++) { if (p0 == f) break; if (i >= m || t < ts[i]) { System.out.print(d > 0 ? 'R' : 'L'); p0 += d; } else { if (i < m && ls[i] <= p0 && p0 <= rs[i]) System.out.print('X'); else if (i < m && ls[i] <= p0+d && p0+d <= rs[i]) System.out.print('X'); else { System.out.print(d > 0 ? 'R' : 'L'); p0 += d; } i++; } } System.out.println(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; void print_steps(int rtime, int steps, bool swap) { if (swap) { cout << string(steps, 'L'); cout << string(rtime - steps, 'X'); } else { cout << string(steps, 'R'); cout << string(rtime - steps, 'X'); } } int main() { int n, m, si, fi, s, f, t, ct = 0, bi, ei, b, e; bool swap_p = 0; cin >> n >> m >> si >> fi; if (si > fi) { swap_p = 1; s = n + 1 - si; f = n + 1 - fi; } else { s = si; f = fi; } int i; int available_steps = 0, delta_t; for (i = 0; i < m; i++) { cin >> t; delta_t = t - ct; if (delta_t > 1) { available_steps = min(t - ct - 1, f - s); s += available_steps; print_steps(available_steps, available_steps, swap_p); delta_t = 1; } if (s == f) { i++; break; } ct = t; cin >> bi >> ei; if (swap_p) { b = n + 1 - ei; e = n + 1 - bi; } else { b = bi; e = ei; } if (e < s) { available_steps = delta_t; s += available_steps; } else if (b > s) { available_steps = min(delta_t, b - s - 1); s += available_steps; } else { available_steps = 0; } print_steps(delta_t, available_steps, swap_p); if (s == f) { i++; break; } } if (s < f) print_steps(f - s, f - s, swap_p); while (i < m) { cin >> t; cin >> bi >> ei; i++; } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
if __name__=='__main__': inp = input() arr = inp.split(' ') n = int(arr[0]) m = int(arr[1]) s = int(arr[2]) f = int(arr[3]) ans = "" ch = 'L' inc = -1 if s<f: ch='R' inc = 1 tm = 1 done = False for i in range(m): inp = input() if done: continue arr = inp.split(' ') ct = int(arr[0]) l = int(arr[1]) r = int(arr[2]) while ct!=tm: ans+=ch tm+=1 s+=inc if s==f: break if s!=f and (((s+inc)>=l and (s+inc)<=r)or((s>=l)and(s<=r))): ans += 'X' elif s!=f: ans += ch s += inc tm+=1 if s==f: done = True while s!=f: s+=inc ans+=ch print(ans)
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> char str[10000001]; int k = 0; int main() { int n, m, s, f; scanf("%d%d%d%d", &n, &m, &s, &f); int ptr = s; int tmp = 1; for (int i = 0; i < m; i++) { int t, l, r; scanf("%d%d%d", &t, &l, &r); if (ptr == f) continue; while (tmp < t) { if (s < f) { ptr++; str[k] = 'R'; } else { ptr--; str[k] = 'L'; } k++; tmp++; if (ptr == f) break; } if (ptr == f) continue; if (s < f) { if ((ptr >= l && ptr <= r) || ptr + 1 == l) { str[k] = 'X'; k++; } else { ptr++; str[k] = 'R'; k++; } } else { if ((ptr >= l && ptr <= r) || ptr - 1 == r) { str[k] = 'X'; k++; } else { ptr--; str[k] = 'L'; k++; } } tmp = t + 1; } while (ptr != f) { if (ptr < f) { ptr++; str[k] = 'R'; k++; } else { ptr--; str[k] = 'L'; k++; } } printf("%s", str); return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> int main() { int q, w, e, r; int flag = 0; int a, b, sum, i, j; int ans = 0; int n, m, s, now; while (~scanf("%d%d%d%d", &n, &m, &s, &e)) { now = s; i = 1; while (m--) { scanf("%d%d%d", &q, &a, &b); while (i - 1 != q) { if (now == e) break; if (e > s) { if (a <= now + 1 && b >= now && i == q) printf("X"); else printf("R"), now++; } else { if (a <= now && b >= now - 1 && i == q) printf("X"); else printf("L"), now--; } i++; } } while (now != e) { if (e > s) { printf("R"), now++; } else { printf("L"), now--; } } puts(""); } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
I=lambda:map(int, raw_input().split()) n, m, s, f = I() d = {} for _ in xrange(m): t, l, r = I() d[t] = (l, r) W = 'R' if s < f else 'L' move = 1 if s < f else -1 i = 1 ans = '' while True: if s == f: break; if i in d: if (d[i][0] -1 <= s <= d[i][1] and W == 'R') or (d[i][0] <= s <= d[i][1] + 1 and W == 'L'): ans += 'X' else: ans += W s += move else: s += move ans += W i += 1 print ans
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; char ans[1000009]; int tot; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int t[100009], l[100009], r[100009]; for (int i = 1; i <= (m); i++) cin >> t[i] >> l[i] >> r[i]; tot = 0; int p = 1; while (1) { ++tot; if (t[p] == tot) { if (s >= l[p] && s <= r[p]) ans[tot] = 'X'; else { if (f > s) { if (s + 1 >= l[p] && s + 1 <= r[p]) ans[tot] = 'X'; else ans[tot] = 'R', s++; } else { if (s - 1 >= l[p] && s - 1 <= r[p]) ans[tot] = 'X'; else ans[tot] = 'L', s--; } } p++; } else { if (f > s) ans[tot] = 'R', s++; else ans[tot] = 'L', s--; } if (s == f) break; } for (int i = 1; i <= (tot); i++) cout << ans[i]; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; public class XeniaAndSpies { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), s = in.nextInt(), f = in .nextInt(); char move = (s < f) ? 'R' : 'L'; int start = s, it = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < m; i++) { if (start == f) break; int t = in.nextInt(), j = 0; long l = in.nextLong(), r = in.nextLong(); int next = ( (move == 'R') ? start + 1 : start - 1 ); boolean found = false; if (move == 'R') { for (j = it + 1; j < t; j++, start++, next++) { if (start == f) { System.out.println(ans); return; } ans.append("R"); } } else { for (j = it + 1; j < t; j++, start--, next--) { if (start == f) { System.out.println(ans); return; } ans.append("L"); } } if (start == f) break; if ((next < l && start < l) || (start > r && next > r)) found = true; if (!found) ans.append("X"); else { if (move == 'R') { ans.append("R"); start++; } else { ans.append("L"); start--; } } it = j; } int j = f - start; if (move == 'R') { for (int x = 0; x < j; x++) ans.append("R"); } else { j *= -1; for (int x = 0; x < j; x++) ans.append("L"); } System.out.println(ans); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int t[100010], l[100010], r[100010]; int main() { ios::sync_with_stdio(false); cin.tie(0); int i, n, m, s, f, j; cin >> n >> m >> s >> f; for ((i) = 0; (i) < (int)(m); (i)++) { cin >> t[i] >> l[i] >> r[i]; } i = 0; for (j = 1;; j++) { if (s == f) { cout << '\n'; return 0; } else if (s < f) { if (i < m && j == t[i]) { if (s + 1 < l[i] || s > r[i]) { s++; cout << "R"; } else { cout << "X"; } i++; } else { s++; cout << "R"; } } else { if (i < m && j == t[i]) { if (s < l[i] || s - 1 > r[i]) { s--; cout << "L"; } else { cout << "X"; } i++; } else { s--; cout << "L"; } } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f, t[110010], l[110010], r[110010], nxt; char c; int main() { cin >> n >> m >> s >> f; for (int i = 1; i <= m; i++) cin >> t[i] >> l[i] >> r[i]; t[m + 1] = 2 * (int)1e9; if (f < s) c = 'L'; else c = 'R'; int add = f < s ? -1 : 1; for (int i = 1; i <= m + 1; i++) { for (int j = 0; j < t[i] - t[i - 1] - 1 && s != f; j++) { cout << c; s += add; } if (s == f) break; nxt = s + add; if ((s >= l[i] && s <= r[i]) || (nxt >= l[i] && nxt <= r[i])) cout << "X"; else { cout << c; s += add; } if (f == s) break; } cout << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f = map(int,raw_input().split()) flag = 1 if s > f : flag = -1 res = "" pre_t = 0 for i in range(m) : t,l,r = map(int,raw_input().split()) if s == f: break if t != (pre_t + 1) : num = 0 if ( t - pre_t - 1) > abs(f-s): num = abs(f-s) else : num = (t-pre_t-1) if flag == 1: res += "R" * num else : res += "L" * num s = s + flag * num if s == f: break pre_t = t n_s = s+flag if (s <= r and s >= l) or (n_s <= r and n_s >= l) : res += "X" else : s = n_s if flag == 1: res += "R" else : res += "L" if s != f: if flag == 1: res += "R" * abs(f-s) else : res += "L" * abs(f-s) print res
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.TreeMap; public class Main{ static StringBuilder c; static class pair{ int form,to; public pair(int from,int to) { this.form=from;this.to=to; } } public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int steps=sc.nextInt(); int s=sc.nextInt(); int f=sc.nextInt(); TreeMap<Integer, pair> st=new TreeMap<Integer,pair>(); c=new StringBuilder(""); int stnow=1; for (int i = 0; i < steps; i++) { st.put(sc.nextInt(), new pair(sc.nextInt(),sc.nextInt())); } while(s!=f){ if(st.containsKey(stnow)){ pair s1=st.get(stnow); if(s>=s1.form&&s<=s1.to){ c.append("X"); }else{ s=trans(s,s1,f); } }else{ s=trans(s, new pair(-1,-1), f); } stnow++; } System.out.println(c); } public static boolean in(pair s1,int x){ if(x>=s1.form&&x<=s1.to){ return true; } return false; } private static int trans(int s, pair s1, int f) { if(f<s){ if(in(s1,s-1)){ c.append("X"); return s; }else {c.append("L"); return s-1; } } else{ if(in(s1,s+1)){ c.append("X"); return s; }else {c.append("R"); return s+1; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, s, f; cin >> n >> m >> s >> f; map<int, pair<int, int> > mop; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; mop[t] = {l, r}; } string ans; while (s != f) { pair<int, int> p = mop[ans.size() + 1]; if (s < f) { if ((s >= p.first && s <= p.second) || (s + 1 >= p.first && s + 1 <= p.second)) ans += 'X'; else ans += 'R', s++; } else { if ((s >= p.first && s <= p.second) || (s - 1 >= p.first && s - 1 <= p.second)) ans += 'X'; else ans += 'L', s--; } } cout << ans << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; map<int, pair<int, int> > w; int main() { int n, m, s, f, temp; cin >> n >> m >> s >> f; for (int i = 0; i < m; i++) { cin >> temp; cin >> w[temp].first >> w[temp].second; } int pasos = 1, pos = s; while (pos != f) { if (s < f) { while (w[pasos].first <= pos + 1 && w[pasos].second >= pos) { pasos++; cout << 'X'; } cout << 'R'; pos++; } else { while (w[pasos].first <= pos && w[pasos].second >= pos - 1) { pasos++; cout << 'X'; } cout << 'L'; pos--; } pasos++; } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; public class XeniaAndSpies { public static void main(String[] args) { MyScanner sc = new MyScanner(); sc.nextInt(); int M = sc.nextInt(); int S = sc.nextInt(); int F = sc.nextInt(); int[] L = new int[200002]; int[] R = new int[200002]; for (int i = 0; i < M; i++) { int t = sc.nextInt(); int left = sc.nextInt(); int rite = sc.nextInt(); if (t < 200002) { L[t] = left; R[t] = rite; } } StringBuilder sb = new StringBuilder(); int curr = S; for (int i = 1; i < 200002 && curr != F; i++) { if (S > F) { if (!inRange(curr, L[i], R[i]) && !inRange(curr - 1, L[i], R[i])) { curr--; sb.append("L"); } else { sb.append("X"); } } else { if (!inRange(curr, L[i], R[i]) && !inRange(curr + 1, L[i], R[i])) { curr++; sb.append("R"); } else { sb.append("X"); } } } System.out.println(sb.toString()); } public static boolean inRange(int x, int left, int rite) { return (x >= left && x <= rite); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.HashMap; import java.util.Scanner; public class Problem1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n, m, s, f; n = scanner.nextInt(); m = scanner.nextInt(); s = scanner.nextInt(); f = scanner.nextInt(); StringBuilder ans = new StringBuilder(); HashMap<Integer, Integer> start = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> end = new HashMap<Integer, Integer>(); for (int i = 0; i < m; i++) { int a, b, c; a = scanner.nextInt(); b = scanner.nextInt(); c = scanner.nextInt(); start.put(a, b); end.put(a, c); } int counter = s; int term = 1; while (true) { if (counter == f) { System.out.println(ans.toString()); break; } if (!start.containsKey(term)) { if (f > s) { ans.append('R'); counter += 1; } else { ans.append('L'); counter -= 1; } } else { int a = start.get(term); int b = end.get(term); if (f > s) { if (counter >= a && counter <= b) { ans.append('X'); } else if (counter + 1 == a) { ans.append('X'); } else { ans.append('R'); counter += 1; } } else { if (counter >= a && counter <= b) { ans.append('X'); } else if (counter - 1 == b) { ans.append('X'); } else { ans.append('L'); counter -= 1; } } } term++; } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int inf = 0x7FFFFFFF; const int maxn = 1000000; string ans = ""; int n, m, s, f; int t, l, r, w = 1; bool ok = false; void work() { if (s == f) { ok = true; return; } if (s < f) ++s, ans += 'R'; else --s, ans += 'L'; } int main() { cin >> n >> m >> s >> f; while (m--) { cin >> t >> l >> r; if (ok) continue; while (w < t && !ok) work(), ++w; if (ok) continue; if (s < f) { if ((s + 1 >= l && s + 1 <= r) || (s >= l && s <= r)) ans += 'X'; else ++s, ans += 'R'; } else if (s > f) { if ((s - 1 >= l && s - 1 <= r) || (s >= l && s <= r)) ans += 'X'; else --s, ans += 'L'; } ++w; } if (s <= f) ans += string(f - s, 'R'); else ans += string(s - f, 'L'); cout << ans << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
//package codeforces.train; import java.io.*; import java.math.BigInteger; import java.util.*; public class ProblemB { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out)); long[] ttl = null; int txl = 0; long readLong() throws IOException { if(ttl == null || txl >= ttl.length) { ttl = readLongs(); txl = 0; } return ttl[txl++]; } long[] readLongs() throws IOException { String[] strings = reader.readLine().split(" "); long[] longs = new long[strings.length]; for(int i = 0; i < longs.length; i++) { longs[i] = Long.parseLong(strings[i]); } return longs; } int[] readInts() throws IOException { String[] strings = reader.readLine().split(" "); int[] ints = new int[strings.length]; for(int i = 0; i < ints.length; i++) { ints[i] = Integer.parseInt(strings[i]); } return ints; } int[] tt = null; int tx = 0; int readInt() throws IOException { if(tt == null || tx >= tt.length) { tt = readInts(); tx = 0; } return tt[tx++]; } void solve() throws IOException { int n = readInt(); int m = readInt(); int s = readInt(); int f = readInt(); int[] t = new int[m]; int[] l = new int[m]; int[] r = new int[m]; for(int i = 0; i < m; i++) { t[i] = readInt(); l[i] = readInt(); r[i] = readInt(); } int d = -(s - f) / Math.abs(s - f); char c = "LR".toCharArray()[d + 1 >> 1]; StringBuffer ans = new StringBuffer(); int j = 0; for(int i = 1; s != f; i++) { if(j < m && i == t[j]) { if(s >= l[j] && s <= r[j] || s + d >= l[j] && s + d <= r[j]) { ans.append('X'); } else { ans.append(c); s += d; } j++; } else { ans.append(c); s += d; } } writer.println(ans); writer.flush(); } void multiSolve() throws IOException { int n = readInts()[0]; for(int i = 0; i < n; i++) { if(i > 0) writer.println(); solve(); } } public static void main(String[] args) throws IOException{ new ProblemB().solve(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f=map(int,input().split()) p=s d=-1 c='L' if s<f: d=1 c='R' t=1 ts={} ans="" for _ in range(m): x,y,z=map(int,input().split()) ts[x]=(y,z) while(p!=f): if t in ts: (l,r)=ts[t] if l<=p<=r or l<=p+d<=r: ans+='X' else: p+=d ans+=c else: p+=d ans+=c t+=1 print(ans)
PYTHON3
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.HashMap; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int s = in.nextInt(); int f = in.nextInt(); HashMap<Integer, Pair> watches = new HashMap<Integer, Pair>(); for (int i = 0; i < m; i++) { watches.put(in.nextInt(), new Pair(in.nextInt(), in.nextInt())); } int current = s; StringBuilder res = new StringBuilder(); int direction = s < f ? 1 : -1; char letter = direction > 0 ? 'R' : 'L'; for (int step = 1; current != f; step++) { if (!watches.containsKey(step)) { res.append(letter); current += direction; } else { if ((watches.get(step).left <= current && current <= watches.get(step).right) || current + direction == watches.get(step).left || current + direction == watches.get(step).right) { res.append('X'); } else { res.append(letter); current += direction; } } } out.println(res); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class Pair { int left; int right; public Pair(int left, int right) { this.left = left; this.right = right; } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.Scanner; import java.io.StreamTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintStream; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author KNIGHT0X300 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputStreamReader in = new InputStreamReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { StreamTokenizer in; PrintWriter out; BufferedReader re; Scanner sc; public void solve(int testNumber, InputStreamReader in, PrintWriter out) { this.in = new StreamTokenizer(new BufferedReader(in)); this.re = new BufferedReader(in); this.sc = new Scanner(in); this.out = out; try { this.solve(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } out.flush(); } ///////////////////////////////////////////////////////////// int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } void solve() throws IOException { int n = nextInt(); int q = nextInt(); int s = nextInt(), f = nextInt(); int dir = 1; char c = 'R'; if (s > f) { dir = -1; c = 'L'; } int cur = s; StringBuilder ans = new StringBuilder(); int time = 0; for (int i = 0; i < q; i++) { if (cur == f) { out.println(ans); return; } int tt = nextInt(); for (; time < tt-1; time++) { ans.append(c); cur += dir; if (cur == f) { out.println(ans); return; } } int ll=nextInt(),rr=nextInt(); if(dir==1){ if(cur>=ll-1&&cur<=rr){ ans.append('X'); } else{ ans.append(c); cur+=dir; } } else{ if(cur>=ll&&cur<=rr+1){ ans.append('X'); } else{ ans.append(c); cur+=dir; } } time=tt; } while(cur!=f){ cur+=dir; ans.append(c); } out.println(ans); // do the sum } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(),m = sc.nextInt(),st = sc.nextInt(),en = sc.nextInt(); StringBuilder ans = new StringBuilder(); int tt=0; while(m-- != 0) { int t = sc.nextInt(),l = sc.nextInt(),r = sc.nextInt(); if(t!=tt+1) { while(t!=tt+1) { tt++; if(en>st) { out.print("R"); st++; } else if(st>en) { out.print("L"); st--; } } } tt++; if(st==en) { continue; } else if(st>en) { if(st<l || st-1>r) { out.print("L"); st--; } else { out.print("X"); } } else { if(st>r || st+1<l) { out.print("R"); st++; } else{ out.print("X"); } } } while(st!=en) { if(st>en) { en++; out.print("L"); } else { st++; out.print("R"); } } out.close(); } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> #pragma comment(linker, "/stack:225450978") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18; const long double Pi = 3.141592653589793116, EPS = 1e-9, Gold = ((1 + sqrt(5)) / 2); long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL}; long long keyCount = sizeof(keymod) / sizeof(long long); template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcountll(s); } auto TimeStart = chrono::steady_clock::now(); auto TimeEnd = chrono::steady_clock::now(); void ControlIO(int argc, char* argv[]); void TimerStart(); void TimerStop(); void Exit(); long long n, m, s, f, t, l, r; map<long long, pair<long long, long long> > Map; void Input() { cin >> n >> m >> s >> f; while (m--) { cin >> t >> l >> r; Map[t] = make_pair(l, r); } } void Solve() { long long turn = 1; while (s != f) { if (Map.find(turn) == Map.end()) { if (s < f) { cout << "R"; s++; } else { cout << "L"; s--; } turn++; continue; } if (Map[turn].first <= s && s <= Map[turn].second || ((s > f && Map[turn].first <= s - 1 && s - 1 <= Map[turn].second) || (s < f && Map[turn].first <= s + 1 && s + 1 <= Map[turn].second))) cout << "X"; else if (s < f) { cout << "R"; s++; } else { cout << "L"; s--; } turn++; } } int main(int argc, char* argv[]) { ControlIO(argc, argv); ios_base::sync_with_stdio(0); cin.tie(NULL); Input(); TimerStart(); Solve(); TimerStop(); return 0; } void ControlIO(int argc, char* argv[]) {} void TimerStart() {} void TimerStop() {} void Exit() { TimerStop(); exit(0); }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import sys N, M, S, F = map(int, raw_input().split()) T = [] for i in xrange(M): T.append(map(int, raw_input().split())) T.append((0, 0, 0)) c = 1 p = 0 while S!=F: NS = S if S<F: NS = S+1 else: NS = S-1 if c==T[p][0] and (T[p][1]<=S<=T[p][2] or T[p][1]<=NS<=T[p][2]): sys.stdout.write('X') else: if S<F: sys.stdout.write('R') S += 1 else: sys.stdout.write('L') S -= 1 if c==T[p][0]: p += 1 c += 1 print
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public void solve() throws Exception { int n = nextInt(); int m = nextInt(); int s = nextInt(); int f = nextInt(); int t = nextInt(); int l = nextInt(); int r = nextInt(); int time = 1; for(int i = 0; i < m; i++) { if(time == t) { i--; time++; if(s <= r && s >= l) { out.print("X"); continue; } if(s < f) { if(s+1 <= r && s+1 >= l) { out.print("X"); continue; } } if(s > f) { if(s-1 <= r && s-1 >= l){ out.print("X"); continue; } } if(s > f) { out.print("L"); s--; } if(s < f) { out.print("R"); s++; } if(s == f) return; continue; } if(time < t) { i--; time++; if(f > s) { out.print("R"); s++; } else { out.print("L"); s--; } if(s == f) return; continue; } if(time > t && i != m-1) { t = nextInt(); l = nextInt(); r = nextInt(); continue; } } while(s != f) { if(s < f) { s++; out.print("R"); } else { s--; out.print("L"); } } } public static void main(String[] args) throws Exception { A problem = new A(); problem.solve(); problem.close(); } BufferedReader in; PrintWriter out; String curLine; StringTokenizer tok; final String delimeter = " "; final String endOfFile = ""; public A(BufferedReader in, PrintWriter out) throws Exception { this.in = in; this.out = out; curLine = in.readLine(); if (curLine == null || curLine == endOfFile) { tok = null; } else { tok = new StringTokenizer(curLine, delimeter); } } public A() throws Exception { this(new BufferedReader(new InputStreamReader(System.in)), new PrintWriter(System.out)); } public A(String filename) throws Exception { this(new BufferedReader(new FileReader(filename + ".in")), new PrintWriter(filename + ".out")); } public boolean hasMore() throws Exception { if (tok == null || curLine == null) { return false; } else { while (!tok.hasMoreTokens()) { curLine = in.readLine(); if (curLine == null || curLine.equalsIgnoreCase(endOfFile)) { tok = null; return false; } else { tok = new StringTokenizer(curLine); } } return true; } } public String nextWord() throws Exception { if (!hasMore()) { return null; } else { return tok.nextToken(); } } public int nextInt() throws Exception { return Integer.parseInt(nextWord()); } public double nextDouble() throws Exception { return Double.parseDouble(nextWord()); } public long nextLong() throws Exception { return Long.parseLong(nextWord()); } public int[] readIntArray(int n) throws Exception { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public void close() throws Exception { in.close(); out.close(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import javax.print.DocFlavor; import javax.swing.plaf.basic.BasicInternalFrameTitlePane; import java.io.*; import java.math.BigInteger; import java.nio.Buffer; import java.sql.BatchUpdateException; import java.util.*; import java.util.stream.Stream; import java.util.Vector; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import static java.lang.Math.*; import java.util.*; import java.nio.file.StandardOpenOption; public class icpc { public static void main(String[] args)throws IOException { // Reader in = new Reader(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s2[] = in.readLine().split(" "); int n = Integer.parseInt(s2[0]); int m = Integer.parseInt(s2[1]); int s = Integer.parseInt(s2[2]); int f = Integer.parseInt(s2[3]); StringBuilder stringBuilder = new StringBuilder(); long current = 0L; for(int i=1;i<=m;i++) { String s1[] = in.readLine().split(" "); long t = Long.parseLong(s1[0]); long l = Long.parseLong(s1[1]); long r = Long.parseLong(s1[2]); long diff = t - current - 1; if(diff > 0 && s != f) { if(s < f) { int temp = s; for(int j=s;j<Math.min(temp+diff,f);j++) { stringBuilder.append("R"); s++; } } else { int temp = s; for(int j=s;j>Math.max(temp-diff,f);j--) { stringBuilder.append("L"); s--; } } } if(s == f) break; else if(s < f && (r < s || l > (s + 1))) { s++; stringBuilder.append("R"); } else if(s > f && (l > s || r < (s - 1))) { s--; stringBuilder.append("L"); } else { stringBuilder.append("X"); } current = t; } if(s != f) { if(s < f) { for(int i=s;i<f;i++) stringBuilder.append("R"); } else { for(int i=s;i>f;i--) stringBuilder.append("L"); } } System.out.println(stringBuilder); } } class Solver { public void solve(long[] A,Game[] B) { } } class Point implements Comparable<Point> { int x; int y; Point(int a ,int b) { this.x = a; this.y = b; } public int compareTo(Point ob) { if(ob.x < this.x) return 1; else if(ob.x > this.x) return -1; else return 0; } } class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } } class Node { int[] A; int moves; Node(int[] B,int c) { this.A = new int[B.length]; A = B.clone(); this.moves = c; } @Override public boolean equals(Object ob) { if(ob == null) return false; if(!(ob instanceof Node)) return false; if(ob == this) return true; Node obj = (Node)ob; boolean flag = true; for(int i=0;i<obj.A.length;i++) { if(this.A[i] != obj.A[i]) flag = false; } return (flag & (obj.moves == this.moves)); } @Override public int hashCode() { return (int)this.A.length; } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } class Game { int last; int diff; boolean flag; Game(int a,int b,boolean z) { this.last = a; this.diff = b; this.flag = z; } } class Play { int ta; int tb; int len; Play(int a,int b,int c) { this.ta = a; this.tb = b; this.len = c; } } class SegmentTree { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public Play[] createSegmentTree(char[] input) { int np2 = nextPowerOfTwo(input.length); Play[] segmentTree = new Play[np2* 2 - 1]; for(int i=0;i<segmentTree.length;i++) segmentTree[i] = new Play(0,0,0); constructSegmentTree(segmentTree,input,0,input.length-1,0); return segmentTree; } public void constructSegmentTree(Play[] segmentTree,char[] input,int low,int high,int pos) { if(low == high) { segmentTree[pos] = new Play((input[low] == '(')?1:0,(input[low] == ')')?1:0,0); return; } int mid = (low + high)/2; constructSegmentTree(segmentTree,input,low,mid,2*pos + 1); constructSegmentTree(segmentTree,input,mid + 1,high,2*pos + 2); int t = Math.min(segmentTree[2*pos + 1].ta, segmentTree[2*pos + 2].tb); int len = segmentTree[2*pos + 1].len + segmentTree[2*pos + 2].len + 2*t; int ta = segmentTree[2*pos + 1].ta + segmentTree[2*pos + 2].ta - t; int tb = segmentTree[2*pos + 1].tb + segmentTree[2*pos + 2].tb - t; segmentTree[pos] = new Play(ta,tb,len); } public Play rangeMinimumQuery(Play[] segmentTree,int qlow,int qhigh,int len) { return rangeMinimumQuery(segmentTree,qlow,qhigh,0,len - 1,0); } private Play rangeMinimumQuery(Play[] segmentTree,int qlow,int qhigh,int low,int high,int pos) { if(qlow <= low && qhigh >= high) { return segmentTree[pos]; } if(qlow > high || qhigh < low) { return new Play(0,0,0); } int mid = (low + high)/2; Play a = rangeMinimumQuery(segmentTree,qlow,qhigh,low,mid,2*pos + 1); Play b = rangeMinimumQuery(segmentTree,qlow,qhigh,mid + 1,high,2*pos + 2); int t = Math.min(a.ta, b.tb); int len = a.len + b.len + 2*t; int ta = a.ta + b.ta - t; int tb = a.tb + b.tb - t; return new Play(ta,tb,len); } } class SegmentTree1 { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public int[] createSegmentTree(int[] input) { int np2 = nextPowerOfTwo(input.length); int[] segmentTree = new int[np2 * 2 - 1]; for(int i=0;i<segmentTree.length;i++) segmentTree[i] = Integer.MAX_VALUE; constructSegmentTree(segmentTree,input,0,input.length-1,0); return segmentTree; } private void constructSegmentTree(int[] segmentTree,int[] input,int low,int high,int pos) { if(low == high) { segmentTree[pos] = input[low]; return; } int mid = (low + high)/ 2; constructSegmentTree(segmentTree,input,low,mid,2*pos + 1); constructSegmentTree(segmentTree,input,mid+1,high,2*pos + 2); segmentTree[pos] = Math.min(segmentTree[2*pos + 1],segmentTree[2*pos + 2]); } public int rangeMinimumQuery(int[] segmentTree,int qlow,int qhigh,int len) { return rangeMinimumQuery(segmentTree,0,len - 1,qlow,qhigh,0); } private int rangeMinimumQuery(int[] segmentTree,int low,int high,int qlow,int qhigh,int pos) { if(qlow <= low && qhigh >= high) { return segmentTree[pos]; } else if(qlow > high || qhigh < low) { return Integer.MAX_VALUE; } int mid = (low + high)/2; return Math.min(rangeMinimumQuery(segmentTree,low,mid,qlow,qhigh,2*pos+1),rangeMinimumQuery(segmentTree,mid+1,high,qlow,qhigh,2*pos+2)); } public void updateSegmentTreeRangeLazy(int[] input,int[] segmentTree,int[] lazy,int startRange,int endRange,int delta) { updateSegmentTreeRangeLazy(input,segmentTree,lazy,startRange,endRange,delta,0,input.length-1,0); } private void updateSegmentTreeRangeLazy(int[] input,int[] segmentTree,int[] lazy,int startRange,int endRange,int delta,int low,int high,int pos) { if(low > high) { return; } if(lazy[pos] != 0) { segmentTree[pos] += lazy[pos]; if(low != high) { lazy[2*pos+1] += lazy[pos]; lazy[2*pos + 2] += lazy[pos]; } lazy[pos] = 0; } if(startRange > high || endRange < low) return; if(startRange <= low && endRange >= high) { segmentTree[pos] += delta; if(low != high) { lazy[2*pos + 1] += delta; lazy[2*pos + 2] += delta; } return; } int mid = (low + high)/2; updateSegmentTreeRangeLazy(input,segmentTree,lazy,startRange,endRange,delta,low,mid,2*pos + 1); updateSegmentTreeRangeLazy(input,segmentTree,lazy,startRange,endRange,delta,mid + 1,high,2 * pos + 2); segmentTree[pos] += Math.min(segmentTree[2*pos + 1],segmentTree[2*pos + 2]); } public int rangeMinimumQueryLazy(int[] segmentTree,int[] lazy,int qlow,int qhigh,int len) { return rangeMinimumQueryLazy(segmentTree,lazy,qlow,qhigh,0,len - 1,0); } private int rangeMinimumQueryLazy(int[] segmentTree,int[] lazy,int qlow,int qhigh,int low,int high,int pos) { if(low > high) return Integer.MAX_VALUE; if(lazy[pos] != 0) { segmentTree[pos] += lazy[pos]; if(low != high) { lazy[2*pos + 1] += lazy[pos]; lazy[2*pos + 2] += lazy[pos]; } lazy[pos] = 0; } if(qlow > high || qhigh < low) return Integer.MAX_VALUE; if(qlow <= low && qhigh >= low) return segmentTree[pos]; int mid = (low + high)/2; return Math.min(rangeMinimumQueryLazy(segmentTree,lazy,qlow,qhigh,low,mid,2*pos + 1),rangeMinimumQueryLazy(segmentTree,lazy,qlow,qhigh,mid + 1,high,2*pos + 2)); } } class FenwickTree { public void update(int[] fenwickTree,int delta,int index) { index += 1; while(index < fenwickTree.length) { fenwickTree[index] += delta; index = index + (index & (-index)); } } public int prefixSum(int[] fenwickTree,int index) { int sum = 0; index += 1; while(index > 0) { sum += fenwickTree[index]; index -= (index & (-index)); } return sum; } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, m, s, f, t, l, r; cin >> n >> m >> s >> f; long long t2 = 0; for (long long i = 1; i <= m; i++) { if (s == f) return 0; cin >> t >> l >> r; if (t2 + 1 < t) { for (long long i = t2 + 1; i < t; i++) { if (s == f) return 0; if (s < f) { cout << "R"; s++; } else { cout << "L"; s--; } } } t2 = t; if (s == f) return 0; if (s >= l && s <= r) { cout << "X"; continue; } else { if (s < f) { if (s + 1 == l) { cout << "X"; continue; } cout << "R"; s++; } else { if (s - 1 == r) { cout << "X"; continue; } s--; cout << "L"; } } } if (s < f) { while (s < f) { cout << "R"; s++; } } else { while (s > f) { cout << "L"; s--; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Solution sol = new Solution(); sol.run(); } void out(String ans) { System.out.println(ans); System.exit(0); } void run() throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer stok = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(stok.nextToken()); int m = Integer.parseInt(stok.nextToken()); int s = Integer.parseInt(stok.nextToken()) - 1; int f = Integer.parseInt(stok.nextToken()) - 1; int dir = (f - s) / Math.abs(f - s); char move = dir > 0 ? 'R' : 'L'; char stay = 'X'; StringBuilder ans = new StringBuilder(); int cur = 1; for (int i = 0; i <= m; ++i) { int t, lf, rg; if (i < m) { stok = new StringTokenizer(reader.readLine()); t = Integer.parseInt(stok.nextToken()); lf = Integer.parseInt(stok.nextToken()) - 1; rg = Integer.parseInt(stok.nextToken()) - 1; } else { t = Integer.MAX_VALUE; lf = rg = -1; } while (cur < t) { ans.append(move); s += dir; if (s == f) out(ans.toString()); ++cur; } int next = s + dir; if ((lf <= s && s <= rg) || (lf <= next && next <= rg)) { ans.append(stay); } else { ans.append(move); s = next; if (s == f) out(ans.toString()); } ++cur; } out(ans.toString()); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; int t, l, r, backT = 0; cin >> n >> m >> s >> f; int cur = s; for (int i = 0; i < m; i++) { cin >> t >> l >> r; for (int j = t - backT; j > 0; j--) { if (cur < f && (j > 1 || (r < cur || cur + 1 < l))) { cur++; cout << "R"; } else if (cur > f && (j > 1 || (r < cur - 1 || cur < l))) { cur--; cout << "L"; } else cout << "X"; if (cur == f) return 0; } backT = t; } while (cur != f) { if (cur < f) { cur++; cout << "R"; } else if (cur > f) { cur--; cout << "L"; } } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f, t, l, r, t1, px; char x; int main() { cin >> n >> m >> s >> f; if (f < s) x = 'L', px = -1; else x = 'R', px = 1; for (int i = 1; i <= m; i++) { if (s == f) break; cin >> t >> l >> r; t1 = t - t1 - 1; for (int j = 1; j <= t1; j++) { cout << x; s += px; if (s == f) break; } if (s == f) break; t1 = t; if ((s >= l && s <= r) || (s + px >= l && s + px <= r)) { cout << 'X'; continue; } else cout << x, s += px; } for (int j = 1; j <= n; j++) { if (s == f) break; cout << x; s += px; } return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f, l2 = 1; string sj = ""; int t1 = 0, t2 = 0, l, r; bool ch = true; cin >> n >> m >> s >> f; if (s > f) ch = false; int e = 0; for (int i = 0; i < m; i++) { t1 = t2; cin >> t2 >> l >> r; e = t2 - t1 - 1; while (e && s != f) { if (ch) { sj += 'R'; s++; } else { sj += 'L'; s--; } e--; } if (!ch && s != f) { if (r >= s - 1 && l <= s - 1 || r >= s && l <= s) sj += 'X'; else { sj += 'L'; s--; } } else if (s != f) { if (r >= s + 1 && l <= s + 1 || r >= s && l <= s) sj += 'X'; else { sj += 'R'; s++; } } } while (s != f) if (ch) { sj += 'R'; s++; } else { sj += 'L'; s--; } cout << sj << endl; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class Main{ public static void main(String []args){ Scanner in=new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); int s=in.nextInt(); int f=in.nextInt(); int[] t=new int[100000],l=new int[100000],r=new int[100000]; for(int i=0;i<m;i++){ t[i]=in.nextInt(); l[i]=in.nextInt(); r[i]=in.nextInt(); } int now=1,tail=0; while(s!=f){ if(now>t[m-1]){ while(s!=f){ if(s<f) { System.out.print("R"); s++; } else { System.out.print("L"); s--; } } break; } while(now<t[tail]){ if(s==f) break; else { if(s<f) { System.out.print("R"); s++; } else { System.out.print("L"); s--; } now++; } } if(s<f){ if((s<=r[tail]&&s>=l[tail])||(s+1<=r[tail]&&s+1>=l[tail])){ System.out.print("X"); } else { s++; System.out.print("R"); } } else if(s>f){ if((s<=r[tail]&&s>=l[tail])||(s-1<=r[tail]&&s-1>=l[tail])){ System.out.print("X"); } else { s--; System.out.print("L"); } } else break; tail++; now++; } } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
n,m,s,f=map(int,raw_input().split()) ar=[] for i in range(m): a,b,c=map(int,raw_input().split()) ar.append([a,b,c]) ans="" curpos=s if s<f: i=0 tm=1 while 1: if curpos==f: break elif i>=m: ans+="R" curpos+=1 tm+=1 elif tm!=ar[i][0]: ans+="R" curpos+=1 tm+=1 elif (curpos<=ar[i][2] and curpos>=ar[i][1]) or (curpos+1<=ar[i][2] and curpos+1>=ar[i][1]): ans+="X" i+=1 tm+=1 else: ans+="R" curpos+=1 i+=1 tm+=1 elif s>f: i=0 tm=1 while 1: if curpos==f: break elif i>=m: ans+="L" curpos-=1 tm+=1 elif tm!=ar[i][0]: ans+="L" curpos-=1 tm+=1 elif (curpos<=ar[i][2] and curpos>=ar[i][1]) or (curpos-1<=ar[i][2] and curpos-1>=ar[i][1]): ans+="X" i+=1 tm+=1 else: ans+="L" curpos-=1 i+=1 tm+=1 print ans
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; public class XeniaAndSpies { public static void main(String[] args) throws Exception { BufferedReader k = new BufferedReader(new InputStreamReader(System.in)); // Scanner k = new Scanner(System.in); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); StringTokenizer s = new StringTokenizer(k.readLine()); int n = Integer.parseInt(s.nextToken()); int m = Integer.parseInt(s.nextToken()); int st = Integer.parseInt(s.nextToken())-1; int end = Integer.parseInt(s.nextToken())-1; int arr[][] = new int[m][3]; for (int i = 0; i < m; i++) { s = new StringTokenizer(k.readLine()); arr[i][0] = Integer.parseInt(s.nextToken()); arr[i][1] = Integer.parseInt(s.nextToken())-1; arr[i][2] = Integer.parseInt(s.nextToken())-1; } StringBuilder res = new StringBuilder(); if (st > end) { int g = 0, count; for (int i = 0; i < m; i++) { count = arr[i][0] - g; while (count-- > 1) { if(st==end) break; st--;res.append("L"); } if(st==end) break; if (((arr[i][1] <= st && arr[i][2] >= st) || (arr[i][1] <= st - 1 && arr[i][2] >= st - 1)) ){ res.append("X"); }else {st--;res.append("L");} g = arr[i][0]; } while(st!=end){ res.append("L"); st--; } } else { int g = 0, count; for (int i = 0; i < m; i++) { count = arr[i][0] - g; while (count-- > 1) { if(st==end) break; st++;res.append("R"); } if(st==end) break; if (((arr[i][1] <= st && arr[i][2] >= st) || (arr[i][1] <= st + 1 && arr[i][2] >= st + 1)) ){ res.append("X"); }else {st++;res.append("R");} g = arr[i][0]; } while(st!=end) { res.append("R");st++; } } out.println(res.toString()); out.close(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; static inline bool between(int x, int l, int r) { return x >= l && x <= r; } int main() { string actions; int n, m, s, f; cin >> n >> m >> s >> f; map<int, pair<int, int> > stages; for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; stages[t] = make_pair(l, r); } int dx = f - s > 0 ? 1 : -1; int curr = s, stage = 0; while (curr != f) { stage++; int l = n + 1, r = -1; if (stages.count(stage)) l = stages[stage].first, r = stages[stage].second; if (between(curr, l, r) || between(curr + dx, l, r)) { actions.push_back('X'); continue; } curr += dx; actions.push_back(dx > 0 ? 'R' : 'L'); } cout << actions; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int inf = INT_MAX / 2; int n, m, s, f; struct node { int l, r, t; }; node a[maxn]; bool cmp(node a, node b) { return a.t < b.t; } int main() { while (scanf("%d%d%d%d", &n, &m, &s, &f) != EOF) { for (int i = 0; i < m; i++) { scanf("%d%d%d", &a[i].t, &a[i].l, &a[i].r); } sort(a, a + m, cmp); int k = 0; int nowt = 1; while (s != f) { if (k < m) { if (nowt < a[k].t) { if (s < f) { printf("R"); s++; } else { printf("L"); s--; } } else if (nowt == a[k].t) { if (a[k].l <= s && a[k].r >= s) { printf("X"); } else { if (s < f) { if (a[k].l == s + 1) printf("X"); else { printf("R"); s++; } } else { if (a[k].r == s - 1) printf("X"); else { printf("L"); s--; } } } k++; } nowt++; } else { if (s < f) { printf("R"); s++; } else { printf("L"); s--; } } } printf("\n"); } }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int n, m, s, f; int main() { int i; scanf("%d%d%d%d", &n, &m, &s, &f); int now = 1; int t, l, r; int flag = s < f ? 1 : -1; char step = 'L'; if (flag == 1) step = 'R'; for ((i) = (1); (i) <= (m); ++(i)) { scanf("%d%d%d", &t, &l, &r); while (s != f && now <= t) { if (now != t || (!(l <= (s) && (s) <= r) && !(l <= (s + flag) && (s + flag) <= r))) { s += flag; cout << step; } else cout << 'X'; now++; } } while (s != f) { s += flag; now++; cout << step; } cout << endl; return 0; }
CPP