题目链接

给一棵树, 两种操作, 一种是将点u的权值改为y, 另一种是查询根节点到点u的路径上, gcd(v, u)>1的深度最深的点v。 修改操作不超过50次。

这个题, 暴力可以过, 但是在cf上找到了一个神奇的代码。

如果没有修改, 那么就将询问存起来。 如果有了修改, 就dfs一次, 将之前的询问都处理完, 然后在修改。 跑的很快....

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int val[maxn], head[maxn*], num, ans[maxn];
struct node
{
int to, nextt;
}e[maxn*];
void add(int u, int v) {
e[num].to = v;
e[num].nextt = head[u];
head[u] = num++;
}
vector <int> Q[maxn], st;
void dfs(int u, int fa) {
if(!Q[u].empty()) {
int k = st.size()-;
while(k>=&&__gcd(val[st[k]], val[u])==)
k--;
if(~k)
k = st[k];
for(auto i: Q[u])
ans[i] = k;
Q[u].clear();
}
st.pb(u);
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs(v, u);
}
st.pop_back();
}
int main()
{
int n, m, x, y, sign;
scanf("%d%d", &n, &m);
mem1(head);
for(int i = ; i<=n; i++)
scanf("%d", &val[i]);
for(int i = ; i<n-; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
int cnt = ;
for(int i = ; i<m; i++) {
scanf("%d", &sign);
if(sign == ) {
scanf("%d", &y);
Q[y].pb(i);
cnt++;
} else {
scanf("%d%d", &x, &y);
if(cnt)
dfs(, );
val[x] = y;
cnt = ;
ans[i] = -;
}
}
if(cnt)
dfs(, );
for(int i = ; i<m; i++) {
if(ans[i] != -)
printf("%d\n", ans[i]);
}
return ;
}

codeforces 463E . Caisa and Tree的更多相关文章

  1. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  2. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  3. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  4. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  6. Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力

    http://codeforces.com/contest/463/problem/E 给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项: 1. 询问节点v到root之间的 ...

  7. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  8. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  9. 【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree

    Prelude 很好的模板题. 传送到Codeforces:(* ̄3 ̄)╭ Solution 首先要会DSU on Tree,不会的看这里:(❤ ω ❤). 众所周知DSU on Tree是可以用来处 ...

随机推荐

  1. ash, bash, ksh, csh, zsh

    /bin/bash (就是 Linux 预设的 shell, 是现在很多Linux的发行版中默认的shell,综合了其他shell的很多优点.)/bin/ksh (Kornshell 由 AT& ...

  2. 链表插入排序(insertion-sort-list)

    自己写的代码有几个比较大的用例一直过不去,网上的代码大部分有问题,思路是先将链表置空表,再将链表中的元素循环插入到指定位置. 下面是一份正确的代码,但是是带头节点的链表: void Insertsor ...

  3. Oracle 11g RAC features

    <一,> oracle 11g r2 RAC提供了以下功能: 高可用:shared-everything 模式保证了单节点的故障不会停止服务,集群中的其他节点将快速接管 可扩展性:多节点分 ...

  4. Android eng版系统烧录

    这里我只能记录下过程 Android有四层,最底层的Linux是一个kernel,然后是运行时库层,驱动大部分在这两层完成,接着是Framework层,主要是Android源码,最后是在源码基础上进行 ...

  5. [置顶] C#扩展方法 扩你所需

    通过前面的学习,了解到:使用扩展方法,可以向现有类型“添加”方法.本文将使用扩展方法来对系统类型,自定义类型及接口进行方法扩展,一睹扩展方法的风采. 1.使用扩展方法来扩展系统类型 String是c# ...

  6. Junk-Mail Filter(并差集删点)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【玩转Ubuntu】04. Ubuntu上配置git环境

    1. 使用PPA安装Git PPA,表示 Personal Package Archives,也就是个人软件包集. 有很多软件因为种种原因,不能进入官方的 Ubuntu 软件仓库. 为了方便 Ubun ...

  8. PID教程

    PID教程 介绍 本教程将向您展示了比例每一个比例项 (P)的特点,积分项(I)和微分项 (D) 控制,以及怎样使用它们来获得所需的响应.在本教程中,我们会考虑下面单位反馈系统: Plant[被控对象 ...

  9. SMACSS:一个关于CSS的最佳实践-2.Base Rules

    回顾 在上一篇SMACSS:一个关于CSS的最佳实践-Overview中,讲到SMACSS将CSS Rules分为5个Categories: Base Layout Module State Them ...

  10. hdu acm 2154(多解取一解)

    //题目中结果有一条限制就是最后必须跳回A,如果我们的思想框在这个条件上就很容易卡住,因为这样的条件下的路径很难有规律的罗列,然而我们说这个图形中有三个区域,我们算出每个区域的第n-1次的种类数,然后 ...