Getting sum of all numbers contained in substrings of S on a segment
$O(N \log K)$ running time looks achievable.

Hints:


Suppose you know the answer to the query $l,m$ and the query $m+1,r$.  Can you work out the answer to the query $l,r$ efficiently?  Would precomputation help?  What information do you need to know?
Use a balanced binary tree, where each digit is at a leaf.  At each internal node $\eta$, you will store the answer to the query $l_\eta,r_\eta$, where $l_\eta$ is the leftmost leaf under $\eta$ and $r_\eta$ is the rightmost leaf under $\eta$.  Also, story the sum of all numbers that can be made by starting at index $i$ (for $l_\eta \le i \le r_\eta$) and ending at index $r_\eta$, as well as the sum of all numbers that can be made by starting at $l_\eta$ and ending at some index $j$ (where $l_\eta \le j \le r_\eta$).


Now for any particular query $l,r$, the answer can be obtained efficiently given only $O(\lg K)$ numbers in the tree.

I'll let you have the joy of working out the rest, since this sounds like a programming contest problem and you should probably make some effort on your own.