传送门

考场上打了两个小时树剖,结果还是没搞出来

发现对于两个确定的点,它们一定可以列出一个方程来

其中系数的大小和正负只与这两点间距离的奇偶性有关

所以可以加一堆分情况讨论然后树剖

至于正解:

考虑两点之间的关系很麻烦,可以固定一个基准点,把它们都用 \(x_1\) 表示出来

  • 当需要极其麻烦地考虑两点之间的关系时,考虑固定一个基准点分别表示它们

发现这样的话对一个点的修改等价于把它的子树整体加上一个数

所以可以建立dfs序,直接树状数组维护,同样根据奇偶性判无解

Code:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1000010
#define ll long long
//#define int long long char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
} int n, q;
int head[N], size, in[N], out[N], tot, val[N], dep[N], lim;
ll a[N<<1], b[N<<1];
struct edge{int to, next;}e[N];
inline void add(int s, int t) {e[++size].to=t; e[size].next=head[s]; head[s]=size;}
inline void upd(ll* a, int i, ll dat) {for (; i<=lim; i+=i&-i) a[i]+=dat;}
inline ll query(ll* a, int i) {ll ans=0; for (; i; i-=i&-i) ans+=a[i]; return ans;} void dfs(int u) {
in[u]=++tot;
upd(a, in[u], (dep[u]&1?-1:1)*val[u]);
upd(b, in[u], (dep[u]&1?1:-1)*val[u]);
for (int i=head[u]; ~i; i=e[i].next) dep[e[i].to]=dep[u]+1, dfs(e[i].to);
out[u]=++tot;
upd(a, out[u], (dep[u]&1?1:-1)*val[u]);
upd(b, out[u], (dep[u]&1?-1:1)*val[u]);
} signed main()
{
memset(head, -1, sizeof(head));
ll w; n=read(); q=read(); lim=n*2;
for (int i=2,u,w; i<=n; ++i) {
u=read(); w=read();
add(u, i); val[i]=w;
}
dep[1]=1; dfs(1);
for (int i=1,u,v,s,dlt; i<=q; ++i) {
if (read()&1) {
u=read(); v=read(); s=read();
//cout<<"uv: "<<u<<' '<<v<<endl;
w=query(dep[u]&1?b:a, in[u])+query(dep[v]&1?b:a, in[v])-s;
//cout<<"w: "<<w<<' '<<query(dep[u]&1?b:a, in[u])<<' '<<query(dep[v]&1?b:a, in[v])<<endl;
if (w&1) puts("none");
else if ((dep[u]&1?1:0)^(dep[v]&1?1:0)) puts(!w?"inf":"none");
else printf("%lld\n", (((dep[u]&1)&&(dep[v]&1))?-1:1)*(w>>1));
}
else {
u=read(); w=read();
dlt=w-val[u];
upd(a, in[u], (dep[u]&1?-1:1)*dlt);
upd(b, in[u], (dep[u]&1?1:-1)*dlt);
upd(a, out[u], (dep[u]&1?1:-1)*dlt);
upd(b, out[u], (dep[u]&1?-1:1)*dlt);
val[u]=w;
}
} return 0;
}

题解 Equation的更多相关文章

  1. [NOIP10.6模拟赛]2.equation题解--DFS序+线段树

    题目链接: 咕 闲扯: 终于在集训中敲出正解(虽然与正解不完全相同),开心QAQ 首先比较巧,这题是\(Ebola\)出的一场模拟赛的一道题的树上强化版,当时还口胡出了那题的题解 然而考场上只得了86 ...

  2. Hdoj 2199.Can you solve this equation? 题解

    Problem Description Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solutio ...

  3. The equation (扩展欧几里得)题解

    There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer r ...

  4. Codeforces Little Dima and Equation 数学题解

    B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. csp-s模拟测试56Merchant, Equation,Rectangle题解

    题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...

  6. 2019牛客多校第九场B Quadratic equation(二次剩余定理)题解

    题意: 传送门 已知\(0 <= x <= y < p, p = 1e9 + 7\)且有 \((x+y) = b\mod p\) \((x\times y)=c\mod p\) 求解 ...

  7. CF20B Equation 题解

    Content 解方程 \(ax^2+bx+c=0\). 数据范围:\(-10^5\leqslant a,b,c\leqslant 10^5\). Solution 很明显上求根公式. 先来给大家推推 ...

  8. CF460B Little Dima and Equation (水题?

    Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...

  9. 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

    这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...

随机推荐

  1. 「CF986F」 Oppa Funcan Style Remastered

    「CF986F」 Oppa Funcan Style Remastered Link 首先发现分解成若干个 \(k\) 的因数很蠢,事实上每个因数都是由某个质因子的若干倍组成的,所以可以将问题转换为分 ...

  2. C语言变量类型

    #include <stdio.h> // C 语言中,任何数据类型都不可以直接存储一个字符串.那么字符串如何存储? //在 C 语言中,字符串有两种存储方式,一种是通过字符数组存储,另一 ...

  3. python -- 程序结构

    一.程序结构 • 定义python文件的头部模板:在File->Settings->Editor->File and Code Templates->Python script ...

  4. 测试龙芯 LoongArch .NET之 使用 FastTunnel 做内网穿透远程计算机

    龙芯3A5000 已经上市,与龙芯3A5000配套的三大编译器GCC.LLVM.GoLang和三大虚拟机Java.JavaScript..NET均已完成开发,从老伙计哪里搞来一台3A5000 机器,安 ...

  5. div填满页面剩余空间的方法

    想让div填满页面剩余空间,最简易的方式还是靠提前的布局. 这里提供两种方法: (1)利用 height 样式的%比例设置布局 <!DOCTYPE html> <html lang= ...

  6. C++第三十六篇 -- 为第一个驱动程序进行调试

    工具是VMware12+Win10+VS2017+WDK1809 https://blog.csdn.net/qq_21763381/article/details/83242916 首先分清楚主计算 ...

  7. vue知识点----element UI+vue关于日期范围选择的操作,picker-options属性的使用

    需求场景如下: 指定起止日期,后选的将会受到先选的限制 不同的日期选择器,不过也存在关联关系 实现方法不难,利用了 change 事件,动态改变 picker-options 中的 disableDa ...

  8. C#曲线分析平台的制作(四,highcharts+ajax加载后台数据)

    在上一篇博客:C#曲线分析平台的制作(三,三层构架+echarts显示)中已经完成了后台的三层构架的简单搭建,为实现后面的拓展应用开发和review 改写提供了方便.而在曲线分析平台中,往往有要求时间 ...

  9. Verilog function的使用

    function的用法 function的标准写法如下: function  <返回值的类型或是范围>  (函数名): <端口说明语句> //input xxx <变量类 ...

  10. joomla 3.7.0 (CVE-2017-8917) SQL注入漏洞

    影响版本: 3.7.0 poc http://192.168.49.2:8080/index.php?option=com_fields&view=fields&layout=moda ...