রবিবার, ১৪ এপ্রিল, ২০১৯

https://toph.co/p/another-query-on-string

1 x ch
Change the xth character of S to ch (i.e. S[x] = ch).
2 L R ch
Count the number of occurrences of ch between indices L and R in S. That means, you have to count number of such indices i where S[i] == ch.

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fast ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);
#define filein freopen("input.txt","r",stdin)
#define fileout freopen("output.txt","w",stdout)
using namespace std;
int n,m;
string str ;
int ara[100009];
int tree[100009*4][27];
void init(int node,int b, int e,char ch)
{
    if(b==e)
    {
        tree[node][ch-'a']= {str[b-1]==ch};
        return ;
    }
    int mid=(b+e)>>1;
    int left=node<<1;
    int right=left+1;
    init(left, b,mid,ch);
    init(right,mid+1,e,ch);
    tree[node][ch-'a']=tree[left][ch-'a']+tree[right][ch-'a'];
}
void update(int node,int b, int e, int i, char ch, int value)
{
    if(e<i or b>i)
        return ;
    if(b>=i and e<=i)
    {
        tree[node][ch-'a']=value;
        return ;
    }
    int mid=(b+e)>>1;
    int left=node<<1;
    int right=left+1;
    update(left, b,mid,i,ch,value);
    update(right,mid+1,e,i,ch,value);
    tree[node][ch-'a']=tree[left][ch-'a']+tree[right][ch-'a'];
}
int  query(int node,int b, int e, int i,int j,char ch)
{
    if(e<i or b>j)
        return 0;
    if(b>=i and e<=j)
    {
        return tree[node][ch-'a'];
    }
    int mid=(b+e)>>1;
    int left=node<<1;
    int right=left+1;
    int x=query(left, b,mid,i,j,ch);
    int y=query(right,mid+1,e,i,j,ch);
    return x+y;
}
bool check[80];
int  main()
{
    cin>>n>>m;
    cin>>str;
    for( int i=0; i<n; i++)
    {
        if(check[str[i]-'a']==false)
        {
            check[str[i]-'a']=true;
            init(1,1,n,str[i]);
        }
    }
    while(m--)
    {
        int a;
        cin>>a;
        if(a==1)
        {
            int b;
            char ch;
            cin>>b>>ch;
            if(str[b-1]!=ch)
            {
                update(1,1,n,b,ch,1) ;
                update(1,1,n,b,str[b-1],0);
                str[b-1]=ch;
            }

        }
        else
        {
            int a1,b;
            char ch;
            cin>>a1>>b>>ch;
            cout<<query(1,1,n,a1,b,ch)<<'\n';

        }
    }

}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...