Nbut 1457 Sona algorithm block processing

Source: Internet
Author: User
Tags ord

[1457] Sona time limit: Ms Memory limit: 65535 K 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 (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 is 109 kinds of notes and a score has105 notes at the most.

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

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

You should help Sona-calculate out the energy of each part. Enter this problem contains several cases. And this problem provides 2 seconds to run.
The first line of all case was 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 was 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 power of that. Sample input

8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5
Sample output
2
1
Tips
No
Source
Xadillax

Mo Team algorithm, refer to my previous blog. or search my "Mo team Algorithm" article.

For each query [L,r] Press the L block, each sqrt (n), and the same block is incremented by R.

The LL,RR pointer marks the processing interval, and the transfer is O (1). First, the number of occurrences of the number is discretized by a single array.

If the number of times plus 1 ans = ans + num[i]*num[i]-(Num[i]) -1* (num[i]-1)

Add an input plug-in faster 900+ms, do not. 1250ms. It's pretty fast.

#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <
Math.h> using namespace std;
#define MAXN 100007 #define LL Long long int NUM[MAXN],COLOR[MAXN],ORD[MAXN];
ll ANS[MAXN];
    struct node{int l,r,id;
ll ans;
};
Node QUERY[MAXN];
int squ;
    inline int Comp (const node &a,const node &b) {if (a.l/squ = = b.l/squ) return A.R < B.R;
return A.l/squ < B.l/squ;
    } int Getn () {int a = 0;
    char x;
        while (1) {x = GetChar ();
        if (x== ' | | x = = ' \ n ') return A;
    A = a*10+x-' 0 ';
}} ll PO3[MAXN];
    inline int compid (const node&a,const Node &b) {return a.id < b.id;} int main () {int n,m,q;
    for (int i = 0;i < MAXN; i++) po3[i] = 1ll*i*i*i;
        while (scanf ("%d", &n)!=eof) {GetChar ();
            for (int i = 1;i <= n; i++) {//scanf ("%d", &color[i]);
            Color[i] = GETN ();
       Ord[i] = Color[i]; } sort (ord+1,ord+n+1);
        int m = Unique (ord+1,ord+1+n)-(ord+1);
        for (int i = 1;i <= n; i++) Color[i] = Lower_bound (Ord+1,ord+1+m,color[i])-(ord+1);
        for (int i = 0;i <= m; i++) num[i] = 0;
        scanf ("%d", &q);
        GetChar ();
            for (int i = 0;i < Q; i++) {//scanf ("%d%d", &AMP;QUERY[I].L,&AMP;QUERY[I].R);
            QUERY[I].L = Getn ();
            QUERY[I].R = Getn ();
        Query[i].id = i;
        } squ = sqrt (n*1.0);
        Sort (query,query+q,comp);
        int ll=query[0].l,rr=query[0].l-1;
        ll res = 0;
        int x;
            for (int i = 0;i < Q; i++) {int id = query[i].id;
                while (RR < QUERY[I].R) {rr++;
                x = num[color[rr]]++;
                RES-= po3[x];
                x + +;
            Res + = Po3[x];
                } while (RR > QUERY[I].R) {x = num[color[rr]]--; RES-= po3[x];
                x--;
                Res + = Po3[x];
            rr--;
                } while (LL > Query[i].l) {ll--;
                x = num[color[ll]]++;
                RES-= po3[x];
                x + +;
            Res + = Po3[x];
                } while (LL < QUERY[I].L) {x = num[color[ll]]--;
                RES-= po3[x];
                x--;
                Res + = Po3[x];
            ll++;
        } Query[i].ans = res;
        } sort (query,query+q,compid);
        for (int i = 0;i < Q; i++) {printf ("%i64d\n", Query[i].ans);
}} return 0;

 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.