题目链接

给一棵树, 两种操作, 一种是将点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. perl笔记

    更精确的空白字符串匹配 1.水平空白字符 Perl5.10之前,使用\s这种分类,不够精确,容易导致意料外的结果: Perl5.10中引入\h字符组,用来匹配任意水平空白字符(包括Unicode字符集 ...

  2. Objective-C中NSArray和NSMutableArray的基本用法

    /*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...

  3. BootStrap 智能表单系列 五 表单依赖插件处理

    这一章比较简单哦,主要就是生产表单元素后的一些后续处理操作,比如日期插件的渲染.一些autocomplete的处理等,在回调里面处理就可以了, demo: $("input.date-pic ...

  4. express小记

    >全局安装方法 `npm install -g express` >cmd切换到你想要放得目录,`express -t ejs blog` 这样就可以生成一个blog文件夹 >还需要 ...

  5. app微信支付服务器端php demo

    class Wxpay { /* 配置参数 */ private $config = array( 'appid' => "wxc92b12277f277355", /*微信 ...

  6. 利用python进行数据分析之绘图和可视化

    matplotlib API入门 使用matplotlib的办法最常用的方式是pylab的ipython,pylab模式还会向ipython引入一大堆模块和函数提供一种更接近与matlab的界面,ma ...

  7. 在iOS上增加手势锁屏、解锁功能

    在iOS上增加手势锁屏.解锁功能 在一些涉及个人隐私的场景下,尤其是当移动设备包含太多私密信息时,为用户的安全考虑是有必要的. 桌面版的QQ在很多年前就考虑到用户离开电脑后隐私泄露的危险,提供了“离开 ...

  8. [转] IOS中AppDelegate中的生命周期事件的调用条件

    IOS中AppDelegate中的生命周期事件的调用条件 //当应用程序将要进入非活动状态执行,在此期间,应用程序不接受消息或事件,比如来电 - (void)applicationWillResign ...

  9. grunt切换下载源

    nrm 是一个 NPM 源管理器,允许你快速地在NPM 源间切换: 安装:npm install -g nrm 列出可选源:nrm ls 切换:nrm use taobao 测试所有源连接时间:nrm ...

  10. mybatis日记配置Log4j

    拷贝log4j-1.2.16.jar到项目lib下 方式一是在src下新建一个log4j.xml文件,其具体内容如下 <?xml version="1.0" encoding ...