fork download
  1.  
  2. import java.io.*;
  3.  
  4. public class Main {
  5.  
  6. static class EntradaRapida {
  7. private final BufferedInputStream flujoEntrada =
  8.  
  9. private final byte[] buffer = new byte[1 << 16];
  10. private int indice = 0;
  11. private int limite = 0;
  12.  
  13. private int leer() throws IOException {
  14. if (indice >= limite) {
  15. limite = flujoEntrada.read(buffer);
  16. indice = 0;
  17.  
  18. if (limite <= 0) return -1;
  19. }
  20.  
  21. return buffer[indice++];
  22. }
  23.  
  24. String leerTexto() throws IOException {
  25. StringBuilder texto = new StringBuilder();
  26. int caracter;
  27.  
  28. do {
  29. caracter = leer();
  30. if (caracter == -1) return null;
  31. } while (caracter <= 32);
  32.  
  33. while (caracter > 32) {
  34. texto.append((char) caracter);
  35. caracter = leer();
  36. }
  37.  
  38. return texto.toString();
  39. }
  40.  
  41. int leerEntero() throws IOException {
  42. int caracter;
  43.  
  44. do {
  45. caracter = leer();
  46. if (caracter == -1) return -1;
  47. } while (caracter <= 32);
  48.  
  49. int valor = 0;
  50.  
  51. while (caracter > 32) {
  52. valor = valor * 10 + caracter - '0';
  53. caracter = leer();
  54. }
  55.  
  56. return valor;
  57. }
  58. }
  59.  
  60. static class ArbolSegmentos {
  61.  
  62. int base;
  63. int[] arbol;
  64.  
  65. ArbolSegmentos(char[] cadena) {
  66. int n = cadena.length;
  67.  
  68. base = 1;
  69.  
  70. while (base < n) {
  71. base <<= 1;
  72. }
  73.  
  74. arbol = new int[base << 1];
  75.  
  76. for (int i = 0; i < n; i++) {
  77. arbol[base + i] = 1 << (cadena[i] - 'a');
  78. }
  79.  
  80. for (int i = base - 1; i > 0; i--) {
  81. arbol[i] = arbol[i << 1] |
  82. arbol[i << 1 | 1];
  83. }
  84. }
  85.  
  86. void actualizar(int posicion, char letra) {
  87. int nodo = base + posicion;
  88.  
  89. arbol[nodo] = 1 << (letra - 'a');
  90.  
  91. nodo >>= 1;
  92.  
  93. while (nodo > 0) {
  94. arbol[nodo] = arbol[nodo << 1] |
  95. arbol[nodo << 1 | 1];
  96.  
  97. nodo >>= 1;
  98. }
  99. }
  100.  
  101. int consultar(int izquierda, int derecha) {
  102. izquierda += base;
  103. derecha += base + 1;
  104.  
  105. int mascara = 0;
  106.  
  107. while (izquierda < derecha) {
  108.  
  109. if ((izquierda & 1) == 1) {
  110. mascara |= arbol[izquierda++];
  111. }
  112.  
  113. if ((derecha & 1) == 1) {
  114. mascara |= arbol[--derecha];
  115. }
  116.  
  117. izquierda >>= 1;
  118. derecha >>= 1;
  119. }
  120.  
  121. return Integer.bitCount(mascara);
  122. }
  123. }
  124.  
  125. public static void main(String[] args) throws IOException {
  126.  
  127. EntradaRapida lector = new EntradaRapida();
  128.  
  129. String texto = lector.leerTexto();
  130.  
  131. if (texto == null) return;
  132.  
  133. char[] cadena = texto.toCharArray();
  134.  
  135. int cantConsultas = lector.leerEntero();
  136.  
  137. ArbolSegmentos arbol = new ArbolSegmentos(cadena);
  138.  
  139. StringBuilder resultado = new StringBuilder();
  140.  
  141. for (int i = 0; i < cantConsultas; i++) {
  142.  
  143. int tipo = lector.leerEntero();
  144.  
  145. if (tipo == 1) {
  146.  
  147. int posicion = lector.leerEntero() - 1;
  148. char letra = lector.leerTexto().charAt(0);
  149.  
  150. if (cadena[posicion] != letra) {
  151. cadena[posicion] = letra;
  152. arbol.actualizar(posicion, letra);
  153. }
  154.  
  155. } else {
  156.  
  157. int izquierda = lector.leerEntero() - 1;
  158. int derecha = lector.leerEntero() - 1;
  159.  
  160. int cantidad = arbol.consultar(
  161. izquierda, derecha
  162. );
  163.  
  164. resultado.append(cantidad).append('\n');
  165. }
  166. }
  167.  
  168. System.out.print(resultado);
  169. }
  170. }
  171.  
Success #stdin #stdout 0.07s 54404KB
stdin
Standard input is empty
stdout
Standard output is empty