#include <bits/stdc++.h>
#define llong long long
using namespace std; const int N = 1e5;
const int P = 1e9+;
const llong INV2 = 5e8+;
int nxt[N+];
int lstpos[N+];
int a[N+];
int num[N+];
int cnum[N+];
llong tmp0[N+],tmp1[N+],tmp2[N+];
vector<int> clrpos[N+];
int n,m,B;
llong ans1,ans2a,ans2b,ans2c,ans0,ans; llong C2(llong x) {return x*(x-1ll)/2ll%P;}
llong update(llong &x,llong y) {x = (x+y)%P;} struct BITree
{
llong tr[N+]; int siz;
void addval(int lrb,llong val)
{
while(lrb<=siz)
{
update(tr[lrb],val);
lrb += (lrb&(-lrb));
}
}
llong querysum(llong rb)
{
llong ret = 0ll;
while(rb)
{
update(ret,tr[rb]);
rb -= (rb&(-rb));
}
return ret;
}
void clear()
{
for(int i=; i<=siz; i++) tr[i] = 0ll;
}
} bit1,bit2; void getans0()
{
llong cur = 0ll;
for(int i=; i<=m; i++)
{
llong tmp = C2(num[i]);
update(ans0,cur*tmp%P);
update(cur,tmp);
}
} void getans1()
{
llong tmp = 0ll;
for(int i=; i<=n; i++)
{
update(ans1,(tmp-C2(cnum[a[i]]))*(num[a[i]]-cnum[a[i]]-));
update(tmp,(llong)cnum[a[i]]);
cnum[a[i]]++;
}
} void getans2a()
{
bit1.siz = n; bit1.clear(); llong cur = 0ll;
for(int i=; i<=n; i++)
{
if(num[a[i]]<=B)
{
int tnum = ;
for(int j=nxt[i]; j; j=nxt[j])
{
llong tmp = cur-bit1.querysum(j)-C2(tnum)+P+P;
update(ans2a,tmp);
tnum++;
}
for(int j=nxt[i]; j; j=nxt[j])
{
cur++;
bit1.addval(j,);
}
}
}
} void getans2b()
{
for(int i=; i<=m; i++)
{
if(num[i]>B)
{
tmp1[] = 0ll; for(int j=; j<=n; j++) tmp1[j] = tmp1[j-]+(a[j]==i?:);
for(int j=; j<=m; j++)
{
if(num[j]<=B)
{
llong cur = 0ll;
for(int k=; k<clrpos[j].size(); k++)
{
int rb = clrpos[j][k];
llong tmp = (num[i]-tmp1[rb])*cur%P;
update(ans2b,tmp);
update(cur,tmp1[rb]);
}
}
}
}
}
} void getans2c()
{
for(int i=; i<=m; i++)
{
if(num[i]>B)
{
tmp1[] = ; for(int j=; j<=n; j++) tmp1[j] = tmp1[j-]+(a[j]==i?:);
for(int j=; j<=m; j++)
{
if(i==j) continue;
llong cur1 = 0ll,cur2 = 0ll;
for(int k=; k<clrpos[j].size(); k++)
{
int ra = clrpos[j][k];
llong tmp = tmp1[ra]*tmp1[ra]%P*k%P;
update(ans2c,tmp);
tmp = tmp1[ra]*(-2ll*cur1-k)%P+P;
update(ans2c,tmp);
tmp = cur2+cur1+P;
update(ans2c,tmp);
update(cur2,tmp1[ra]*tmp1[ra]);
update(cur1,tmp1[ra]);
}
}
}
}
ans2c = ans2c*INV2%P;
} int main()
{
scanf("%d",&n); B = sqrt(n)/;
for(int i=; i<=n; i++) scanf("%d",&a[i]),num[a[i]]++,m = max(m,a[i]),clrpos[a[i]].push_back(i);
for(int i=; i<=n; i++)
{
nxt[i] = lstpos[a[i]];
lstpos[a[i]] = i;
}
getans0();
getans1();
getans2a();
getans2b();
getans2c();
ans = ((ans0-ans1-ans2a-ans2b-ans2c)%P+P)%P;
printf("%lld\n",ans);
return ;
}

【CF】Sereja and Arcs的更多相关文章

  1. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  2. 【CF】438E. The Child and Binary Tree

    http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...

  3. 【CF】148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...

  4. 【CF】328 D. Super M

    这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...

  5. 【CF】323 Div2. D. Once Again...

    挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...

  6. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  7. 【CF】86 B. Petr#

    误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解).后来发现是求set(all vali ...

  8. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  9. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

随机推荐

  1. 使用Python从PDF文件中提取数据

    前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都应该了 ...

  2. JVM系列之:从汇编角度分析NullCheck

    目录 简介 一个普通的virtual call 普通方法中的null check 反优化的例子 总结 简介 之前我们在讲Virtual call的时候有提到,virtual call方法会根据传递的参 ...

  3. SpringBoot实现发送邮件

    1.QQ邮箱发送邮件设置 首先登录QQ邮箱>>>登录成功后找到设置>>>然后找到邮箱设置>>>点击账户>>>找到POP3|SMT ...

  4. javascript作用域、预解析笔记

    1.作用域     一般情况下,一段代码中所用到的名字并不总是有效可用的,     而限定这个名字(变量)的可用性的代码范围就是这个名字的作用域,可用有效的减少变量名冲突     2.js的作用域(e ...

  5. IDEA操作jdbc总结

    今天学习IDEA操作JDBC,MySQL包导入到项目 // 1.注册数据库的驱动// Driver driver=new com.mysql.jdbc.Driver();// DriverManage ...

  6. java 启动Tomcat报错:The specified JRE installation does not exist

    启动TomCat服务报错: The specified JRE installation does not exist 解决方法: Eclipse:window->perferences-> ...

  7. 洛谷P1149.火柴棒等式(暴力搜索)

    题目描述 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注 ...

  8. 《Java从入门到失业》第二章:Java环境(二):JDK、JRE、JVM

    2.2JDK.JRE.JVM 在JDK的安装目录中,我们发现有一个目录jre(其实如果是下一步下一步安装的,在和JDK安装目录同级目录下,还会有一个jre目录).初学Java的同学,有时候搞不清楚这3 ...

  9. package controllerutil

    原文链接:https://s0godoc0org.icopy.site/sigs.k8s.io/controller-runtime/pkg/controller/controllerutil imp ...

  10. 第5篇 Scrum 冲刺博客

    1.站立会议 照骗 进度 成员 昨日完成任务 今日计划任务 遇到的困难 钟智锋 完成技能 完全重构游戏逻辑代码,并编写调试模块 队友的代码已经和想法相去甚远 庄诗楷 制作了开始游戏的界面 进行了相关的 ...