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 ...
随机推荐
- flutter 自定义主题切换
1. 定义local_srorage.dart文件 使用Flutter第三方插件shared_preferences实现存储键值对信息 相关shared_preferences插件可参考: flutt ...
- 佳文赏析:How to uninstall Linux
来源:https://www.dedoimedo.com/computers/linux-uninstall.html This article was suggested to me by a re ...
- 二、Redis安装
一.下载Redis: 下载地址:https://github.com/MSOpenTech/redis/releases. 由于redis并不支持window系统,而window版本的redis的是由 ...
- document对象获取例子
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Nginx LOG阶段里log模块
L68 log_format 指令 syntax : name [escape =default|josn|none] string "...."; default : combi ...
- [LOJ3084][GXOI/GZOI2019]宝牌一大堆——DP
题目链接: [GXOI/GZOI2019]宝牌一大堆 求最大值容易想到$DP$,但如果将$7$种和牌都考虑进来的话,$DP$状态不好设,我们将比较特殊的七小对和国士无双单独求,其他的进行$DP$. 观 ...
- jmeter笔记(8)--关联
关联是jmeter中比较重要的一个点,在测试过程中有些数据是经常发生变化的,要获取这些数据,就需要使用关联,Jmeter可以通过“后置处理器”中的“正则表达式提取器”来处理关联.. 正则表达式提取器 ...
- Sublime Text 3 安装简记
1.下载:( Sublime Text Version 3.1.1 Build 3176 ) https://www.sublimetext.com/3 2.安装Package Control: &q ...
- python之路day10-命名空间和作用域、函数嵌套,作用域链、闭包
楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return mbigger = my_max(10,20)print ...
- Linux系统网络编程中TCP通讯socket--send导致进程被关闭
https://blog.csdn.net/dsanmux/article/details/52083403 https://blog.csdn.net/u011425939/article/deta ...