CodeForces 671C - Ultimate Weirdness of an Array
题意:
给以一个定义, F(l, r) 的值表示序列 A[1:n]的子序列
A[1....(l-1),(r+1)...n] 之中 任意两个数的最大公约数的最大值。
求 Sum=∑i=1N∑j=1N(F(i,j)),(i≠j)
思路:
英文题解
大致解释一下:
H[i] 表示 F(l, r) <= i 区间 (l,r) 的个数。
V[A[i]] = { b1, b2, .... bk }, 其中 A[i] % bx == 0
还有一个next 数组
设 next[j] = k, 表示 F(l, k) <= i, k 尽可能的小。
当然会有 k 不存在的时候, 则 next[j] = n+1;(为什么, 看下面)
这时候可以知道 :
H[i]=∑j=1N(n−next[j]+1)
右端为 next[j] , 左端则有 n-next[j]+1种选择。
Code:
#include <bits/stdc++.h>
#define lson l , m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
const LL maxn = 200000 + 131;
const LL MaxN = 200000;
struct node {
LL Max, Min;
LL Sum, Flag;
};
node Node[maxn<<2];
LL H[maxn], A[maxn], Idx[maxn];
LL L1[maxn], L2[maxn], R1[maxn], R2[maxn];
///SegmentTree
void PushUp(LL rt) {
Node[rt].Max = max(Node[rt<<1].Max, Node[rt<<1|1].Max);
Node[rt].Min = min(Node[rt<<1].Min, Node[rt<<1|1].Min);
Node[rt].Sum = Node[rt<<1].Sum + Node[rt<<1|1].Sum;
}
void PushDown(LL l, LL r, LL rt) {
LL m = (l + r) >> 1;
if(Node[rt].Flag)
{
Node[rt<<1].Flag = Node[rt<<1].Max = Node[rt<<1].Min = Node[rt].Flag;
Node[rt<<1|1].Flag = Node[rt<<1|1].Max = Node[rt<<1|1].Min = Node[rt].Flag;
Node[rt<<1].Sum = Node[rt].Flag * (m - l + 1);
Node[rt<<1|1].Sum = Node[rt].Flag * (r - m);
Node[rt].Flag = 0;
}
}
void Build(LL l, LL r, LL rt) {
Node[rt].Flag = 0;
if(l == r) {
Node[rt].Max = Node[rt].Min = Node[rt].Sum = l;
return ;
}
LL m = (l + r) >> 1;
Build(lson), Build(rson);
PushUp(rt);
}
void Update_section(LL L, LL R, LL val, LL l, LL r, LL rt) {
if(L > R) return ;
if(Node[rt].Min >= val) return ;
if(L <= l and r <= R and Node[rt].Max <= val) {
Node[rt].Flag = val;
Node[rt].Min = Node[rt].Max = val;
Node[rt].Sum = LL(r - l + 1) * LL(val);
return ;
}
PushDown(l, r, rt);
LL m = (l + r) >> 1;
if(L <= m) Update_section(L, R, val, lson);
if(R > m) Update_section(L, R, val, rson);
PushUp(rt);
}
/// Solve
int main() {
std::ios::sync_with_stdio(false);
LL n;
cin >> n;
for(LL i = 1; i <= n; ++i) {
cin >> A[i];
Idx[A[i]] = i;
}
/// Get l(1, b1) -> bk-1, (b1,b2) -> bk, (b2, n) -> n+1
for(LL i = 1; i <= MaxN; ++i)
{
for(LL j = i; j <= MaxN; j +=i)
if(Idx[j])
{
if(L1[i] == 0 or L1[i] > Idx[j]) L2[i] = L1[i], L1[i] = Idx[j];
else if(L2[i] == 0 or L2[i] > Idx[j]) L2[i] = Idx[j];
if(R1[i] < Idx[j]) R2[i] = R1[i], R1[i] = Idx[j];
else if(R2[i] < Idx[j]) R2[i] = Idx[j];
}
}
/// Build Tree
Build(1,n,1);
for(LL i = MaxN; i > 0; --i)
{
if(L1[i] != R1[i])
{
Update_section(1,L1[i],R2[i], 1, n, 1);
Update_section(L1[i]+1, L2[i], R1[i], 1, n, 1);
Update_section(L2[i]+1, n, n+1, 1, n, 1);
}
H[i] = LL(n*(n+1)) - Node[1].Sum;
//cout << H[i] << endl;
}
LL Ans = 0;
for(LL i = 1; i < MaxN; ++i)
Ans += i * (H[i+1]-H[i]);
cout << Ans <<endl;
return 0;
}
CodeForces 671C - Ultimate Weirdness of an Array的更多相关文章
- Codeforces 671C - Ultimate Weirdness of an Array(线段树维护+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 *2800 的 DS,不过还是被我自己想出来了 u1s1 这个 D1C 比某些 D1D 不知道难到什么地方去了 首先碰到这类问题我们肯定考 ...
- codeforces 671C Ultimate Weirdness of an Array 线段树+构造
题解上说的很清楚了,我照着写的,表示膜拜题解 然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn) #include <stdio.h> #inc ...
- Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)
看见$a_i\leq 200000$和gcd,就大概知道是要枚举gcd也就是答案了... 因为答案是max,可以发现我们很容易算出<=i的答案,但是很难求出单个i的答案,所以我们可以运用差分的思 ...
- 【CodeForces】671 C. Ultimate Weirdness of an Array
[题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...
- 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)}$ ...
- CF671C. Ultimate Weirdness of an Array
n<=200000个<=200000的数问所有的f(i,j)的和,表示去掉区间i到j后的剩余的数字中任选两个数的最大gcd. 数论日常不会.. 先试着计算一个数组:Hi表示f(l,r)&l ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
随机推荐
- Django(八)上:Model操作和Form操作
↑↑↑点上面的”+”号展开目录 Model和Form以及ModelForm简介 Model操作: 创建数据库表结构 操作数据库表 做一部分的验证 Form操作: 数据验证(强大) ModelForm ...
- 安装 VMware CentOS Xmanager Xshell
1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:https://www.centos.org/download/DVD ISO:普通光盘完整安装版镜像,可离线安装到计算机硬盘上, ...
- Java 异常体系(美团面试)
Java把异常作为一种类,当做对象来处理.所有异常类的基类是Throwable类,两大子类分别是Error和Exception. 系统错误由Java虚拟机抛出,用Error类表示.Error类描述的是 ...
- React多层级表单
因项目需要封装的组件,组件库使用的是Ant Design 用到了 Form组件 , 布局组件,表单控件 ,如果没有使用Ant Design,可以用rc-form代替,需要对组件中使用的表单控件和布局进 ...
- Git 操作命令
一.Git 基本配置 1.配置 命令:git config --global prop_name prop_value 如配置git用户名与邮箱: git config --global user. ...
- ADC采样工作原理详解
如何利用单片机的ADC模块(或者独立的ADC芯片)得到接入ADC管脚上的实际电压值?这个问题,是第一次接触ADC时候,大家都会遇到的问题.会读到什么值单片机会读到什么值?需要看一个特性,就是几位的AD ...
- DBCP 连接池
DBCP数据源 DBCP 是 Apache 软件基金组织下的开源连接池实现 导入maven包: <!-- dbcp连接池 --> <dependency> <groupI ...
- 第七周博客作业<西北师范大学|李晓婷>
1.助教博客链接:https://home.cnblogs.com/u/lxt-/ 2.本周应批作业0,实批作业0. 3.本周小结:本周我们助教开始准备团队项目题目,下周三之前将会进行作业提交.
- git最基础操作一
1. 从远端克隆:git clone -b 克隆的分支 git的地址( eg:git clone -b master https://gitee.com/test/test.git ) 2.提交修改的 ...
- BeanUtils 日期转换(本地格式yyyy-MM-dd)转换成date
1.BeanUtils工具的使用 1)beanUtils 可以便于对javaBean的属性进行赋值. 2)beanUtils 可以便于对javaBean的对象进行赋值. 3)beanUtils可以将一 ...