NBUT 1457 莫队算法 离散化

Sona
Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:
Appoint description: 

Description

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 are109 kinds of notes and a score has 105 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.

Input

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.

Output

For each part, you should output the energy of that part.

Sample Input

8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5

Sample Output

128
72
2
1
 
题意:
n个数,q次查询,区间内每个数字出现的次数的三次方的和。
思路:
莫队算法,这题卡时间很厉害,map会TLE的。由于n最多只有10^5,又我们的统计是数出现的次数,所以我们不必需要真正的数,所以可以离散化,统计的时候,只要累加这个数对应的位置即可。
 
/*
 * Author:  sweat123
 * Created Time:  2016/7/15 8:25:26
 * File Name: main.cpp
 */
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = 100010;
struct node{
    int l,r,id;
}q[MAXN];
int a[MAXN],n,m,pos[MAXN],b[MAXN],k,p[MAXN];
ll ret,ans[MAXN];
int mp[MAXN];
bool cmp(node a,node b){
    if(pos[a.l] == pos[b.l])return a.r < b.r;
    return pos[a.l] < pos[b.l];
}
ll power(int x){
    return 1LL * x * x * x;
}
int getkey(int x){
    int l,r,m,ans;
    l = 1,r = k - 1;
    while(l <= r){
        m = (l + r) >> 1;
        if(b[m] == x)return m;
        else if(b[m] > x) r = m - 1;
        else l = m + 1;
    }
}
void init(){
    sort(b+1,b+n+1);
    k = 2;
    for(int i = 2; i <= n; i++){
        if(b[i] != b[i-1]){
            b[k++] = b[i];
        }
    }
    for(int i = 1; i <= n; i++){
        int tp = getkey(a[i]);
        p[i] = tp;
    }
}
void updata(int x,int val){
    ret -= power(mp[p[x]]);
    mp[p[x]] += val;
    ret += power(mp[p[x]]);
}
int main(){
    while(~scanf("%d",&n)){
        int tp = (int)ceil(sqrt(n * 1.0));
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
            b[i] = a[i];
            pos[i] = (i - 1) / tp;
        }
        init();
        memset(mp,0,sizeof(mp));
        scanf("%d",&m);
        for(int i = 1; i <= m; i++){
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id = i;
        }
        sort(q+1,q+m+1,cmp);
        int pl,pr;
        pl = 1;
        pr = 0;
        ret = 0;
        for(int i = 1; i <= m; i++){
            int id = q[i].id;
            if(q[i].l == q[i].r){
                ans[id] = 1;
                continue;
            } else {
                if(pr <= q[i].r){
                    for(int j = pr + 1; j <= q[i].r; j++){
                        updata(j,1);
                    }
                } else{
                    for(int j = pr; j > q[i].r; j--){
                        updata(j,-1);
                    }
                }
                pr = q[i].r;
                if(pl < q[i].l){
                    for(int j = pl; j < q[i].l; j++){
                        updata(j,-1);
                    }
                } else{
                    for(int j = pl - 1; j >= q[i].l; j--){
                        updata(j,1);
                    }
                }
                pl = q[i].l;
                ans[id] = ret;
            }
        }
        for(int i = 1; i <= m; i++){
            printf("%I64d
",ans[i]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sweat123/p/5672614.html