hdu5692 Snacks dfs序+線段樹

題目傳送門

題目大意:給出一顆樹,根節點是0,有兩種操作,一是修改某個節點的value,二是查詢,從根節點出發,經過 x 節點的路徑的最大值。

思路:用樹狀數組寫發現還是有些麻煩,最后用線段樹了。

   其實這道題的查詢,就是查詢從根節點到x節點+x節點走下去的路徑的最大值,這樣會發現,其實就是查詢包括x節點的所有子樹中權值最大的那個,而包括x節點的子樹,如果用dfs序轉換一下的話,可以在線段上用一段連續的點表示出來,所以最后就轉換成了線段樹區間求最大值,然后單點修改的題目了。

  要注意的是dfs序和原標號的對應,有一個地方弄反了,卡了好久。

#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
#define CLR(a,b) memset(a,b,sizeof(a))
#define PI acos(-1)
#define lson rt*2,l,(l+r)/2
#define rson rt*2+1,(l+r)/2+1,r
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=100010;
struct edge{int to,Next;
}e[maxn*2];
int tot,m,n,head[maxn],pos,dfn[maxn],fa[maxn],son[maxn],l[maxn],r[maxn];
ll val[maxn],dis[maxn];
inline void init(){CLR(head,-1),tot=0,pos=0;CLR(dis,0);fa[1]=1;
}
inline void addv(int u,int v){e[++tot]={v,head[u]};head[u]=tot;
}
ll tree[maxn << 2], laz[maxn << 2];
inline void pushup(int rt) {tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}
inline void pushdown(int rt) {if (laz[rt]) {tree[rt << 1] += laz[rt];tree[rt << 1 | 1] += laz[rt];laz[rt << 1] += laz[rt];laz[rt << 1 | 1] += laz[rt];laz[rt] = 0;}
}
inline void build(int rt, int l, int r) {laz[rt] = 0;if (l == r) {tree[rt] = dis[l];return ;}build(lson);build(rson);pushup(rt);
}
inline void update(int L, int R, ll v, int rt, int l, int r) {if (L <= l && R >= r) {tree[rt] += v;laz[rt] += v;return;}pushdown(rt);if (L <= (l + r) / 2)   update(L, R, v, lson);if (R > (l + r) / 2)    update(L, R, v, rson);pushup(rt);
}
inline ll query(int L, int R, int rt, int l, int r) { if (L <= l && R >= r) {return tree[rt];}pushdown(rt);if (L > (l + r) / 2)    return query(L,R,rson);else if (R <= (l + r) / 2)  return query(L,R,lson);else return max(query(L,R,lson),query(L,R,rson));
}inline void dfs(int u,int pre){dfn[u]=++pos;l[u]=pos;son[u]=1;dis[dfn[u]]=dis[dfn[pre]]+val[u];for(int i=head[u];i!=-1;i=e[i].Next){int v=e[i].to;if(v==fa[u])continue;fa[v]=u;dfs(v,u);son[u]+=son[v];}r[u]=pos;
}
int main(){int T,cas=1;cin>>T;while(T--){init();scanf("%d%d",&n,&m);for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);addv(u+1,v+1);addv(v+1,u+1);}for(int i=1;i<=n;i++)scanf("%lld",&val[i]);dfs(1,1);build(1,1,n);printf("Case #%d:\n",cas++);while(m--){int op,x;ll y;scanf("%d%d",&op,&x);if(op==1){//    printf("debug  %d  %d\n",dfn[x+1],dfn[x+1]+son[x+1]-1);
            printf("%lld\n",query(l[x+1],r[x+1],1,1,n));}else{scanf("%lld",&y);update(l[x+1],r[x+1],y-val[x+1],1,1,n);val[x+1]=y;}}}
}

Snacks

Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5563????Accepted Submission(s): 1265


Problem Description
百度科技園內有n個零食機,零食機之間通過n?1條路相互連通。每個零食機都有一個值v,表示為小度熊提供零食的價值。

由于零食被頻繁的消耗和補充,零食機的價值v會時常發生變化。小度熊只能從編號為0的零食機出發,并且每個零食機至多經過一次。另外,小度熊會對某個零食機的零食有所偏愛,要求路線上必須有那個零食機。

