zoukankan      html  css  js  c++  java
  • [洛谷P2709]小B的询问

    题目大意:有一个序列$s_i$(最大的数为$k$),有$m$个询问,询问$[l,r]$中$sum_{i=1}^{k}c_i^2$($c_i$表示数字$i$在$[l,r]$中的出现次数)。

    题解:莫队

    卡点:为什么我奇偶性优化会锅???(后记:发现了,用异或时,若($a.l==b.l&&a.r==b.r$时可能会出现$a<b=true&&b<a=true$)

    C++ Code:

    #include <cstdio>
    #include <algorithm>
    #define bsz 224
    #define belong(i) ((i / bsz) + (i % bsz && 1))
    #define maxn 50010
    int n, m, k;
    int ans[maxn];
    int s[maxn], cnt[maxn];
    struct node {
        int l, r, num;
        inline bool operator < (const node &rhs) const {return (belong(l) == belong(rhs.l)) ? ((belong(l) & 1) ? r < rhs.r : r > rhs.r) : l < rhs.l;}
    } q[maxn];
    int main() {
        scanf("%d%d%d", &n, &m, &k);
        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].num = i;
        std::sort(q + 1, q + m + 1);
        int l = 1, r = 1, res = 1; cnt[s[1]]++;
        for (int i = 1; i <= m; i++) {
            while (l > q[i].l) res += cnt[s[--l]]++ << 1 | 1;
            while (r < q[i].r) res += cnt[s[++r]]++ << 1 | 1;
            while (l < q[i].l) res -= --cnt[s[l++]] << 1 | 1;
            while (r > q[i].r) res -= --cnt[s[r--]] << 1 | 1;
            ans[q[i].num] = res;
        }
        for (int i = 1; i <= m; i++) printf("%d
    ", ans[i]);
        return 0;
    }
    

      

  • 相关阅读:
    HDU 5918 SequenceI (2016 CCPC长春站 KMP模版变形)
    HDU 4585 Shaolin (set的应用)
    HDU 4329 MAP(stringstream的用法)
    CodeForces 698B Fix a Tree (并查集应用)
    UVALive 2520 Holedox Moving(BFS+状态压缩)
    UVA
    毛竹
    kmp
    博弈论
    最长回文子串
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9533812.html
Copyright © 2011-2022 走看看