zoukankan      html  css  js  c++  java
  • 暑假集训Day14 I (莫队)

    题目链接在这里:Problem - I - Codeforces

    应该是一个比较经典的莫队题,一开始想的是这个数据范围肯定是要搞一个前缀和,后来发现如果弄前缀和的话区间还是不好操作,但是如果一位一位的算的话还是可以的,所以想到了莫队。

    莫队要分块!!!不分块会T!

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef __int64 LL;
     4 const int MAX=2e6+6;
     5 LL n,t,a[MAX];
     6 LL L,R,ans[MAX],cnt[MAX];
     7 LL c[MAX];
     8 struct Node{
     9     LL l,r,no;
    10 }stu[MAX];
    11 bool cmp(Node x,Node y){
    12     if (c[x.l]!=c[y.l]) return x.l<y.l;
    13     return x.r<y.r;
    14 }
    15 int main(){
    16     freopen ("i.in","r",stdin);
    17     freopen ("i.out","w",stdout);
    18     LL i,j,an;
    19     scanf("%I64d%I64d",&n,&t);
    20     for (i=1;i<=n;i++) scanf("%I64d",a+i);
    21     for (i=1,j=(LL)sqrt(n);i<=n;i++) c[i]=i/j;
    22     for (i=1;i<=t;i++){
    23         scanf("%I64d%I64d",&stu[i].l,&stu[i].r);
    24         stu[i].no=i;
    25     }
    26     sort(stu+1,stu+t+1,cmp);
    27     L=1,R=an=0;
    28     memset(cnt,0,sizeof(cnt));
    29     for (i=1;i<=t;i++){
    30         //cout<<stu[i].l<<' '<<stu[i].r<<endl;
    31         while (R<stu[i].r){
    32             R++;an+=(cnt[a[R]]*2+1)*a[R];
    33             cnt[a[R]]++;
    34         }//cout<<an<<" fuck"<<endl;
    35         while (L>stu[i].l){
    36             L--;an+=(cnt[a[L]]*2+1)*a[L];
    37             cnt[a[L]]++;
    38         }
    39         while (R>stu[i].r){
    40             an-=((cnt[a[R]]-1)*2+1)*a[R];
    41             cnt[a[R]]--;
    42             R--;
    43         }
    44         while (L<stu[i].l){
    45             an-=((cnt[a[L]]-1)*2+1)*a[L];
    46             cnt[a[L]]--;
    47             L++;
    48         }
    49         ans[stu[i].no]=an;
    50     }
    51     for (i=1;i<=t;i++) printf("%I64d
    ",ans[i]);
    52     return 0;
    53 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    Linux-文件目录管理
    20. 有效的括号
    242. 有效的字母异位词
    387. 字符串中的第一个唯一字符
    136. 只出现一次的数字
    14. 最长公共前缀
    268. 丢失的数字
    169. 多数元素
    26. 删除有序数组中的重复项
    283. 移动零
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/15073044.html
Copyright © 2011-2022 走看看