為小度熊規劃一個路線,使得路線上的價值總和最大。

?

Input
輸入數據第一行是一個整數T(T10),表示有T組測試數據。

對于每組數據,包含兩個整數n,m(1n,m100000),表示有n個零食機,m次操作。

接下來n?1行,每行兩個整數xy(0x,y<n),表示編號為x的零食機與編號為y的零食機相連。

接下來一行由n個數組成,表示從編號為0到編號為n?1的零食機的初始價值v(|v|<100000)

接下來m行,有兩種操作:0?x?y,表示編號為x的零食機的價值變為y1?x,表示詢問從編號為0的零食機出發,必須經過編號為x零食機的路線中,價值總和的最大值。

本題可能棧溢出,辛苦同學們提交語言選擇c++,并在代碼的第一行加上:

`#pragma comment(linker, "/STACK:1024000000,1024000000") `

?

Output
對于每組數據,首先輸出一行”Case #?:”,在問號處應填入當前數據的組數,組數從1開始計算。

對于每次詢問,輸出從編號為0的零食機出發,必須經過編號為x零食機的路線中,價值總和的最大值。

?

Sample Input
1 6 5 0 1 1 2 0 3 3 4 5 3 7 -5 100 20 -5 -7 1 1 1 3 0 2 -1 1 1 1 5

?

Sample Output
Case #1: 102 27 2 20

轉載于:https://www.cnblogs.com/mountaink/p/9863097.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388124.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388124.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388124.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python數據建模數據集_Python中的數據集

python數據建模數據集There are useful Python packages that allow loading publicly available datasets with just a few lines of code. In this post, we will look at 5 packages that give instant access to a range of datasets. For each package, we will look at h…

打開editor的接口討論

【打開editor的接口討論】 先來看一下workbench吧&#xff0c;workbench從靜態劃分應該大致如下&#xff1a; 從結構圖我們大致就可以猜測出來&#xff0c;workbench page作為一個IWorkbenchPart&#xff08;無論是eidtor part還是view part&#…

【三角函數】已知直角三角形的斜邊長度和一個銳角角度,求另外兩條直角邊的長度...

如圖,已知直角三角形ABC中,∠C90, ∠Aa ,ABc ,求直角邊AC、BC的長度. ∵ ∠C90,∠Aa ,ABc ,Cos∠AAC/AB ,Sin∠ABC/AB ,∴ ACAB*Cos∠Ac*Cosa ,BCAB*Sin∠Ac*Sina . 復制代碼

網絡攻防技術實驗五

2018-10-23 實驗五 學 號201521450005 中國人民公安大學 Chinese people’ public security university 網絡對抗技術 實驗報告 實驗五 綜合滲透 學生姓名 陳軍 年級 2015 區隊 五 指導教師 高見 信息技術與網絡安全學院 2018年10月23日 實驗任務總綱 2018—2019 …

usgs地震記錄如何下載_用大葉草繪制USGS地震數據

usgs地震記錄如何下載One of the many services provided by the US Geological Survey (USGS) is the monitoring and tracking of seismological events worldwide. I recently stumbled upon their earthquake datasets provided at the website below.美國地質調查局(USGS)…

Springboot 項目中 xml文件讀取yml 配置文件

2019獨角獸企業重金招聘Python工程師標準>>> 在xml文件中讀取yml文件即可&#xff0c;代碼如下&#xff1a; 現在spring-boot提倡零配置&#xff0c;但是的如果要集成老的spring的項目&#xff0c;涉及到的bean的配置。 <bean id"yamlProperties" clas…

eclipse 插件打包發布

如果想把調試好的插件打包發布&#xff0c;并且在ECLIPSE中可以使用. 1.File-->Export 2.選擇 PLug-in Development下 的 Deployable plug-ins and fragments 3.進入 Deployable plug-ins and fragments 頁面 4.把底下的 Destubatuib 的選項中選擇 Archive file 在這里添入要…

無法獲取 vmci 驅動程序版本: 句柄無效

https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 將 vmci0.present "TURE" 改為 “FALSE”; 轉載于:https://www.cnblogs.com/limanjihe/p/9868462.html

數據可視化 信息可視化_更好的數據可視化的8個技巧

