Codeforces Round #590 (Div. 3) C,D

时间:2019-10-02 01:15:07   收藏:0   阅读:130

C

#include<bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int,int> pii;
const int N = 2e5+10;
int op[N][2];
int n;
string s;
int main(){
    // freopen("1.out","w",stdout);
    int t,val;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        cin >> s;
        for(int i=0;i<n;++i){
            if(s[i]=='1' || s[i]=='2')  op[i+1][0] = 1;
            else op[i+1][0] = 2;
        }
        cin >> s;
        for(int i=0;i<n;++i){
            if(s[i]=='1' || s[i]=='2')  op[i+1][1] = 1;
            else op[i+1][1] = 2;
        }
        int step = n*3;
        int x = 1, y = 0,px =0;
        while(step--){
            if(x == n+1)  break;
            if(op[x][y]==1) x++; 
            else{
                if(op[x][1-y]!=2)   break;
                x++; y = 1-y;
            }
        }

        if(x==n+1 && y==1){
            puts("YES");
        }else{
            puts("NO");
        }
    }
    return 0;
}

D

#include <iostream>
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int n ;
struct tree{
    int a[N];
    int lowbit(int x){
        return x&(-x);
    }
    void update(int p,int x){
        for(int i=p;i<=n;i+=lowbit(i))
            a[i] += x;
    }
    int sum(int p){
        int res = 0;
        for(int i=p;i>0;i-=lowbit(i))
            res += a[i];
        return res;
    }
    int query(int l,int r){
        return sum(r) - sum(l-1);
    }
}ch[30];

string s;
int main(){

    cin >> s;
    n = s.length();
    int m,op,l,r;
    char cha;
    for(int i=0;i<n;++i){
        ch[s[i]-'a'].update(i+1,1);
    }
    cin >> m;
    while(m--){
        cin >> op;
        if(op == 1){
            cin >> l >>  cha;
            ch[s[l-1]-'a'].update(l,-1);
            s[l-1] = cha;
            ch[s[l-1]-'a'].update(l,1);
        }else{
            cin >> l >> r;
            int ans = 0;
            for(int i=0;i<26;++i){
                if(ch[i].query(l,r)>0)  ans++;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

原文:https://www.cnblogs.com/xxrlz/p/11616584.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!