Problem:
Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
  class Program {
    public static void Main(string[] args) {
      var sw = new StreamWriter(Console.OpenStandardOutput()) {
        AutoFlush=
#if DEBUG
			true
#else
			false
#endif
      };
      Console.SetOut(sw);
      new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
      , ' '));
      Console.Out.Flush();
    }
    public void Solve(ConsoleInput cin) {
			var Trees = new SegmentTree<int>[26];
			var N = cin.ReadInt;
			for(int i = 0; i<26; i++) 
				Trees[i]=new SegmentTree<int>(N, (i, j) => i+j, 0);
			var S = cin.ReadLine.ToCharArray();
			for(int i = 0; i<S.Length; i++) {
				Trees[S[i]-'a'].ImmediateUpdate(i, 1);
			}
			var Q = cin.ReadInt;
			for(int q = 0; q<Q; q++) {
				var query = cin.ReadInt;
				switch(query) {
					case 1:
						var iq = cin.ReadInt-1;
						var cq = cin.Read[0];
						Trees[S[iq]-'a'].ImmediateUpdate(iq, 0);
						Trees[cq-'a'].ImmediateUpdate(iq, 1);
						S[iq]=cq;
						break;
					case 2:
						var lq = cin.ReadInt;
						var rq = cin.ReadInt;
						Console.WriteLine(Trees.Count(t => t.GetValue(lq-1, rq)>0));
						break;
					default:
						break;
				}
			}
    }
	}

  static class ExtendClass {
    public static string Join<T>(this IEnumerable<T> X, string separator = " ")
		  => string.Join(separator, X);

	}
  class ConsoleInput {
    private readonly TextReader _stream;
    private readonly char _separator = ' ';
    private readonly Queue<string> inputStream;
    public ConsoleInput(TextReader stream, char separator = ' ') {
      this._separator=separator;
      this._stream=stream;
      inputStream=new Queue<string>();
    }
    public string Read {
      get {
        if(inputStream.Count!=0) return inputStream.Dequeue();
        string[] tmp = _stream.ReadLine().Split(_separator);
        for(int i = 0; i<tmp.Length; ++i)
          inputStream.Enqueue(tmp[i]);
        return inputStream.Dequeue();
      }
    }
    public string ReadLine { get { return _stream.ReadLine(); } }
    public int ReadInt { get { return int.Parse(Read); } }
    public long ReadLong { get { return long.Parse(Read); } }
    public double ReadDouble { get { return double.Parse(Read); } }
    public string[] ReadStrArray(long N) {
      var ret = new string[N];
      for(long i = 0; i<N; ++i)
        ret[i]=Read;
      return ret;
    }
    public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
    public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
    public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
    public bool isEOF {
      get { return _stream.Peek()==-1; }
    }
	}
	class SegmentTree<T> {
		//UI is 0-indexed, internal is 1-indexed
		public readonly int rank;
		public T[] Nodes { get; }
		public Func<T, T, T> OPER { get; }
		public T unit { get; }
		private int width { get { return 1<<(rank-1); } }
		private T raise(int c) {
			return (c&1)==0
			? OPER(Nodes[c-1], Nodes[(c-1)+1]) //$c is even
			: OPER(Nodes[(c-1)-1], Nodes[c-1]); //odd
		}
		private int leafIndex(int x) => (1<<(rank-1))+(x-1);
		public SegmentTree(int count, Func<T, T, T> _OPER, T _unit) {
			if(count==0) throw new ArgumentNullException();
			rank=count==1 ? 1 : (int)Math.Log(count-1, 2)+2;
			Nodes=Enumerable.Repeat(_unit, (1<<rank)-1).ToArray();
			OPER=_OPER;
			unit=_unit;
		}
		public void ImmediateUpdate(int x, T value) {
			var r = rank-1;
			var jc = (1<<r)+x;
			Nodes[jc-1]=value;
			for(r--; r>=0; r--) {
				var parentIndex = (jc>>1)-1;
				var oldValue = Nodes[parentIndex];
				Nodes[parentIndex]=raise(jc);
				if(oldValue.Equals(Nodes[parentIndex])) break;
				jc>>=1;
			}
		}
		public void ImmediateOperate<S>(int x, S value, Func<S, T, T> LeftOP) {
			ImmediateUpdate(x, LeftOP(value, Nodes[leafIndex(x)]));
		}
		public T GetValue(int start, int end) => GV(start, end, 1, 0, width);
		private T GV(int start, int end, int count, int focusStart, int focusEnd) {
			if(focusEnd<=start||end<=focusStart) return unit;
			if(start<=focusStart&&focusEnd<=end) return Nodes[count-1];
			var mid = focusStart+(focusEnd-focusStart)/2;
			return OPER(
				GV(start, end, count*2, focusStart, mid),
				GV(start, end, count*2+1, mid, focusEnd)
			);
		}
		public IEnumerable<T> GetLayer(int c) => Nodes.Skip((1<<(c-1))-1).Take(1<<(c-1));
		public IEnumerable<T> GetLeaves() => Nodes.Skip((1<<(rank-1))-1).Take(1<<(rank-1));
	}
}