數據可視化 信息可視化Ggplot is R’s premier data visualization package. Its popularity can likely be attributed to its ease of use — with just a few lines of code you are able to produce great visualizations. This is especially great for beginners who are…

分布式定時任務框架Elastic-Job的使用

為什么80%的碼農都做不了架構師&#xff1f;>>> 一、前言 Elastic-Job是一個優秀的分布式作業調度框架。 Elastic-Job是一個分布式調度解決方案&#xff0c;由兩個相互獨立的子項目Elastic-Job-Lite和Elastic-Job-Cloud組成。 Elastic-Job-Lite定位為輕量級無中心化…

Memcached和Redis

Memcached和Redis作為兩種Inmemory的key-value數據庫&#xff0c;在設計和思想方面有著很多共通的地方&#xff0c;功能和應用方面在很多場合下(作為分布式緩存服務器使用等) 也很相似&#xff0c;在這里把兩者放在一起做一下對比的介紹 基本架構和思想 首先簡單介紹一下兩者的…

第4章 springboot熱部署 4-1 SpringBoot 使用devtools進行熱部署

/imooc-springboot-starter/src/main/resources/application.properties #關閉緩存, 即時刷新 #spring.freemarker.cachefalse spring.thymeleaf.cachetrue#熱部署生效 spring.devtools.restart.enabledtrue #設置重啟的目錄,添加那個目錄的文件需要restart spring.devtools.r…

border-radius 漲知識的寫法

<div idapp></div>復制代碼#app{width:100%;height:80px;background:pink;border-radius:75%/20% 20% 0 0;}復制代碼僅供自己總結記憶轉載于:https://juejin.im/post/5c80afd66fb9a049f81a1217

ibm python db_使用IBM HR Analytics數據集中的示例的Python獨立性卡方檢驗

ibm python dbSuppose you are exploring a dataset and you want to examine if two categorical variables are dependent on each other.假設您正在探索一個數據集&#xff0c;并且想要檢查兩個分類變量是否相互依賴。 The motivation could be a better understanding of …

Oracle優化檢查表

分類檢查項目相關文件或結果狀態備注日志及文件Oracle Alert 日志bdump/udump下是否存在明顯的報警listener相關日志SQL* Net日志參數/參數文件listener.ora/tnsnames.ora操作系統操作系統版本檢查操作系統補丁節點名操作系統vmstat狀態操作系統I/O狀態操作系統進程情況操作系統…

spring分布式事務學習筆記(2)

此文已由作者夏昀授權網易云社區發布。歡迎訪問網易云社區&#xff0c;了解更多網易技術產品運營經驗。Model類如下&#xff1a;package com.xy.model1 package com.xy.model;2 3 /**4 * Created by helloworld on 2015/1/30.5 */6 public class NameQa {7 private long …

sql 左聯接 全聯接_通過了解自我聯接將您SQL技能提升到一個新的水平

sql 左聯接 全聯接The last couple of blogs that I have written have been great for beginners ( Data Concepts Without Learning To Code or Developing A Data Scientist’s Mindset). But, I would really like to push myself to create content for other members of …

如何查看linux中文件打開情況

如何查看linux中文件打開情況 前言 我們都知道&#xff0c;在linux下&#xff0c;“一切皆文件”&#xff0c;因此有時候查看文件的打開情況&#xff0c;就顯得格外重要&#xff0c;而這里有一個命令能夠在這件事上很好的幫助我們-它就是lsof。 linux下有哪些文件 在介紹lsof命…

hadoop windows

1、安裝JDK1.6或更高版本 官網下載JDK&#xff0c;安裝時注意&#xff0c;最好不要安裝到帶有空格的路徑名下&#xff0c;例如:Programe Files&#xff0c;否則在配置Hadoop的配置文件時會找不到JDK&#xff08;按相關說法&#xff0c;配置文件中的路徑加引號即可解決&#xff…

Ocelot中文文檔入門

入門 Ocelot僅適用于.NET Core&#xff0c;目前是根據netstandard2.0構建的&#xff0c;如果Ocelot適合您&#xff0c;這個文檔可能會有用。 .NET Core 2.1 安裝NuGet包 使用nuget安裝Ocelot及其依賴項。 您需要創建一個netstandard2.0項目并將其打包到其中。 然后按照下面的“…