codeforces 463E . Caisa and Tree
给一棵树, 两种操作, 一种是将点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的更多相关文章
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces Round #264 (Div. 2) E. Caisa and Tree 树上操作暴力
http://codeforces.com/contest/463/problem/E 给出一个总节点数量为n的树,每个节点有权值,进行q次操作,每次操作有两种选项: 1. 询问节点v到root之间的 ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- 【题解】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是可以用来处 ...
随机推荐
- BZOJ 4300 绝世好题(位运算)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4300 [题目大意] 给出一个序列a,求一个子序列b,使得&和不为0 [题解] ...
- VS2010/MFC对话框:一般属性页对话框的创建及显示
一般属性页对话框的创建及显示 本节将介绍一般属性页对话框的创建和显示. 实际上,一般属性页对话框的创建和显示过程和向导对话框是很类似的.鸡啄米将上一节中的向导对话框进行少量修改,使其成为一般属性页对话 ...
- 每天学点Linux:二
关于输入输出和重定向: 默认情况下标准I/O的输入为键盘,输出为显示屏.输入和输出可以通过符号‘>’进行重定向. 例如可以通过命令:$>newfile 来创建一个新文件(如果newfile ...
- Android Animation学习(一) Property Animation介绍
Android Animation Android framework提供了两种动画系统: property animation (introduced in Android 3.0)和view an ...
- 【Android界面实现】信息更新小红点显示——自己定义控件BadgeView的使用介绍
在如今大部分的信息公布类应用,都有这样的一个功能:当后台数据更新,比方有系统消息或者是用户间有互动的时候,通过在控件上显示一个小红点来提示用户有新的信息.一般来说,这样的业务需求,我们能够在布局文件里 ...
- SQL2000下修复某数据库的经历
某个SQL2000的数据库,在通过备份/还原的方法升级到2005时发生错误: 查找解决方法未果 正好最近在看 @一线码农 的<sql server之旅>,就想自己试试解决这个问题 首先运行 ...
- Foundation 框架 NSArray、NSMutableArray排序
一.使用selector对数组进行排序(无返回) 数组 book 中包含 AddressCard对象. 1.对数组调用 sortUsingSelector方法 -(void) sortByName { ...
- 最大流之sap算法
若有向图G = (V , E)满足下列条件: 1.有且仅有一个顶点S,它的入度为 0 ,这个顶点称为源点. 2.有且仅有一个顶点T,它的出度为 0 ,这个顶点称为汇点. 3.每一条弧都有一个非负数,叫 ...
- BZOJ 1625: [Usaco2007 Dec]宝石手镯( dp )
最裸的01背包.... --------------------------------------------------------------------- #include<cstdio ...
- java 简单的词法分析
package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...