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

C

  • The meaning of problems: there are two pipes (straight and curved) to give a composed of the two tubes \ (n * 2 \) matrix Q can be adjusted through the tube such that the upper left corner toward the right can come lower corner.
  • Ideas: straight pipe can only go straight, bend down all must bend in order to move forward and to come to another layer.
#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

  • Meaning of the questions: given a string operation has modified how many different letters within a point and query interval.
  • Ideas: Maintenance \ (26 \) tree-like array, representing each letter from \ (1 to i \) there have been many times for the query, traverses this \ (26 \) tree-like array to see whether each letter interval appears to modify the position of the original letter minus 1, plus one new letter can be.
#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;
}

Guess you like

Origin www.cnblogs.com/xxrlz/p/11616584.html