NBUT 1457 分块法

  • [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 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.

  • 输入
  • 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.
  • 样例输入
  • 8
    1 1 3 1 3 1 3 3
    4
    1 8
    3 8
    5 6
    5 5
    
  • 样例输出
  • 128
    72
    2
    1
    
  • 提示
  • 来源
  • XadillaX
  • 操作


题意很纠结,看了好久,果然英语水平不足,给定一串数字,询问区间所有数出现次数的立方和。

看莫队算法,学会了分块爆搞。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/12 16:40:57
File 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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值