题解上说的很清楚了,我照着写的,表示膜拜题解

然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn)

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 2e5+;
int mat[N],n,mx,tmp,lz[N<<],a[N<<];
LL c[N<<],sum,h[N];
vector<int>v[N];
void build(int rt,int l,int r){
if(l==r){
a[rt]=c[rt]=1ll*l;
return;
}
int m=(l+r)>>;
build(rt<<,l,m);
build(rt<<|,m+,r);
c[rt]=c[rt<<]+c[rt<<|];
a[rt]=min(a[rt<<],a[rt<<|]);
}
void down(int rt,int l,int r){
if(!lz[rt])return;
int m=(l+r)>>;
c[rt<<]=1ll*lz[rt]*(m-l+);
c[rt<<|]=1ll*lz[rt]*(r-m);
a[rt<<]=a[rt<<|]=a[rt];
lz[rt<<]=lz[rt<<|]=lz[rt];
lz[rt]=;
}
void modify(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y){
c[rt]=1ll*tmp*(r-l+);
a[rt]=lz[rt]=tmp;
return;
}
down(rt,l,r);
int m=(l+r)>>;
if(x<=m)modify(rt<<,l,m,x,y);
if(y>m)modify(rt<<|,m+,r,x,y);
c[rt]=c[rt<<]+c[rt<<|];
a[rt]=min(a[rt<<],a[rt<<|]);
}
int ask(int rt,int l,int r,int k){
if(l==r)return l;
int m=(l+r)>>;
if(a[rt<<|]<k)ask(rt<<|,m+,r,k);
else ask(rt<<,l,m,k);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i){
int x;scanf("%d",&x);
mat[x]=i;mx=max(mx,x);
}
for(int i=;i<=mx;++i){
for(int j=i;j<=mx;j+=i)
if(mat[j])
v[i].push_back(mat[j]);
}
for(int i=;i<=mx;++i)
if(v[i].size())sort(v[i].begin(),v[i].end());
build(,,n);
sum=1ll*n*(n+);
for(int i=mx;i>=;--i){
h[i]=sum-c[]; int k=v[i].size();
if(k<)continue;
tmp=n+;
if(v[i][]+<=n){
modify(,,n,v[i][]+,n);
}
tmp=v[i][k-];
if(a[]<tmp){
int pos=ask(,,n,tmp);
pos=min(pos,v[i][]);
if(pos>v[i][])
modify(,,n,v[i][]+,pos);
}
tmp=v[i][k-];
if(a[]<tmp){
int pos=ask(,,n,tmp);
pos=min(pos,v[i][]);
if(pos>)
modify(,,n,,pos);
}
}
LL ans=;
for(int i=;i<=mx;++i)
ans+=1ll*i*(h[i]-h[i-]);
printf("%I64d\n",ans);
return ;
}

codeforces 671C Ultimate Weirdness of an Array 线段树+构造的更多相关文章

  1. Codeforces 671C - Ultimate Weirdness of an Array(线段树维护+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 *2800 的 DS,不过还是被我自己想出来了 u1s1 这个 D1C 比某些 D1D 不知道难到什么地方去了 首先碰到这类问题我们肯定考 ...

  2. Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)

    看见$a_i\leq 200000$和gcd,就大概知道是要枚举gcd也就是答案了... 因为答案是max,可以发现我们很容易算出<=i的答案,但是很难求出单个i的答案,所以我们可以运用差分的思 ...

  3. 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 ...

  4. Codeforces 1114F Please, another Queries on Array? 线段树

    Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...

  5. Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]

    Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...

  6. 【CodeForces】671 C. Ultimate Weirdness of an Array

    [题目]C. Ultimate Weirdness of an Array [题意]给定长度为n的正整数序列,定义一个序列的价值为max(gcd(ai,aj)),1<=i<j<=n, ...

  7. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  8. 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)}$ ...

  9. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

随机推荐

  1. python解决汉诺塔问题

    今天刚刚在博客园安家,不知道写点什么,前两天刚刚学习完python 所以就用python写了一下汉诺塔算法,感觉还行拿出来分享一下 首先看一下描述: from :http://baike.baidu. ...

  2. myeclipse2013 for linux及其破解补丁百度网盘下载

    FQ下载1.1G的东西不是开玩笑的,用GA下载了两回均失败,还是用了某某门在win下下载好的,来之不易,所以特意上传分享给大家,免得FQ.破解文件也一并附上: 注意:本人这个是在原文件基础上bzip2 ...

  3. (转)[C++语法] 关键字typedef用法

    转自http://www.cnblogs.com/SweetDream/archive/2006/05/10/395921.html C/C++语言中的typedef相信大家已经不陌生,本文对C/C+ ...

  4. ZOJ Problem Set - 1002(DFS)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题意:给出一个n,有n*n大小的城市,(.)表示空地,从碉堡(O)射 ...

  5. ural 1200

    推出公式  然后特判两端  代码其实挺烂      但是有人竟然可以直接暴过去的 ...... #include <cstdio> #include <cstring> #in ...

  6. javascript closure

    http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...

  7. 快速排序(quicksort)算法实现

    快速排序(quicksort)是分治法的典型例子,它的主要思想是将一个待排序的数组以数组的某一个元素X为轴,使这个轴的左侧元素都比X大,而右侧元素都比X小(从大到小排序).然后以这个X在变换后数组的位 ...

  8. codeforces #310 div1 C

    操作无论是U还是L,都会使原图形分裂成两个图形,且两个图形的操作互不影响 我们又发现由于操作点只可能在下斜线上,如果将操作按x排序 那么无论是U还是L,都会将操作序列完整分割成两半,且两个操作序列互不 ...

  9. hhtml from表单为什么能提交数据

    1.html的列表,分为list,table,form. form表单是专门用来提交数据的,即上传数据的.所以form表单默认是必须有提交按钮的,也就是必须要有个button type类型为submi ...

  10. Servlet课程0425(四) Servlet实现简单用户登录验证

    Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...