NBUT 1457 分块法
来源:互联网 发布:vb打印字符串 编辑:程序博客网 时间:2024/05/18 20:48
[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 ancientscore(乐谱). But because it's too long,Sona can't play it in a short moment. SoSona 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.
- 输入
- 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.
- 样例输入
81 1 3 1 3 1 3 341 83 85 65 5
- 样例输出
1287221
- 提示
无
- 来源
XadillaX
- 操作
题意很纠结,看了好久,果然英语水平不足,给定一串数字,询问区间所有数出现次数的立方和。
看莫队算法,学会了分块爆搞。
代码:
/* ***********************************************Author :rabbitCreated Time :2014/3/12 16:40:57File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const ll maxn=100100;ll S[maxn],col[maxn],pos[maxn],a[maxn];struct Query{ll l,r,a,id;}pp[maxn];bool cmp1(Query a,Query b){return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&a.r<b.r);}bool cmp2(Query a,Query b){return a.id<b.id;}void update(ll pos,ll &ans,ll add){ans=ans-S[col[pos]]*S[col[pos]]*S[col[pos]];S[col[pos]]+=add;ans=ans+S[col[pos]]*S[col[pos]]*S[col[pos]];}int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); ll n,m; while(~scanf("%lld",&n)){ for(ll i=0;i<n;i++)scanf("%lld",&a[i]),col[i+1]=a[i]; sort(a,a+n); ll ss=unique(a,a+n)-a; for(ll i=1;i<=n;i++)col[i]=lower_bound(a,a+ss,col[i])-a+1; memset(S,0,sizeof(S)); scanf("%lld",&m); for(ll i=1;i<=m;i++)scanf("%lld%lld",&pp[i].l,&pp[i].r),pp[i].id=i; ll limit=(ll)sqrt(n+0.5); for(ll j=1;j<=n;j++)pos[j]=(j-1)/limit+1; sort(pp+1,pp+m+1,cmp1);ll ans=0; for(ll i=1,l=1,r=0;i<=m;i++){ if(r<pp[i].r){ for(r=r+1;r<=pp[i].r;r++)update(r,ans,1);r--; } if(r>pp[i].r)for(;r>pp[i].r;r--)update(r,ans,-1); if(l<pp[i].l)for(;l<pp[i].l;l++)update(l,ans,-1); if(l>pp[i].l){ for(l=l-1;l>=pp[i].l;l--)update(l,ans,1);l++; } pp[i].a=ans; } sort(pp+1,pp+m+1,cmp2); for(int i=1;i<=m;i++)printf("%lld\n",pp[i].a); } return 0;}
- NBUT 1457 分块法
- NBUT 1457 分块
- NBUT 1457 Sona 莫队算法 分块处理
- NBUT 1457 Sona
- NBUT
- NBUT
- NBUT
- NBUT
- NBUT
- 【NBUT
- NBUT
- NBUT
- NBUT
- NBUT
- NBUT
- NBUT 1457 Sona (莫队算法)
- Sona - NBUT 1457 莫队算法
- NBUT 1457 Sona [简单莫队]
- 09____java线程同步工具类之信号灯(Semaphore)
- C++ 自定义迭代器
- 写给Python初学者的设计模式入门
- C#生成Guid
- Integer和BigDecimal转换
- NBUT 1457 分块法
- UVa 10066 - The Twin Towers
- iPhone消息推送机制实现与探讨
- UITableView 滚动流程性优化
- Java2的集合框架主要的三种:List、Set和Map
- ns-leach.tcl
- 游戏
- Android自定义适配器,BaseAdapter的使用方法
- 打发v吧