Week5练习赛总结

· · 个人记录

题目编号 T1 T2 T3 T4 总结
pts 70 60 25 0
耗时 40min 50min 20min 0min 2h

T1

正解DP,但打了个贪心60pts。

正解是用DP_i表示到第i个字符最大可产生的答案,对于以下合法的状态转移,有:

code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 10, INF = 1e9 + 7;
int n, a, b, c, cnt, ans, dp[N];
string s, s1, s2, s3;
int check(int x, string y){
    int len = y.size();
    int cnt = 0;
    if(x < len){
        return 0;
    }
    for(int i = x - len + 1; i <= x; i ++){
        if(s[i] != y[cnt ++]){
            return 0;
        }
    } 
    return 1;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> a >> b >> c >> s;
    s = '#' + s;
    s1 = "acwa", s2 = "acwaac", s3 = "acwaacwaac";
    for(int i = 1; i <= n; i ++){
        dp[i] = dp[i - 1];
        if(check(i, s1)){
            dp[i] = max(dp[i], dp[i - 4] + a);
        }
        if(check(i, s2)){
            dp[i] = max(dp[i], dp[i - 6] + b);
        }
        if(check(i, s3)){
            dp[i] = max(dp[i], dp[i - 10] + c);
        }
    }
    cout << dp[n];
    return 0;
}

T2

dij,赛时建边和最大值被卡了一下,-30pts。

对于每个公交车站i,都有:

并查集板子,对于每一个被两个黑节点围住的红节点块求数量,最后遍历所有黑节点乘法原理+乘法分配律统计所有答案即可。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 10; 
string s;
vector<int> e[N];
int fa[N], num[N], n, ans;
void add_edge(int u, int v){
    e[u].push_back(v), e[v].push_back(u);
    return ;
}
int find(int x){
    if(x != fa[x]) return fa[x] = find(fa[x]);
    return x;
}
void Get(int u){
    vector<int> G;
    int sum = 0;
    for(int i = 0, v; i < e[u].size(); i ++){
        v = e[u][i];
        if(s[v] == 'R'){
            int fv = find(v);
            G.push_back(num[fv]);
            sum += num[fv], ans += num[fv];
        }
    }
    int cnt = 0;
    for(int i = 0; i < G.size(); i ++){
        cnt += G[i] * (sum - G[i]);
    }
    ans += cnt / 2;
    return ;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> s;
    s = ' ' + s;
    for(int i = 1; i <= n; i ++){
        fa[i] = i, num[i] = 1;
    }
    for(int i = 1, u, v; i < n; i ++){
        cin >> u >> v;
        if(s[u] == 'R' && s[v] == 'R'){
            int fu = find(u), fv = find(v);
            if(fu != fv){
                fa[fu] = fv;
                num[fv] += num[fu];
            }
        }
        add_edge(u, v);
    }
    for(int i = 1; i <= n; i ++){
        if(s[i] == 'B') Get(i);
    }
    cout << ans;
    return 0;
}

T4

树状数组的区间修改以及区间求逻辑与

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, mod = 1e9 + 7;
int n, q, m, a[N], pw[N];
struct Bit_Tree {
    int C[N];
    int lowbit(int x) {
        return x & (-x);
    }
    void modify(int x, int value) {
        while(x <= n) {
            C[x] += value, x += lowbit(x);
        }
        return ;
    }
    int query(int x) {
        int ans = 0;
        while(x > 0) {
            ans += C[x], x-= lowbit(x);
        }
        return ans;
    }
    int Q(int l, int r) {
        return query(r) - query(l - 1);
    }
} T[35];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    pw[0] = 1;
    for(int i = 1; i <= 1000000; i ++) {
        pw[i] = (pw[i - 1] * 2) % mod;
    }
    cin >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i ++) {
        for(int j = 0; j < 30; j ++) {
            if((a[i] >> j) & 1) {
                T[j].modify(i, 1);
            }
        }
    }
    cin >> q;
    while(q --) {
        int op;
        cin >> op;
        if(op == 1) {
            int pos, x;
            cin >> pos >> x;
            for(int j = 0; j < 30; j ++) {
                if((a[pos] >> j) & 1) {
                    T[j].modify(pos, -1);
                }
            }
            a[pos] = x;
            for(int j = 0; j < 30; j ++) {
                if((a[pos] >> j) & 1) {
                    T[j].modify(pos, 1);
                }
            }
        } else {
            int l, r, ans = 0;
            cin >> l >> r;
            for(int j = 0; j < 30; j ++) {
                ans += 1ll * (pw[T[j].Q(l, r)] - 1) * pw[j] % mod;
                ans %= mod;
            }
            cout << ans << '\n';
        }
    }
    return 0;
}