【CF】Sereja and Arcs
#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的更多相关文章
- 【CF】223 Div.1 C Sereja and Brackets
水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...
- 【CF】438E. The Child and Binary Tree
http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...
- 【CF】148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...
- 【CF】328 D. Super M
这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...
- 【CF】323 Div2. D. Once Again...
挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...
- 【CF】7 Beta Round D. Palindrome Degree
manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...
- 【CF】86 B. Petr#
误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解).后来发现是求set(all vali ...
- 【CF】121 Div.1 C. Fools and Roads
题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...
- 【CF】310 Div.1 C. Case of Chocolate
线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...
随机推荐
- JS 模仿京东键盘输入内容
css代码 .search { width: 300px; height: 80px; margin: 0 auto; position: relative; } .con { display: no ...
- 解决drf_yasg中的SwaggerAPI无法正确分组问题
swagger是后台开发中很好用的交互式文档,Django原本的Django-Swagger已经停止维护了,现在一般用drf_yasg这个包来实现文档,它里面支持swagger和redoc两种,red ...
- 【源码】RocketMQ如何实现获取指定消息
概要 消息查询是什么? 消息查询就是根据用户提供的msgId从MQ中取出该消息 RocketMQ如果有多个节点如何查询? 问题:RocketMQ分布式结构中,数据分散在各个节点,即便是同一Topic的 ...
- 3.MongoDB恢复探究:为什么oplogReplay参数只设置了日志应用结束时间oplogLimit,而没有设置开始时间?
(一)问题 在使用MySQL数据库binlog日志基于时间点恢复数据库时,我们必须要指定binlog的开始位置和结束位置,而在MongoDB里面,如果使用oplog进行恢复,只有oplogLimit参 ...
- jQuery - AJAX笔记
@ 目录 什么是AJAX 关于 jQuery 与 AJAX jQuery AJAX 参考手册 jQuery ajax - ajax() 方法 定义和用法 语法 参数 options async bef ...
- JavaScript学习系列博客_20_JavaScript 作用域
作用域 - 作用域指一个变量的作用的范围 - 在JS中一共有两种作用域 1.全局作用域 - 直接编写在script标签中的JS代码,都在全局作用域- 全局作用域在页面打开时创建,在页面关闭时销毁 - ...
- 在laravel中遇到并发的解决方案
1,在mysql中创建唯一索引,在代码中try catch mysql的1062错误 2.将存在并发的代码丢给队列异步处理.这种解决方案的问题是,接下来的代码不能依赖队列的处理结果 3.使用mysql ...
- Tugnsten Fabric-MPLS-三层转发
1.网络拓扑图如下: 2.场景:虚机1.1.1.3 ping 虚机3.3.3.3(两个虚机加入到虚拟路由器里面了,所以可以互通) 3.查看虚机1.1.1.3所对应的VRF: 4.其中41为mpls标签 ...
- 表单和 v-model
思维导图 form 做表单一定要用 form+button组合 <template> <div id="app"> 登录 <form @submit. ...
- Clayff力场(转载)
本文转载自:http://blog.sina.com.cn/s/blog_b48a7ac30102w4km.html 注意:lammps中并没有完整的clayff势函数,需要将一下三者结合使用. an ...