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 ...
随机推荐
- js作用域零碎的知识点,不同的script块,虽然同是全局变量
如下代码,第一次弹出a,因为解析器里找到var a,赋予a变量undefined,弹出undefined <!DOCTYPE html> <html> <head> ...
- C/C++音视频库ffmpeg的数据包AVPacket分析
ffmpeg下载地址 http://www.ffmpeg.club/ AVPacket是ffmpeg用来存放编码后的视频帧数据,我们来分析一下这个结构体,先贴出ffmpeg3.2中AVPacket声明 ...
- 如何用Electron Js创建第一个应用Hello World
什么是Electron Node.js和Chromium的结合品.允许只使用HTML,CSS和JavaScript来开发跨平台桌面应用. 编写第一个Electron程序(Hello World) 在开 ...
- CPU监控
题目描述 Bob需要一个程序来监视CPU使用率.这是一个很繁琐的过程,为了让问题更加简单,Bob会慢慢列出今天会在用计算机时做什么事. Bob会干很多事,除了跑暴力程序看视频之外,还会做出去玩玩和用鼠 ...
- python 向量化
study from : https://www.jianshu.com/p/ad8933dd6407
- mysql函数调用过程
1.conn = mysql_init(NULL);//初始化 MYSQL *conn; 2.mysql_real_connect(conn, "localhost", &quo ...
- 【JS】前端文件下载(无刷新)方法总结
#传统方法 利用iframe 或 form.submit 或 windows.open直接向后端发请求,后端返回文件流,后端处理成功后会直接返回到页面,浏览器会整理并打开自己的保存下载文件机制 . 1 ...
- 深入剖析Kubernetes学习笔记:容器基础(05-06)
05 :从进程说起 1.容器本身没有价值,有价值的是"容器编排" 2.什么是进程? 一旦"程序"被执行起来,它就从磁盘上的二进制文件,变成 1.计算机内存中的数 ...
- 目前的.NET 主流的后端ORM框架
转自:https://www.cnblogs.com/yeminglong/p/9518438.html 推荐一些常用的asp.net ORM框架 SqlSugar (国内) Dos.ORM (国内 ...
- H5_0007:使用base64做为背景图片
page { overflow:hidden; position:fixed; /* background-image:url('http://p0d5ombx1.bkt.clouddn.com/lo ...