zoukankan      html  css  js  c++  java
  • 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
    ",ans[i]);
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    正则表达式
    Ajax跨域问题---jsonp
    Ajax
    字符串总结
    js 字符串加密
    jsDate()
    HDU 5430 Reflect
    HDU 5429 Geometric Progression
    HDU 5428 The Factor
    POJ 2485 Highways
  • 原文地址:https://www.cnblogs.com/WABoss/p/5252330.html
Copyright © 2011-2022 走看看