首页 > 代码库 > [原创题] count 分段Hash

[原创题] count 分段Hash

题意

  技术分享

 

实现

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <map>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 300000;

int n, m, a[N];
int M; map<int, int> h;

inline int rd(void) {
    int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == -) f = -1;
    int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-0; return x*f;
}

inline void Modify(int x, int w, int d) { for (x += M; x > 0; x >>= 1) h[x][w] += d; }
inline void Query(int L, int R, int w) {
    int sum = 0;
    for (L = L-1+M, R = R+1+M; L^R^1; L >>= 1, R >>= 1) {
        if (!(L&1)) sum += h[L^1].count(w) ? h[L^1][w] : 0;
        if (R&1) sum += h[R^1].count(w) ? h[R^1][w] : 0;
    }
    return sum;
}

int main(void) {
    #ifndef ONLINE_JUDGE
        freopen("count.in", "r", stdin);
        freopen("count.out", "w", stdout);
    #endif
    n = rd(), m = rd();
    for (M = 1; M < n+2; M <<= 1);
    F(i, 1, n)
        Modify(i, a[i] = rd(), 1);
    F(i, 1, m) {
        int k = rd();
        if (k == 1) {
            int x = rd(), w = rd();
            Modify(x, a[x], -1);
            Modify(x, a[x] = w, 1);
        }
        else {
            int l = rd(), r = rd(), w = rd();
            printf("%d\n", Query(l, r, w));
        }
    }
    return 0;
}

 

[原创题] count 分段Hash