codeforces 551E GukiZ and GukiZiana 分块

参考:http://blog.youkuaiyun.com/discreeter/article/details/78184803
题目:

https://vjudge.net/problem/CodeForces-551E
题意:

给定一个长度为n的序列a,有两种操作:

1 l r x:把区间[l,r]内的元素都加上x
2 y:查询整个序列中值为y的元素的最远距离,若没有y输出−1

心得:
这个奇怪的询问方式一般只能分块来解决了..

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 500000 + 10, INF = 0x3f3f3f3f;
int L[N], R[N], pos[N];
int sz, block;
LL a[N], b[N], add[N];
void reset(int x) {
    for(int i = L[x]; i <= R[x]; i++) b[i] = a[i];
    sort(b + L[x], b + 1 + R[x]);
}
void init(int n) {
    block = (int)sqrt(n);
    sz = n / block;
    if(n % block) sz++;
    for(int i = 1; i <= n; i++) pos[i] = (i-1) / block + 1;
    for(int i = 1; i <= sz; i++) {
        L[i] = (i-1) * block + 1;
        R[i] = i * block;
    }
    R[sz] = n;
    for(int i = 1; i <= sz; i++) reset(i);
    memset(add, 0, sizeof add);
}
void update(int l, int r, int val) {
    int lb = pos[l], rb = pos[r];
    if(lb == rb) {
        for(int i = l; i <= r; i++) a[i] += val;
        reset(lb);
    }
    else {
        for(int i = l; i <= R[lb]; i++) a[i] += val;
        for(int i = L[rb]; i <= r; i++) a[i] += val;
        for(int i = lb+1; i < rb; i++) add[i] += val;
        reset(lb); reset(rb);
    }
}
int query(LL val) {
    int x = 0, y = 0;
    for(int i = 1; i <= sz; i++)
        if(binary_search(b + L[i], b + 1 + R[i], val - add[i])) {
            for(int j = L[i]; j <= R[i]; j++)
                if(a[j] + add[i] == val)
                {
                    x = j; break;
                }
            break;
        }
    for(int i = sz; i >= 1; i--)
        if(binary_search(b + L[i], b + 1 + R[i], val - add[i])) {
            for(int j = R[i]; j >= L[i]; j--)
                if(a[j] + add[i] == val)
            {
                y = j; break;
            }
            break;
        }
    if(!x && !y) return -1;
    else return y - x;
}
int main() {
    int n, m;
    while(~ scanf("%d%d", &n, &m)) {
        for(int i = 1; i <= n; i++) scanf("%LL", &a[i]);
        init(n);
        int opt, x, y, z;
        for(int i = 1; i <= m; i++) {
            scanf("%d", &opt);
            if(opt == 1) {
                scanf("%d%d%d", &x, &y, &z);
                update(x, y, z);
            }
            else {
                scanf("%d", &x);
                printf("%d\n", query(x));
            }
        }
    }
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值