Codeforces 86D Powerful array(莫队算法)

BZOJ2038差不多。。复习一下。

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 int block;
 6 struct Query{
 7     int i,l,r;
 8     bool operator<(const Query &q)const{
 9         if(l/block==q.l/block) return r<q.r;
10         return l/block<q.l/block;
11     }
12 }query[220000];
13 int cnt[1111111];
14 long long res,ans[220000];
15 void insert(long long x){
16     res-=x*cnt[x]*cnt[x];
17     ++cnt[x];
18     res+=x*cnt[x]*cnt[x];
19 }
20 void remove(long long x){
21     res-=x*cnt[x]*cnt[x];
22     --cnt[x];
23     res+=x*cnt[x]*cnt[x];
24 }
25 int a[220000];
26 int main(){
27     int n,t;
28     scanf("%d%d",&n,&t);
29     for(int i=1; i<=n; ++i) scanf("%d",a+i);
30     block=(int)sqrt(n);
31     for(int i=0; i<t; ++i){
32         query[i].i=i;
33         scanf("%d%d",&query[i].l,&query[i].r);
34     }
35     sort(query,query+t);
36     int l=1,r=1; cnt[a[1]]=1; res=a[1];
37     for(int i=0; i<t; ++i){
38         while(l<query[i].l) remove(a[l++]);
39         while(l>query[i].l) insert(a[--l]);
40         while(r>query[i].r) remove(a[r--]);
41         while(r<query[i].r) insert(a[++r]);
42         ans[query[i].i]=res;
43     }
44     for(int i=0; i<t; ++i){
45         printf("%I64d\n",ans[i]);
46     }
47     return 0;
48 }

 

转载于:https://www.cnblogs.com/WABoss/p/5252330.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值