CF 题目集锦 PART 7 #264 div 2 E
来源:互联网 发布:淘宝客服常用语 编辑:程序博客网 时间:2024/05/16 09:21
【原题】
Caisa is now at home and his son has a simple task for him.
Given a rooted tree with n vertices, numbered from 1 to n (vertex 1 is the root). Each vertex of the tree has a value. You should answer q queries. Each query is one of the following:
- Format of the query is "1 v". Let's write out the sequence of vertices along the path from the root to vertex v: u1, u2, ..., uk (u1 = 1; uk = v). You need to output such a vertex ui that gcd(value of ui, value of v) > 1 and i < k. If there are several possible vertices ui pick the one with maximum value of i. If there is no such vertex output -1.
- Format of the query is "2 v w". You must change the value of vertex v to w.
You are given all the queries, help Caisa to solve the problem.
The first line contains two space-separated integers n, q (1 ≤ n, q ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 2·106), where ai represent the value of node i.
Each of the next n - 1 lines contains two integers xi and yi (1 ≤ xi, yi ≤ n; xi ≠ yi), denoting the edge of the tree between vertices xi and yi.
Each of the next q lines contains a query in the format that is given above. For each query the following inequalities hold: 1 ≤ v ≤ n and 1 ≤ w ≤ 2·106. Note that: there are no more than 50 queries that changes the value of a vertex.
For each query of the first type output the result of the query.
4 610 8 4 31 22 33 41 11 21 31 42 1 91 4
-112-11
gcd(x, y) is greatest common divisor of two integers x and y.
【分析】这道题是做现场赛的。本来能A的,但是太紧张了=而且也不会用vector,边表搞的麻烦死了。
开始看到修改操作才50次、时间又松,真是爽!估计每次可以暴力重构这颗树,然后对于每个质因子记录最优值。
首先每次不能sqrt的效率枚举一个数的因子,我们可以预处理出每个数的所有质因子。(其实有更省空间的)
剩下来要解决的问题是:因为我是用dfs的,怎么把某个子树的信息在搜完后再去掉?(以免影响其他子树)HHD表示用vector一点也不虚。其实应该也可以用边表类似的思路,但是麻烦= =
【代码】
#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#define N 100005#define S 2000005#define push push_back#define pop pop_backusing namespace std;vector<int>fac[S],f[S];int data[N],ans[N],end[N],pf[S],deep[N];int C,cnt,n,Q,i,x,y,opt;struct arr{int go,next;}a[N*2];inline void add(int u,int v){a[++cnt].go=v;a[cnt].next=end[u];end[u]=cnt;}inline void init(){ int H=2000000; for (int i=2;i<=H;i++) if (!pf[i]) { for (int j=i;j<=H;j+=i) fac[j].push(i),pf[j]=1; }}void dfs(int k,int fa){ int P=data[k]; for (int i=0;i<fac[P].size();i++) { int go=fac[P][i],temp=f[go].size(); if (temp&&deep[f[go][temp-1]]>deep[ans[k]]) ans[k]=f[go][temp-1]; f[go].push(k); } for (int i=end[k];i;i=a[i].next) if (a[i].go!=fa) dfs(a[i].go,k); for (int i=0;i<fac[P].size();i++) f[fac[P][i]].pop();}inline void get_deep(int k,int fa){ for (int i=end[k];i;i=a[i].next) if (a[i].go!=fa) deep[a[i].go]=deep[k]+1,get_deep(a[i].go,k);}int main(){ scanf("%d%d",&n,&Q); for (i=1;i<=n;i++) scanf("%d",&data[i]); for (i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x); init();deep[0]=-1;get_deep(1,0); memset(ans,0,sizeof(ans));dfs(1,0); while (Q--) { scanf("%d%d",&opt,&x); if (opt==1) {printf("%d\n",ans[x]?ans[x]:-1);continue;} memset(ans,0,sizeof(ans)); scanf("%d",&y);data[x]=y;dfs(1,0); } return 0;}
- CF 题目集锦 PART 7 #264 div 2 E
- CF 题目集锦 PART 4 #258 div 2 E
- CF 题目集锦 PART 5 #266 div 2 E
- CF 题目集锦 PART 2 #260 div 1 C
- CF 题目集锦 PART 3 #262 div 2 D
- CF 题目集锦 PART 1 #138 div 1 A
- CF 题目集锦 PART 6 # 265 div 1 C
- 【MZ】CF 363E #211 (Div. 2)
- CF Round #254 (Div. 2) E
- Divisors - CF #256 (Div. 2) E (448E) 暴搜
- Cf #358 (Div. 2) E Alyona and Triangles (682E)
- CF div #151 E题
- CF 257Div.1 E (449E)
- 【MZ】CF 359C - 359E #209 (Div. 2)
- 【MZ】CF 358D - 358E #209 (Div. 2)
- CF 358D - 358E #209 (Div. 2)
- CF 359C - 359E #209 (Div. 2)
- cf Round #280 (Div. 2) E. Vanya and Field 数学
- 暑期开发过程中的一些经验记录
- 如果刚装好的ORACLE,却无法访问数据库,则把LINSTENER删除试试
- 【POJ3208】传说中POJ最难的数位DP?(正解AC自动机,二解数位DP,吾异与之)
- luaAPI解析
- Internal Patent Idea Candidates
- CF 题目集锦 PART 7 #264 div 2 E
- 根据MySQL表数据导出INSERT INTO语句的方法
- 百度编辑器ueditor的使用
- C++ const小结
- 浅谈在Tomcat中监控JVM内存使用情况
- 递归的练习——使用递归算法利用欧几里得算法,得出最大公约数
- Learning the 01 day for ios
- Flashloader简介
- osg中智能指针的使用