http://www.lydsy.com/JudgeOnline/problem.php?id=3251

这道题在北京八十中的时候有人讲过。。 不过由于自己continue 写掉了一个所以调了很久。

做法是如果整个序列没有合法三角形的话,那么整个链长不超过50个(最大的情况是斐波那契数列) 所以大于50个一定成立, 小于50个排序扫一遍就好了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll; const ll maxn = 200010;
const ll maxv = 50; ll int_get() {
ll x = 0; char c = (char)getchar(); bool f = 0;
while(!isdigit(c)) {
if(c == '-') f = 1;
c = (char)getchar();
}
while(isdigit(c)) {
x = x * 10 + (ll)(c - '0');
c = (char)getchar();
}
if(f) x = -x;
return x;
} struct edge {
ll t; edge* next;
}e[maxn * 2], *head[maxn]; ll ne = 0; void addedge(ll f, ll t) {
e[ne].t = t, e[ne].next = head[f], head[f] = e + ne ++;
} ll h[maxn];
ll n, m, v[maxn], fa[maxn]; void dfs(ll x, ll pre) {
h[x] = h[pre] + 1; fa[x] = pre;
for(edge* p = head[x]; p; p = p-> next) {
if(p-> t != pre) dfs(p-> t, x);
}
} void read() {
n = int_get(); m = int_get();
for(ll i = 1; i <= n; ++ i) v[i] = int_get();
for(ll i = 1; i < n; ++ i) {
ll f, t;
f = int_get(), t = int_get();
addedge(f, t), addedge(t, f);
}
dfs(1, 0);
} ll sta[maxn], top = 0; void sov() {
while(m --) {
ll opt = int_get();
if(!opt) {
ll a = int_get(), b = int_get();
top = 0;
if(h[a] < h[b]) swap(a, b);
while(h[a] != h[b] && top <= maxv) sta[++ top] = v[a], a = fa[a];
if(a != b) {
while(a != b && top <= maxv) sta[++ top] = v[a], sta[++ top] = v[b], a = fa[a], b = fa[b];
}
sta[++ top] = v[a];
if(top >= maxv) {
printf("Y\n"); continue;
}
sort(sta + 1, sta + top + 1); bool f = 0;
for(ll i = 1; i < top - 1 && !f; ++ i) {
if(sta[i] + sta[i + 1] > sta[i + 2]) f = 1;
}
if(f) printf("Y\n");
else printf("N\n");
}
else {
ll p = int_get(), w = int_get();
v[p] = w;
}
}
} int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
read(), sov();
return 0;
}

bzoj 3251的更多相关文章

  1. BZOJ 3251 树上三角形:LCA【构成三角形的结论】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3251 题意: 给你一棵树,n个节点,每个点的权值为w[i]. 接下来有m个形如(p,a,b ...

  2. BZOJ 3251 树上三角形

    NOIP的东西回成都再说吧... 这题暴力. #include<iostream> #include<cstdio> #include<cstring> #incl ...

  3. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

  4. BZOJ 3275: Number

    3275: Number Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 874  Solved: 371[Submit][Status][Discus ...

  5. BZOJ 2879: [Noi2012]美食节

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1834  Solved: 969[Submit][Status] ...

  6. bzoj 4610 Ceiling Functi

    bzoj 4610 Ceiling Functi Description bzoj上的描述有问题 给出\(n\)个长度为\(k\)的数列,将每个数列构成一个二叉搜索树,问有多少颗形态不同的树. Inp ...

  7. BZOJ 题目整理

    bzoj 500题纪念 总结一发题目吧,挑几道题整理一下,(方便拖板子) 1039:每条线段与前一条线段之间的长度的比例和夹角不会因平移.旋转.放缩而改变,所以将每条轨迹改为比例和夹角的序列,复制一份 ...

  8. 【sdoi2013】森林 BZOJ 3123

    Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负整数 ...

  9. 【清华集训】楼房重建 BZOJ 2957

    Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...

随机推荐

  1. 分布式消息中间件及RabbitMQ

    分布式应用和集群: 从部署形态来看,它们都是多台机器或者多个进程部署,而且都是为了实现一个业务功能. 如果是一个业务被拆分成多个子业务部署在不同的服务器上,那就是分布式应用 如果是同一个业务部署在多台 ...

  2. 开源大数据生态下的 Flink 应用实践

    过去十年,面向整个数字时代的关键技术接踵而至,从被人们接受,到开始步入应用.大数据与计算作为时代的关键词已被广泛认知,算力的重要性日渐凸显并发展成为企业新的增长点.Apache Flink(以下简称 ...

  3. Yii2 kineditor

    用 kineditor实现异步 <table cellspadding=5 width=400> <tr height='150'> <td valign="t ...

  4. 【CF1257A】Two Rival Students【思维】

    题意:给你n个人和两个尖子生a,b,你可以操作k次,每次操作交换相邻两个人的位置,求问操作k次以内使得ab两人距离最远是多少 题解:贪心尽可能的将两人往两边移动,总结一下就是min(n-1,|a-b| ...

  5. MySqL rownum 序号 类似于 oracle的rownum

    mysql中没有 rownum 序号的功能,所以需要自己去实现序号的功能. @rownum 只是一个变量 可以换为 @i 等其他变量,但必须有@符号 SELECT @rownum:=@rownum+1 ...

  6. Spring Boot学习一之Spring Beans和依赖注入

    你可以自由地使用任何标准的Spring框架技术去定义beans和它们注入的依赖.简单起见,我们经常使用 @ComponentScan 注解搜索beans,并结合 @Autowired 构造器注入. 如 ...

  7. iPad如何恢复

    iPad在有网络连接的情况下,可以通过iCloud进行恢复. 没有连接WiFi的情况下,只能通过USB连接才能恢复. ①下载最新版本的iTunes:https://support.apple.com/ ...

  8. web前端典型示例

    1.轨迹回放:https://openlayers.org/en/v4.6.5/examples/feature-move-animation.html https://blog.csdn.net/s ...

  9. 五. jenkins部署springboot项目(2)--windows环境--服务

    前提:jenkins和springboot运行在同一台机器 springboot 通过winsw部署为服务 winsw 下载地址:https://github.com/kohsuke/winsw/re ...

  10. HBase–RegionServer宕机恢复原理

    Region Server宕机总述 HBase一个很大的特色是扩展性极其友好,可以通过简单地加机器实现集群规模的线性扩展,而且机器的配置并不需要太好,通过大量廉价机器代替价格昂贵的高性能机器.但也正因 ...