I think this method could work
x[torch.arange(B).unsqueeze(1).unsqueeze(1),
  torch.arange(S).unsqueeze(1),
  I[torch.arange(B).unsqueeze(1), torch.arange(S), :, 0],
  I[torch.arange(B).unsqueeze(1), torch.arange(S), :, 1]] = S