問題の解決策DTOJの#1667 [小さなBの問い合わせ(クエリ)]

ようこそ私Luoguスペース


[タイトル]効果

シーケンス、各クエリ\([L、R] \ ) 各桁の数の二乗、及び発生の範囲。

[説明]

Moのチーム

Moのチームのタイトルは非常にテンプレート。

打ち上げ\((N + 1)^ 2 ^ 2N = 2N + 1 \) 数に出現n個);
多くの寄与が原因とどのように回答数を表示します1つの以上の数を意味します。

ブロック\(\のSQRT {N} \ ) 。

[コード]

// 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;
}

おすすめ

転載: www.cnblogs.com/bosswnx/p/10988081.html