日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Sona(NBUT-1457)

發(fā)布時間:2025/3/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sona(NBUT-1457) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Problem 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 are 109 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 個數(shù)和 m 組詢問,對于每組詢問給出一個區(qū)間 [l,r],求區(qū)間內(nèi)每種數(shù)字出現(xiàn)次數(shù)的立方和

思路:普通莫隊+離散化

很基礎(chǔ)的普通莫隊,直接修改 O(1) 部分即可,由于數(shù)據(jù)范圍,需要進行離散化

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=10007; const int N=500000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std;struct Node{int l,r;//詢問的左右端點int id;//詢問的編號 }q[N]; int n,m,a[N],temp[N]; int block;//分塊 LL ans,cnt[N]; LL res[N];bool cmp(Node a,Node b){//奇偶性排序return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r); }void add(int x){//統(tǒng)計新的ans-=cnt[a[x]]*cnt[a[x]]*cnt[a[x]];cnt[a[x]]++;ans+=cnt[a[x]]*cnt[a[x]]*cnt[a[x]]; } void del(int x){//減去舊的ans-=cnt[a[x]]*cnt[a[x]]*cnt[a[x]];cnt[a[x]]--;ans+=cnt[a[x]]*cnt[a[x]]*cnt[a[x]]; } int main(){while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;++i){scanf("%d",&a[i]);temp[i]=a[i];}//離散化sort(temp+1,temp+1+n);int len=unique(temp+1,temp+1+n)-temp;for(int i=1;i<=n;i++)a[i]=lower_bound(temp+1,temp+1+len,a[i])-temp;scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}ans=0;memset(cnt,0,sizeof(cnt));block=n/sqrt(m*2/3*1.0);//分塊,防卡常數(shù)sort(q+1,q+m+1,cmp);//對詢問進行排序int l=1,r=0;//左右指針for(int i=1;i<=m;i++){int ql=q[i].l,qr=q[i].r;//詢問的左右端點while(l>ql) add(--l);//[l-1,r]while(l<ql) del(l++);//[l+1,r]while(r<qr) add(++r);//[l,r+1]while(r>qr) del(r--);//[l,r-1]res[q[i].id]=ans;//獲取答案}for(int i=1;i<=m;i++)printf("%lld\n",res[i]);}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的Sona(NBUT-1457)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。