题解 Equation
考场上打了两个小时树剖,结果还是没搞出来
发现对于两个确定的点,它们一定可以列出一个方程来
其中系数的大小和正负只与这两点间距离的奇偶性有关
所以可以加一堆分情况讨论然后树剖
至于正解:
考虑两点之间的关系很麻烦,可以固定一个基准点,把它们都用 \(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的更多相关文章
- [NOIP10.6模拟赛]2.equation题解--DFS序+线段树
题目链接: 咕 闲扯: 终于在集训中敲出正解(虽然与正解不完全相同),开心QAQ 首先比较巧,这题是\(Ebola\)出的一场模拟赛的一道题的树上强化版,当时还口胡出了那题的题解 然而考场上只得了86 ...
- 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 ...
- 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 ...
- Codeforces Little Dima and Equation 数学题解
B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...
- csp-s模拟测试56Merchant, Equation,Rectangle题解
题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...
- 2019牛客多校第九场B Quadratic equation(二次剩余定理)题解
题意: 传送门 已知\(0 <= x <= y < p, p = 1e9 + 7\)且有 \((x+y) = b\mod p\) \((x\times y)=c\mod p\) 求解 ...
- CF20B Equation 题解
Content 解方程 \(ax^2+bx+c=0\). 数据范围:\(-10^5\leqslant a,b,c\leqslant 10^5\). Solution 很明显上求根公式. 先来给大家推推 ...
- CF460B Little Dima and Equation (水题?
Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...
- 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题
这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...
随机推荐
- Leetcode No.27 Remove Element(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...
- 42. Trapping Rain Water [dp][stack]
description: Given n non-negative integers representing an elevation map where the width of each bar ...
- NetCore微服务简单流程审批数据库设计及后台服务开发
1.数据库设计 -- ---------------------------- -- Table structure for TBase_Flow -- ----------------------- ...
- 「CF1380G」 Circular Dungeon
CF1380G Circular Dungeon 看懂样例就能做. 虽然我瞪了 20 分钟 菜是原罪 首先可以将从每一个点出发所能获得的价值相加,再除以 \(n\) 就可以得到价值的期望. 所以问题转 ...
- 案例分享:Qt+Arm基于RV1126平台的内窥镜软硬整套解决方案(实时影像、冻结、拍照、录像、背光调整、硬件光源调整,其他产品也可使用该平台,如视频监控,物联网产品等等)
自研产品系列方案 1. 基于瑞芯微的 RV1126 芯片平台: 2. 外接 USB 摄像头(OV9734. OV6946.OV2740 等 UVC 模块)作为图像输入源: 3. 可通过 LED ...
- docker容器技术基础之联合文件系统OverlayFS
我们在上篇介绍了容器技术中资源隔离与限制docker容器技术基础之linux cgroup.namespace 这篇小作文我们要尝试学习容器的另外一个重要技术之联合文件系统之OverlayFS,在介绍 ...
- Spring RestTemplate 之post请求
●post请求:在RestTemplate中,POST请求可以通过如下三个方法来发起,但post提交方式又有两种 formData 和 payLoad,而且接口设计与传统的浏览器使用的提交方式又有差异 ...
- 怎么添加对Shopify 的WebHook 验证
怎么添加对Shopify 的WebHook 验证 背景介绍 Shopify 是一家一站式SaaS 模式的电商服务平台,总部位于加拿大首都渥太华,专注于为跨境电商用户提供海外品牌建立及销售渠道管理.为电 ...
- 前端基础div(六)
实例 文档中的一个部分会显示为绿色: <div style="color:#00FF00"> <h3>This is a header</h3> ...
- Python基础之用tkinter写界面
参考链接:https://blog.csdn.net/qq_37482202/article/details/84201259 Tkinter介绍 Tkinter(也叫Tk接口)是Tk图形用户界面工具 ...