NBUT 1457 (莫队算法 离散化)

题目链接:点击打开链接

  • [1457] Sona

  • 时间限制: 5000 ms 内存限制: 65535 K
  • 问题描述
  • Sona Maven of the Strings . Of cause, she can play the zither.

    Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.

    There're an ancient score(乐谱). But because it's too long, Sona can't play it in a short moment. So Sona decide to just play a part of it and revise it.

    A score is composed of notes. There are 109 kinds of notes and a score has105 notes at most.

    To diversify Sona's own score, she have to select several parts of it. The energy of each part is calculated like that:

    Count the number of times that each notes appear. Sum each of the number of times' cube together. And the sum is the energy.

    You should help Sona to calculate out the energy of each part.

  • 输入
  • This problem contains several cases. And this problem provides 2 seconds to run.
    The first line of each case is an integer N (1 ≤ N ≤ 10^5), indicates the number of notes.
    Then N numbers followed. Each number is a kind of note. (1 ≤ NOTE ≤ 10^9)
    Next line is an integer Q (1 ≤ Q ≤ 10^5), indicates the number of parts.
    Next Q parts followed. Each part contains 2 integers Li and Ri, indicates the left side of the part and the right side of the part.
  • 输出
  • For each part, you should output the energy of that part.
  • 样例输入
  • 8
    1 1 3 1 3 1 3 3
    4
    1 8
    3 8
    5 6
    5 5
    
  • 样例输出
  • 128
    72
    2
    1

题意:求出每一个区间的能量值,能量值等于所有数字出现次数的三次方之和.

很显然离散化之后记录下每一个值出现的次数就能O(1)转移了,删除增加不难想.

特别坑的是要用__int64,好像n个分数也要用__int64.

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 111111

int pos[maxn];
int n, m, k;
__int64 cnt[maxn];
__int64 ans[maxn], tmp[maxn];
struct node {
    int l, r, id;
    bool operator < (const node &a) const {
        return pos[l] < pos[a.l] || (pos[l] == pos[a.l] && r < a.r);
    }
} p[maxn];
struct reflect {
    __int64 num;
    int id, pos;
    bool operator < (const reflect &a) const {
        return num < a.num;
    }
}a[maxn];
__int64 cur;

__int64 f (__int64 x) {
    return x*x*x;
}

void add (int pos) {
    cur -= f (cnt[a[pos].id]);
    cnt[a[pos].id]++;
    cur += f (cnt[a[pos].id]);
}

void del (int pos) {
    cur -= f (cnt[a[pos].id]);
    cnt[a[pos].id]--;
    cur += f (cnt[a[pos].id]);
}

bool cmp (const reflect &a, const reflect &b) {
    return a.pos < b.pos;
}

int main () {
    //freopen ("in.txt", "r", stdin);
    while (scanf ("%d", &n) == 1) {
        for (int i = 1; i <= n; i++) {
            scanf ("%I64d", &a[i].num);
            a[i].pos = i;
        }
        sort (a+1, a+1+n);
        a[1].id = 1;
        for (int i = 2; i <= n; i++) {
            if (a[i].num > a[i-1].num) a[i].id = a[i-1].id+1;
            else a[i].id = a[i-1].id;
        }
        sort (a+1, a+1+n, cmp);
        int block = ceil (sqrt (n*1.0));
        for (int i = 1; i <= n; i++) pos[i] = (i-1)/block;
        scanf ("%d", &m);
        for (int i = 0; i < m; i++) {
            scanf ("%d%d", &p[i].l, &p[i].r);
            p[i].id = i;
        }
        sort (p, p+m);
        int l = 1, r = 1;
        cur = 0;
        memset (cnt, 0, sizeof cnt);
        add (1);
        for (int i = 0; i < m; i++) {
            while (r > p[i].r) {
                del (r);
                r--;
            }
            while (r < p[i].r) {
                r++;
                add (r);
            }
            while (l > p[i].l) {
                l--;
                add (l);
            }
            while (l < p[i].l) {
                del (l);
                l++;
            }
            ans[p[i].id] = cur;
        }
        for (int i = 0; i < m; i++) {
            printf ("%I64d\n", ans[i]);
        }
    }
    return 0;
}


你可能感兴趣的:(NBUT 1457 (莫队算法 离散化))