Ultimate Weirdness of an Array CodeForces - 671C (gcd,线段树)
大意: 定义一个数列的特征值为两个数gcd的最大值, $f(l,r)$表示数列删除区间$[l,r]$的元素后剩余元素的特征值, 求$\sum_{i=1}^n\sum_{j=i}^n{f(i,j)}$
怎么这div.1的C怎么这么难.....好像D过的人比C还要多.
特征值不好处理, 考虑将贡献转为前缀
即转化为对于所有的$x$, 求出$H[x]=\space f(l,r)\le x$的个数, 显然$H[x]$是单调不减的.
记$next[x]_l=\space f(l,r)\le x$的最小$r$, 就有$H[x]=\sum\limits_{i=1}^n(n+1-next[x]_i)$
显然$next[x]$是单调不增的, 考虑如何维护$next$.
我们从大到小枚举$x$, 这样初始$next[x]_i=i$, 考虑$x$变为$x-1$时, 每个位置的$next$值如何变化.
可以先枚举$gcd$, 预处理出$a_i|gcd$的位置$i$, 假设为$v_1,v_2,...,v_{m-1},v_{m}$.
那么显然对于$[v_2+1,n]$位置的$next$值与$n+1$取最大.
对于$[v_1+1,v_2]$位置的$next$值与$v_m$取最大.
对于$[1,v_1]$位置的$next$值与$v_{m-1}$取最大.
所以就需要支持区间取最大值, 区间求和, 可以用$STL$的$set$或线段树实现, 复杂度都是$O(nlog^2n)$
#include <iostream>
#include <algorithm>
#include <cstdio>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (lc|1)
#define ls lc,l,mid
#define rs rc,mid+1,r
using namespace std;
typedef long long ll; const int N = 1e6+10, INF = 0x3f3f3f3f;
int n, mx, a[N];
struct {
int ma,mi,tag;
ll sum;
void upd(int x, int t) {
ma=mi=tag=x, sum=(ll)t*x;
}
} tr[N<<2];
int L1[N], L2[N], R1[N], R2[N];
ll H[N]; void pu(int o) {
tr[o].ma = max(tr[lc].ma,tr[rc].ma);
tr[o].mi = min(tr[lc].mi,tr[rc].mi);
tr[o].sum = tr[lc].sum+tr[rc].sum;
}
void pd(int o, int l, int r) {
if (tr[o].tag) {
tr[lc].upd(tr[o].tag,mid-l+1);
tr[rc].upd(tr[o].tag,r-mid);
tr[o].tag = 0;
}
}
void build(int o, int l, int r) {
if (l==r) return tr[o].upd(l,1);
build(ls),build(rs);
pu(o);
}
void update(int o, int l, int r, int ql, int qr, int v) {
if (ql>qr||tr[o].mi>=v) return;
if (ql<=l&&r<=qr&&tr[o].ma<=v) return tr[o].upd(v,r-l+1);
pd(o,l,r);
if (mid>=ql) update(ls,ql,qr,v);
if (mid<qr) update(rs,ql,qr,v);
pu(o);
} int main() {
scanf("%d", &n);
REP(i,1,n) {
int t;
scanf("%d", &t);
a[t] = i;
mx = max(mx, t);
}
REP(i,1,mx) {
for (int j=i; j<=mx; j+=i) if (a[j]) {
if (!L1[i]||L1[i]>a[j]) L2[i] = L1[i], L1[i] = a[j];
else if (!L2[i]||L2[i]>a[j]) L2[i] = a[j];
if (R1[i]<a[j]) R2[i] = R1[i], R1[i] = a[j];
else if (R2[i]<a[j]) R2[i] = a[j];
}
}
build(1,1,n);
PER(i,1,mx) {
if (L1[i]!=R1[i]) {
update(1,1,n,1,L1[i],R2[i]);
update(1,1,n,L1[i]+1,L2[i],R1[i]);
update(1,1,n,L2[i]+1,n,n+1);
}
H[i] = (ll)(n*(n+1))-tr[1].sum;
}
ll ans = 0;
REP(i,1,mx-1) {
ans += (ll)i*(H[i+1]-H[i]);
}
printf("%lld\n", ans);
}
Ultimate Weirdness of an Array CodeForces - 671C (gcd,线段树)的更多相关文章
- G - Greg and Array CodeForces - 296C 差分+线段树
题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...
- 【CodeForces】671 C. Ultimate Weirdness of an Array
[题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...
- Codeforces 671C - Ultimate Weirdness of an Array(线段树维护+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 *2800 的 DS,不过还是被我自己想出来了 u1s1 这个 D1C 比某些 D1D 不知道难到什么地方去了 首先碰到这类问题我们肯定考 ...
- Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)
看见$a_i\leq 200000$和gcd,就大概知道是要枚举gcd也就是答案了... 因为答案是max,可以发现我们很容易算出<=i的答案,但是很难求出单个i的答案,所以我们可以运用差分的思 ...
- CF671C. Ultimate Weirdness of an Array
n<=200000个<=200000的数问所有的f(i,j)的和,表示去掉区间i到j后的剩余的数字中任选两个数的最大gcd. 数论日常不会.. 先试着计算一个数组:Hi表示f(l,r)&l ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树
E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 1076G - Array Game(博弈论+线段树)
Codeforces 题面传送门 & 洛谷题面传送门 一道 hot tea--听讲解时半懂不懂因为不知道题目意思,最后终究还是琢磨出来了( 首先注意到对于每个 \(a_i\),它具体是什么并不 ...
- F - One Occurrence CodeForces - 1000F (线段树+离线处理)
You are given an array aa consisting of nn integers, and qq queries to it. ii-th query is denoted by ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
随机推荐
- mysql函数之四:concat() mysql 多个字段拼接
语法: COUNT(DISTINCT expr ,[expr ...]) 函数使用说明:返回不同的非NULL 值数目.若找不到匹配的项,则COUNT(DISTINCT) 返回 0 Mysql的查询结果 ...
- python3.4学习笔记(十六) windows下面安装easy_install和pip教程
python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...
- python之路----hashlib模块
在平时生活中,有很多情况下,你在不知不觉中,就用到了hashlib模块,比如:注册和登录认证注册和登录认真过程,就是把注册用的账户密码进行:加密 --> 解密 的过程,在加密.解密过程中,用的了 ...
- MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
INDEX UNION Rules WHERE VS. UNION UNION VS. UNION ALL Sorting Combined Query Results UNION Rules As ...
- 20145332 拓展:注入shellcode实验
20145332卢鑫 拓展:注入shellcode实验 shellcode基础知识 Shellcode实际是一段代码(也可以是填充数据),是用来发送到服务器利用特定漏洞的代码,一般可以获取权限.另外, ...
- linux下如何查看当前机器提供了哪些服务
答:使用netstat工具 在命令行下输入netstat -atun即可列出当前机器提供的服务 netstat各选项解析: -a 列出所有服务 -t 列出tcp相关 -u 列出udp相关 -n 以数字 ...
- P3386 【模板】二分图匹配 -网络流版
二分图匹配 题目背景 二分图 感谢@一扶苏一 提供的hack数据 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+ ...
- JavaScript 开闭原则OCP
代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- 'curl' is not recognized as an internal or external command
使用everything搜索本地的curl.exe发现如下 官网查看最新版本https://curl.haxx.se/windows/ 2019-03-06 最新版本7.64.0 curl-7.64. ...
- git源码阅读
https://github.com/git-for-windows/git/issues/1854 https://github.com/git-for-windows/git/pull/1902/ ...