E. Li Hua and Array

2023-10-11 14:20
文章标签 array li hua

本文主要是介绍E. Li Hua and Array,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem - E - Codeforces

思路:观察给定的函数,其实就是求与这个数互质的数的个数,即欧拉函数,我们发现一个数迭代欧拉函数不会很多,那么对于第一个操作来说我们可以直接暴力修改,而对于第二个操作来说,就是求l,r的最近公共祖先,那么我们可以用线段树维护区间的最近公共祖先,并且由于迭代的次数很少,所以并不需要建图,直接暴力跳跃求最近公共祖先即可,那么最总的答案就是用l到r的深度之和-最近公共祖先的深度,乘以区间长度,这就是把这每个点跳跃到最近公共祖先的花费

一定不要建图,建图会MLE

// Problem: E. Li Hua and Array
// Contest: Codeforces - Codeforces Round 864 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1797/E
// Memory Limit: 512 MB
// Time Limit: 3000 ms#include<bits/stdc++.h>
#include<sstream>
#include<cassert>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=5e6+7 ,M=6e7+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}int T,hackT;
int n,m,k;
int cost[N];
int pr[3000],cnt;
bool st[3000];
int vis[N];
int depth[N];
struct Node{int l,r;int start;int sum;int add;
};
Node tr[N*4];int lca(int a,int b) {while(a!=b) {if(a>b) a=vis[a];else b=vis[b];}return a;
}void pushup(int u) {tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;tr[u].add=tr[u<<1].add+tr[u<<1|1].add;tr[u].start=lca(tr[u<<1].start,tr[u<<1|1].start);
}void build(int u,int l,int r) {if(l==r) {if(depth[cost[l]]==0) tr[u]={l,r,cost[l],depth[cost[l]],1};else tr[u]={l,r,cost[l],depth[cost[l]],0};}else {tr[u]={l,r,INF,0,0};int mid=l+r>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);}
}void modify(int u,int l,int r) {if(tr[u].r-tr[u].l+1==tr[u].add) return ;if(tr[u].l==tr[u].r) {int tp=vis[tr[u].start];if(depth[tp]==0) tr[u]={tr[u].l,tr[u].r,tp,depth[tp],1};else tr[u]={tr[u].l,tr[u].r,tp,depth[tp],0}; }else {int mid=tr[u].l+tr[u].r>>1;if(l<=mid) modify(u<<1,l,r);if(r>mid) modify(u<<1|1,l,r);pushup(u);}
}int query_sum(int u,int l,int r) {if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum;else {int mid=tr[u].l+tr[u].r>>1;int res=0;if(l<=mid) res+=query_sum(u<<1,l,r);if(r>mid) res+=query_sum(u<<1|1,l,r);return res;}
}int query_lca(int u,int l,int r) {if(tr[u].l>=l&&tr[u].r<=r) return tr[u].start;else {int mid=tr[u].l+tr[u].r>>1;if(l<=mid&&r>mid) {int a=query_lca(u<<1,l,r);int b=query_lca(u<<1|1,l,r);return lca(a,b);}else if(l<=mid) return query_lca(u<<1,l,r);else return query_lca(u<<1|1,l,r);}
}void get(int x) {int tx=x;int res=x;for(int i=0;i<cnt&&pr[i]<=x/pr[i];i++) {if(x%pr[i]==0) {res=res/pr[i]*(pr[i]-1);while(x%pr[i]==0) x/=pr[i];}}if(x!=1) res=res/x*(x-1);vis[tx]=res;depth[tx]=depth[res]+1;
}void init() {int t=sqrt(5000000);for(int i=2;i<=t;i++) {if(!st[i]) pr[cnt++]=i;for(int j=0;pr[j]<=t/i;j++) {st[pr[j]*i]=true;if(i%pr[j]==0) break;}}depth[1]=0;for(int i=2;i<=5000000;i++) get(i);
}void solve() {n=read(),m=read();for(int i=1;i<=n;i++) cost[i]=read();init();build(1,1,n);while(m--) {int op=read();int l=read(),r=read();if(op==1) {modify(1,l,r);}else if(op==2) {int s=query_lca(1,l,r);int sum=query_sum(1,l,r);printf("%d\n",sum-depth[s]*(r-l+1));}}
}   int main() {// init();// stin();// ios::sync_with_stdio(false); // scanf("%d",&T);T=1; while(T--) hackT++,solve();return 0;       
}          

这篇关于E. Li Hua and Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/188668

相关文章

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

Lenze伦茨EMF2102IBC−LECOM−A/B/LI L−force Communication手测

Lenze伦茨EMF2102IBC−LECOM−A/B/LI L−force Communication手测

做一个问卷考试,标准答案对比用户填写的答案,array_diff 进行差集比对

if( empty(array_diff($answer_mark, $answer)) && empty(array_diff( $answer,$answer_mark))){//用户答题正确}else{// 答题错误} 做一个问卷考试,标准答案对比用户填写的答案,array_diff  进行差集比对   如用户填写的答案变量为answer   标准答案为answer_mark

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all

[LeetCode] 215. Kth Largest Element in an Array

题:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not th

MyBatis - 使用foreach迭代List/Array的说明

在 MyBatis 中的 foreach 元素,主要用于迭代 集合数据 以动态生成执行语句;主要有 item、index、collection、open、separator、close 等属性 属性说明         collection:要迭代的数据集对象,必填项         item:迭代出的元素的别名,必填项         index:元素的序号(map时为k

LeetCode - 33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个数组,这个数组是由两个有序的数组拼接成的(无重复元素),在这个数组中查找元素k的下标. anal

[置顶]后缀数组(suffix array)详解

写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具。 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料。 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, 能够实现后缀树的很多功能而时间复杂度也不太逊色,并且,它比后缀树所占用的空间小很多。 可以说,在信息学竞赛中后缀数组比后缀树要更为实用! 因此在本文中笔者想介绍一下后缀数组的基本概念、构造

【CodeForces】266E More Queries to Array... 线段树

E. More Queries to Array... time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output You've got an array, consisting of n