結果
問題 | No.1439 Let's Compare!!!! |
ユーザー |
![]() |
提出日時 | 2025-03-20 18:59:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 652 ms / 2,000 ms |
コード長 | 2,715 bytes |
コンパイル時間 | 183 ms |
コンパイル使用メモリ | 83,032 KB |
実行使用メモリ | 147,928 KB |
最終ジャッジ日時 | 2025-03-20 19:00:42 |
合計ジャッジ時間 | 7,291 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 17 |
ソースコード
import sysclass SegmentTree:def __init__(self, data):self.n = len(data)self.size = 1while self.size < self.n:self.size <<= 1 # Find the next power of twoself.min_tree = [float('inf')] * (2 * self.size)# Fill the leavesfor i in range(self.n):self.min_tree[self.size + i] = data[i]# Build the tree upwardsfor i in range(self.size - 1, 0, -1):self.min_tree[i] = min(self.min_tree[2*i], self.min_tree[2*i+1])def update(self, pos, new_val):pos += self.size # Translate to the leaf indexself.min_tree[pos] = new_val# Move up to update the treepos >>= 1while pos >= 1:new_min = min(self.min_tree[2*pos], self.min_tree[2*pos+1])if self.min_tree[pos] == new_min:break # No change needed further upself.min_tree[pos] = new_minpos >>= 1def get_min(self):return self.min_tree[1] # The root contains the min of the entire rangedef main():input = sys.stdin.read().split()idx = 0N = int(input[idx]); idx +=1S = input[idx]; idx +=1T = input[idx]; idx +=1Q = int(input[idx]); idx +=1s_digits = list(S)t_digits = list(T)# Prepare initial data for the segment treedata = []for i in range(N):if s_digits[i] != t_digits[i]:data.append(i+1) # Positions are 1-based in the problemelse:data.append(float('inf'))tree = SegmentTree(data)for _ in range(Q):c_i = input[idx]; idx +=1x_i = int(input[idx]); idx +=1y_i = input[idx]; idx +=1# Adjust to 0-based indexpos_in_digits = x_i -1if c_i == 'S':s_digits[pos_in_digits] = y_ielse:t_digits[pos_in_digits] = y_i# Determine the new value for this position in the segment treeif s_digits[pos_in_digits] != t_digits[pos_in_digits]:new_val = x_i # 1-based positionelse:new_val = float('inf')tree.update(pos_in_digits, new_val)# Query the minimal differing positionmin_pos = tree.get_min()if min_pos == float('inf'):print('=')else:# Compare the digits at min_pos (1-based)s_char = s_digits[min_pos -1]t_char = t_digits[min_pos -1]if s_char > t_char:print('>')elif s_char < t_char:print('<')else:print('=')if __name__ == '__main__':main()