17多校6 HDU - 6102

题意:给一个排列p,m次查询l,r,\(\sum_{i=l}^r\sum_{j=i+1}^r\sum_{k=j+1}^r[gcd(p_i,p_j)==p_k]p_k\)
题解:离线,枚举右端点,对于每个数在i位置的数\(p_i\),考虑前面所有是\(p_i\)的倍数的位置,假设是\(t_1,t_2,...,t_x\)从后往前枚举,那么对于\(t_j\)来说,所有在\(t_j\)到\(i\)之间的位置,假设有k个位置和\(t_j\)gcd为\(a_i\),那么对于右端点在i,左端点在\(t_j\)左侧的查询,都要加上该贡献,就是\(k*a_i\),这里把贡献都加到树状数组上,对于求\(gcd(a_{t_j},a_x)=a_i\)的个数考虑反演,即\(\mu(p*a_i)*cnt_{p*a_i}\).在从后往前枚举的时候加到cnt中即可,复杂度\(O(nlog^2n)\)

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.in","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=500000+10,inf=0x3f3f3f3f;

vi f[N],fac[N];
int cnt[N],a[N],pos[N],mu[N];
struct bit{
    ll sum[N];
    int len;
    void init(int n)
    {
        len=n;
        for(int i=1;i<=n;i++)sum[i]=0;
    }
    void update(int i,ll v){for(;i<=len;i+=i&(-i))sum[i]+=v;}
    ll query(int i){ll ans=0;for(;i;i-=i&(-i))ans+=sum[i];return ans;}
}b;
vector<pii>q[N];
ll ans[N];
int main()
{
    mu[1]=1;
    for(int i=1;i<N;i++)
    {
        for(int j=i;j<N;j+=i)f[j].pb(i);
        for(int j=2*i;j<N;j+=i)mu[j]-=mu[i];
    }
    for(int i=1;i<N;i++)if(mu[i]!=0)for(int j=i;j<N;j+=i)fac[j].pb(i);
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,m;scanf("%d%d",&n,&m);
        b.init(n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[a[i]]=i,q[i].clear();
        for(int i=1;i<=m;i++)
        {
            int l,r;scanf("%d%d",&l,&r);
            q[r].pb(mp(l,i));
        }
        for(int i=1;i<=n;i++)
        {
            vi v;
            for(int j=2*a[i];j<=n;j+=a[i])if(pos[j]<i)v.pb(pos[j]);
            sort(v.begin(),v.end(),greater<int>());
            for(int x:v)
            {
                ll te=0;
                for(int y:fac[a[x]/a[i]])te+=cnt[y]*mu[y];
                te=te*a[i];
                b.update(1,te);b.update(x+1,-te);
                for(int y:f[a[x]/a[i]])cnt[y]++;
            }
            for(int x:v)for(int y:f[a[x]/a[i]])cnt[y]--;
            for(pii x:q[i])ans[x.se]=b.query(x.fi);
        }
        for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
    }
    return 0;
}
/********************

********************/

原文地址:https://www.cnblogs.com/acjiumeng/p/11156607.html

时间: 2024-11-13 08:41:17

17多校6 HDU - 6102的相关文章

HDU 6059 17多校3 Kanade&#39;s trio(字典树)

Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) satisfy that (i<j<k) and ((A[i] xor A[j])<(A[j] xor A[k])) There are T test cases. 1≤T≤20 1≤∑n≤5∗105 0≤A[i]<230 Input There is only one integer T on fi

判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

1 // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence 2 // 题意:三种操作,1增加值,2开根,3求和 3 // 思路:这题与HDU 4027 和HDU 5634 差不多 4 // 注意开根号的话,遇到极差等于1的,开根号以后有可能还是差1.如 5 // 2 3 2 3... 6 // 8 9 8 9... 7 // 2 3 2 3... 8 // 8 9 8 9... 9 // 剩下就是遇到区间相等的话,就直接开根号不往下传 10 11 12

多校训练hdu --Nice boat(线段树,都是泪)

Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 47 Accepted Submission(s): 10 Problem Description There is an old country and the king fell in love with a devil. The devil always ask

HDU 6045 17多校2 Is Derek lying?

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 687    Accepted Submission(s): 389 Problem Description Derek and Alfia are good friends.Derek 

HDU 6077 17多校4 Time To Get Up 水题

Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading the time on the clock, Little Q lies down and starts sleeping again. Well, he has 5 alarms, and it's just the first one, he can continue sleeping for a

HDU 6090 17多校5 Rikka with Graph(思维简单题)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For an undirected graph G with n nodes and m edges, we can define the distance between 

HDU 6060 17多校3 RXD and dividing(树+dfs)

Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T. he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,where ?Si={2,3,…,n} and for all d

HDU 6055 17多校 Regular polygon(计算几何)

Problem Description On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make. Input The input file consists of several test cases. Each case the first line is a numbers

HDU 6047 17多校 Maximum Sequence(优先队列)

Problem Description Steph is extremely obsessed with "sequence problems" that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one da