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

C

  • 题意: 有两种管子(直和弯的),给出一个由这两种管子构成的\(n*2\)的矩阵,问能不能通过任意调整管子的朝向使得左上角能走到右下角.
  • 思路: 直管只能直着走,弯管必须上下都是弯管才能向前走,且要走到另外一层.
#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

  • 题意: 给出一个字符串,操作有修改某个点和查询区间内有多少不同的字母.
  • 思路: 维护\(26\)个树状数组,代表每个字母从\(1到i\)出现了多少次,对于查询,遍历这\(26\)个树状数组看每个字母是否在区间内出现,对于修改,这个位置原来的字母减去1,新来的字母加上1即可.
#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;
}

猜你喜欢

转载自www.cnblogs.com/xxrlz/p/11616584.html