BZOJ 3251 树上三角形

NOIP的东西回成都再说吧。。。

这题暴力。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 100500
#define maxe 200500
using namespace std;
int n,q,x,y,z,val[maxv],anc[maxv][20],dis[maxv],g[maxv],nume=0;
int s[maxv],top=0;
struct edge
{
    int v,nxt;
}e[maxe];
void addedge(int u,int v)
{
    e[++nume].v=v;
    e[nume].nxt=g[u];
    g[u]=nume;
}
int read()
{
    char ch;int data=0;
    while (ch<‘0‘ || ch>‘9‘) ch=getchar();
    while (ch>=‘0‘ && ch<=‘9‘)
    {
        data=data*10+ch-‘0‘;
        ch=getchar();
    }
    return data;
}
void dfs(int x,int fath)
{
    for (int i=g[x];i;i=e[i].nxt)
    {
        int v=e[i].v;
        if (v!=fath)
        {
            anc[v][0]=x;dis[v]=dis[x]+1;
            dfs(v,x);
        }
    }
}
void get_table()
{
    for (int e=1;e<=19;e++)
        for (int i=1;i<=n;i++)
            anc[i][e]=anc[anc[i][e-1]][e-1];
}
int lca(int x,int y)
{
    if (dis[x]<dis[y]) swap(x,y);
    for (int e=19;e>=0;e--)
    {
        if ((dis[anc[x][e]]>=dis[y]) && (anc[x][e]))
           x=anc[x][e];
    }
    if (x==y) return x;
    for (int e=19;e>=0;e--)
    {
        if (anc[x][e]!=anc[y][e])
        {
            x=anc[x][e];
            y=anc[y][e];
        }
    }
    return anc[x][0];
}
void get_push(int t)
{
    top=0;
    while (x!=t) {s[++top]=val[x];x=anc[x][0];}
    while (y!=t) {s[++top]=val[y];y=anc[y][0];}
    s[++top]=val[t];
}
void ask()
{
    int t=lca(x,y),tot=dis[x]+dis[y]-dis[t]-dis[anc[t][0]];
    if (tot>50) {printf("Y\n");return;}
    get_push(t);
    sort(s+1,s+top+1);
    int flag=1;
    for (int i=1;i<=top-2;i++)
    {
        if ((long long)s[i]+(long long)s[i+1]>(long long)s[i+2])
           flag=0;
    }
    if (flag) printf("N\n");
    else printf("Y\n");
}
int main()
{
    n=read();q=read();
    for (int i=1;i<=n;i++) val[i]=read();
    for (int i=1;i<=n-1;i++)
    {
          x=read();y=read();
          addedge(x,y);addedge(y,x);
    }
    dis[1]=1;dfs(1,0);get_table();
    for (int i=1;i<=q;i++)
    {
        z=read();x=read();y=read();
        if (z) val[x]=y;
        else ask();
    }
    return 0;
}
时间: 2024-07-28 13:13:58

BZOJ 3251 树上三角形的相关文章

BZOJ 3251 树上三角形 暴力

题目大意:给定一棵树,每个点上有点权,多次修改点权,以及查询两点间路径上所有点权之间能否找出三个值构成三角形的三边长 被逗了- - 首先考虑如果一些数不能构成三角形的三边长,那么这些数最多有多少个? 显然当这些数构成斐波那契数列的时候数值的个数最多- - 那么2^31以内共有多少个斐波那契数?46! 也就是说当两点间路径上的点>=47时答案一定是YES! 那么小于47时只要暴力就行- - 时间复杂度O(mlogk) 其中k是最大的数的大小- - #include <cstdio> #in

【BZOJ3251】树上三角形 暴力

[BZOJ3251]树上三角形 Description 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. Input 第一行两个整数n.q表示树的点数和操作数 第二行n个整数表示n个点的点权 以下n-1行,每行2个整数a.b,表示a是b的父亲(以1为根的情况下) 以下q行,每行3个整数t.a.b 若t=0,则询问(a,b) 若t=1,则将点a的点权修改为b Output 对每个询问输出一行表示答案,

BZOJ 2458 最小三角形 | 平面分治

BZOJ 2458 最小三角形 题面 一个平面上有很多点,求他们中的点组成的周长最小的三角形的周长. 题解 跟平面最近点对差不多,也是先把区间内的点按x坐标从中间分开,递归处理,然后再处理横跨中线的三角形. 如何缩小范围?设左右两个子区间发现的最小周长是d,则与中线距离超过d / 2都没有用了,对于一个点,所有与它距离超过d / 2的点也都没有用. #include <cstdio> #include <cstring> #include <cmath> #includ

树上三角形(斐波那契数列神奇应用)

树上三角形(斐波那契数列神奇应用) Description给定一个大小为 n 的有点权树,需要支持两个操作.0:询问(u,v),能否在 u 到 v 的简单路径上取三个点,使这三个点的点权作为边长可以构成一个三角形.1:修改某个点的点权. Input第一行两个整数 n,q 表示树的点数和操作数.第二行 n 个整数表示 n 个点的初始的点权.接下来 n-1 行,每行两个整数 a,b,表示 a 是 b 的父亲.接下来 q 行,每行三个整数 op,a,b:若 op=0,则表示询问(a,b).若 op=1

bzoj 3251

http://www.lydsy.com/JudgeOnline/problem.php?id=3251 这道题在北京八十中的时候有人讲过.. 不过由于自己continue 写掉了一个所以调了很久. 做法是如果整个序列没有合法三角形的话,那么整个链长不超过50个(最大的情况是斐波那契数列) 所以大于50个一定成立, 小于50个排序扫一遍就好了 #include <iostream> #include <cstdio> #include <cstring> #includ

BZOJ3251 树上三角形

一看这题...难道要链剖乱搞什么的吗...不会啊汗... 突然发现不构成三角形的条件其实非常苛刻,由斐波那契数列: 1,1,2,3,5,8,13,21,34...... 可以知道其实小于int的大概就50项的样子. 于是路径长度>50直接输出'Y',否则排序判断... 看来还是蛮快的... 1 /************************************************************** 2 Problem: 3251 3 User: rausen 4 Langu

BZOJ 1845: [Cqoi2005] 三角形面积并 [计算几何 扫描线]

1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1151  Solved: 313[Submit][Status][Discuss] Description 给出n个三角形,求它们并的面积. Input 第一行为n(N < = 100), 即三角形的个数 以下n行,每行6个整数x1, y1, x2, y2, x3, y3,代表三角形的顶点坐标.坐标均为不超过10 ^ 6的实数,输入数据保留1位小数 Out

bzoj 1845: [Cqoi2005] 三角形面积并 扫描线

1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 848  Solved: 206[Submit][Status][Discuss] Description 给出n个三角形,求它们并的面积. Input 第一行为n(N < = 100), 即三角形的个数 以下n行,每行6个整数x1, y1, x2, y2, x3, y3,代表三角形的顶点坐标.坐标均为不超过10 ^ 6的实数,输入数据保留1位小数 Outp

bzoj 3757 树上莫队

感谢以下文章作者: http://blog.csdn.net/kuribohg/article/details/41458639 http://vfleaking.blog.163.com/blog/static/174807634201311011201627/ http://blog.csdn.net/jiangyuze831/article/details/41476865 http://hzwer.com/5259.html 做了树上的莫队,感觉对这个算法的思想理解更深了 先分块,不论怎