Dragons Flight | Coding

๐ Dragons Flight โ Write-Up | Cyber Apocalypse 2025
| Details | |
|---|---|
| URL | [Hack The Box :: Dragons Flight |
| Difficulty | Easy |
| Team Rank | 541 / 8129 |
| CTF Date | 26 Mar, 13:00 |
๐ Challenge Summary
In the mystical realm of the Floating Isles, dragons navigate the skies between sacred sanctuaries. But turbulent wind currents across segmented paths can make or break their flight. You, the Dragon Flight Master, must simulate their journey by adapting to wind shifts and answering critical pathfinding queries in real-time.
๐ง Problem Rules
You are given:
- A list of
Nwind segments, each representing a stretch of the sky.- Positive values = tailwinds (boost)
- Negative values = headwinds (resistance)
- A sequence of
Qoperations:U i x: Update the wind effect at segmentito valuexQ l r: Query the maximum contiguous subarray sum (most favourable continuous stretch) from indexltor(inclusive, 1-based)
๐ป Python Solution
We use Kadaneโs Algorithm to compute the maximum subarray sum for each query and perform updates directly on the array.
def max_subarray_sum(arr):
max_sum = float('-inf')
current = 0
for val in arr:
current = max(val, current + val)
max_sum = max(max_sum, current)
return max_sum
# Read input
N, Q = map(int, input().split())
arr = list(map(int, input().split()))
for _ in range(Q):
parts = input().split()
if parts[0] == 'U':
i = int(parts[1]) - 1
x = int(parts[2])
arr[i] = x
elif parts[0] == 'Q':
l = int(parts[1]) - 1
r = int(parts[2])
print(max_subarray_sum(arr[l:r]))
๐งช Example Flight Simulation
Input
6 6
-10 -7 -1 -4 0 -5
Q 3 3
U 2 9
Q 6 6
U 1 -1
Q 6 6
U 5 -9
Output
-1
-5
-5
Step-by-Step Breakdown:
-
Initial Array:
[-10, -7, -1, -4, 0, -5] -
Q 3 3
โ Segment 3 =-1
โ โค Output:-1 -
U 2 9
โ Update segment 2 to9
โ New array:[-10, 9, -1, -4, 0, -5] -
Q 6 6
โ Segment 6 =-5
โ โค Output:-5 -
U 1 -1
โ Update segment 1 to-1
โ New array:[-1, 9, -1, -4, 0, -5] -
Q 6 6
โ Segment 6 =-5
โ โค Output:-5 -
U 5 -9
โ Update segment 5 to-9
โ Final array:[-1, 9, -1, -4, -9, -5]
๐ฎ Final Challenge Input
Input
7 5
7 4 0 -4 -6 3 2
U 7 4
Q 4 7
U 1 3
U 3 4
U 1 6
Operation Breakdown
- Initial Array:
[7, 4, 0, -4, -6, 3, 2] - U 7 4 โ Array becomes:
[7, 4, 0, -4, -6, 3, 4] - Q 4 7 โ Query
[ -4, -6, 3, 4 ]โ max contiguous sum =7 - U 1 3 โ Update index 1 to
3 - U 3 4 โ Update index 3 to
4 - U 1 6 โ Update index 1 to
6
Output
7
๐ Flag
HTB{DR4G0N_FL1GHT_5TR33_5907f8463bb8ab2291f1cc49c699d816}