Solution to a problem DTOJ # 1667 [small B inquiry (query)]

Welcome to My Luogu Space .


[Title] effect

A sequence, each query \ ([l, r] \ ) the square of the number of each digit, and the range of occurrence.

【answer】

Mo team .

Mo team title very template.

Launched \ ((the n-+ 1) ^ 2 ^ 2n = 2n + 1 \) (the n-number of occurrences to a number);
mean one more number will appear a number of answers to how much contribution caused.

Block into \ (\ sqrt {n} \ ) a.

[Code]

// output format !!
// long long !!
#include <bits/stdc++.h>
#define H puts("HYX")
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 50000+10;
int bol;
struct Q{
    int l, r, id;
    bool operator<(Q a)const{
        return (r/bol==a.r/bol) ? l<a.l : r<a.r;
    }
}q[MAXN];

int n, m, k;
int cnt[MAXN], s[MAXN];
LL ans, Ans[MAXN];

int main(){
    scanf("%d%d%d", &n, &m, &k);
    bol = sqrt(n);
    for(int i=1; i<=n; ++i) scanf("%d", s+i);
    for(int i=1; i<=m; ++i) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
    std::sort(q+1, q+m+1);
    int L = 1, R = 0;
    for(int i=1; i<=m; ++i){
        while(R-1 >= q[i].r) ans -= 1ll*cnt[s[R]]*2-1, --cnt[s[R--]];
        while(R+1 <= q[i].r) ans += 1ll*cnt[s[++R]]*2+1, ++cnt[s[R]];
        while(L+1 <= q[i].l) ans -= 1ll*cnt[s[L]]*2-1, --cnt[s[L++]];
        while(L-1 >= q[i].l) ans += 1ll*cnt[s[--L]]*2+1, ++cnt[s[L]];
        Ans[q[i].id] = ans;
    }
    for(int i=1; i<=m; ++i) printf("%lld\n", Ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/bosswnx/p/10988081.html