题目链接:点击打开链接
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.
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; }