Codeforces-1234-D. Distinct Characters Queries(莫队/线段树)

D. Distinct Characters Queries

You are given a string s consisting of lowercase Latin letters and q queries for this string.

Recall that the substring s[l;r] of the string s is the string slsl+1…sr. For example, the substrings of “codeforces” are “code”, “force”, “f”, “for”, but not “coder” and “top”.

There are two types of queries:

1 pos c (1≤pos≤|s|, c is lowercase Latin letter): replace spos with c (set spos:=c);
2 l r (1≤l≤r≤|s|): calculate the number of distinct characters in the substring s[l;r].

Input

The first line of the input contains one string s consisting of no more than 105 lowercase Latin letters.

The second line of the input contains one integer q (1≤q≤105) — the number of queries.

The next q lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.

Output

For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.

Examples

input

abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

output

3
1
2

input

dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11

output

5
2
5
2
6

解题思路:

带修莫队,线段树也可以做。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
char s[maxn];
struct node
{
    int l,r;
    int time;
    int id;
}t[maxn];
struct Change
{
    int pos;  //要修改的位置
    int col;  //要改成的值
}c[maxn];
int n,m,a[maxn];
int block;
int numq,numc; //查询、修改操作的次数
int ans,cnt[maxn],res[maxn];
bool cmp(node a,node b){//排序
    if( (a.l/block)==(b.l/block) ){//当左端点位于同一个块时
        if( (a.r/block)==(b.r/block) )//当右端点位于同一个块时
            return a.time<b.time;
        else
            return a.r<b.r;
    }
    else//当左端点不位于同一个块时
        return a.l<b.l;
    //return (a.l/block)^(b.l/block) ? a.l<b.l : ( ((a.r/block)^(b.r/block))?a.r<b.r:a.time<b.time );
}
void add(int x)  //统计新的
{
    if(cnt[x]==0)
        ans++;
    cnt[x]++;
}
void del(int x)  //减去旧的
{
    cnt[x]--;
    if(cnt[x]==0)
        ans--;
}
void change(int x,int ti) //改变时间轴
{
    if(c[ti].pos>=t[x].l && c[ti].pos<=t[x].r)
    {
        del(a[c[ti].pos]);  //删除指定位置上的值
        add(c[ti].col); //统计新的数
    }
    swap(c[ti].col,a[c[ti].pos]); //直接互换,下次执行时必定是回退这次操作
}
int main()
{
    while(~scanf("%s",s+1))
    {
        n=strlen(s+1);
        for(int i=1;i<=n;i++){
            a[i]=s[i];
        }
        ans = 0;
        numq = numc = 0;
        memset(cnt,0,sizeof(cnt));
        block = 2000;
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            int d;
            scanf("%d",&d);
            if(d==2)  //查询
            {
                numq++;   //查询操作次数+1
                scanf("%d%d",&t[numq].l,&t[numq].r);
                t[numq].id = numq;
                t[numq].time = numc;
            }
            else  //修改
            {
                char sss[3];
                numc++;   //修改操作次数+1
                scanf("%d%s",&c[numc].pos,sss);
                c[numc].col=sss[0];
            }
        }
        sort(t+1,t+1+numq,cmp);  //对询问进行排序
        int l=1,r=0,time=0; //左右指针与时间轴
        for(int i=1;i<=numq;i++)
        {
            while(l>t[i].l)  add(a[--l]); //[l-1,r,time]
            while(l<t[i].l)  del(a[l++]); //[l+1,r,time]
            while(r<t[i].r)  add(a[++r]); //[l,r+1,time]
            while(r>t[i].r)  del(a[r--]); //[l,r-1,time]
            while(time<t[i].time)  change(i,++time); //[l,r,time+1]
            while(time>t[i].time)  change(i,time--); //[l,r,time-1]
            res[t[i].id] = ans;
        }
        for(int i=1;i<=numq;i++)
            printf("%d\n",res[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值