313B - Ilya and Queries - CodeForces Solution


dp implementation *1100

Please click on ads to support us..

Python Code:

from bisect import *
from collections import *
from itertools import *
from heapq import *
from sys import stdin,setrecursionlimit
from functools import lru_cache
setrecursionlimit(10**7)

lines=stdin.readlines()
lines.reverse()
setrecursionlimit(10**8)
def input():
    return lines.pop().strip("\n")
s=list(input())
a=[0]
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        a.append(1)
    else:
        a.append(0)
a=list(accumulate(a))
q=int(input())
for i in range(q):
    l,r=map(int,input().split())
    print(a[r-1]-a[l-1])
    	 		  	  		  		 	 	   		 	

C++ Code:

    #include<bits/stdc++.h>

    using namespace std;
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>

    #define ll long long
    # define pii pair<int , int >
    #define pll pair<long long, long long>
    #define vi vector<int> 
    #define vll vector<long long>
    #define mii map<int, int>
    #define si set<int>
    #define sc set<char>

    /* FUNCTION */
    #define f(i, s, e) for(long long i=s; i<e; i++)
    #define fe(i, s, e) for(long long i=s; i<=e; i++)
    #define rf(i, e, s) for(long long i=e-1; i>=s; i--)
    #define pb push_back
    #define eb emplace_back

    /* PRINTS */
    template <class T>
    void print_v(vector<T> &v){cout<<"{"; for(auto x:v) cout<<x<<","; cout<<"\b";}

    /* UTILS */
    #define MOD 1000000007
    #define pi 3.14159
    ll min(ll a, int b) {if(a<b) return a; return b;}
    ll min(int a, ll b) {if (a<b) return a; return b;}
    ll max(ll a, int b) { if (a>b) return a; return b; }
    ll max(int a,ll b) { if (a>b) return a; return b; }
    ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
    ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
    string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
    string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
    bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) if (a%i==0) return 0; return 1; }
    void yes() { cout<<"YES\n"; }
    void no() { cout<<"NO\n"; }

    typedef long int int32;
    typedef unsigned long int uint32;
    typedef long long int int64;
    typedef unsigned long long int  uint64;
    typedef vector<ll> vl;
    typedef vector<char> vc;
    typedef vector<string> vs;

    template <typename T>
    void add(T& a, T b, T M) {
        a = ((a % M) + (b % M)) % M;
    }
     
    template <typename T>
    void mul(T& a, T b, T M) {
        a = ((a % M) * (b % M)) % M;
    }
     
    template <typename T>
    void sub(T& a, T b, T M) {
        a = (a - b + M) % M;
    }

    /* ------------------------------------------ OM GAN GANPATYAY NAMAH -------------------------------------------------*/

    // int solve(ll n){
        
    // }
    // map<int,int>mp;
    ll M=1e9+7;



    void solve(int x){
        if(x<0) return;
        ll a=sqrt(x);
        if(a*a!=x)a++;
        a*=a;
        solve(a-x-1);
        for(int i=x; i>=a-x; i--){
            cout<<i<<" ";
        }

            }

    int main(){
       

        #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin); //file input.txt is opened in reading mode i.e "r"
        freopen("output.txt","w",stdout);  //file output.txt is opened in writing mode i.e "w"
        #endif

        ios_base::sync_with_stdio(false);
        cin.tie(NULL);   
        cout.tie(NULL);
        // map<int,int> mp;
    //     ll t;
    //     cin>>t;


    //     while(t--){
                string s;
                cin>>s;
                vector<ll> pref(s.length()+1);
                pref[1]=0;
                for(ll i=1; i<s.length(); i++){
                    if(s[i]==s[i-1]) {
                        pref[i+1]=pref[i]+1;
                    }
                    else pref[i+1]=pref[i];
                }
                ll n;
                cin>>n;
                while(n--){
                    ll a,b;
                    cin>>a>>b;
                    // cout<<pref[b]<<" "<<pref[a]<<", ";
                    cout<<pref[b]-pref[a]+0ll<<endl;


                }
        

    // }
        return 0;}


Comments

Submit
0 Comments
More Questions

129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem