HDU 3966 樹鏈剖分后線段樹維護

題意:

  一棵樹,

  操作1.$path(a,b)$之間的點權$+k$

  操作2.單點查詢

題解:

樹鏈剖分即可,注意代碼細節,雙向映射

主要是記錄一下板子

#include <string.h>
#include <stdio.h>
#include <algorithm>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(ii,x) for(int ii=head[x];ii;ii=edge[ii].next)
using namespace std;
const int maxn=5e4+20,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
//head
int casn,n,m,k;
int num[maxn];
class segtree{public:
#define nd node[now]
#define ndl node[now<<1]
#define ndr node[now<<1|1]int node[maxn*4],n;int *mp;void maketree(int s,int t,int now){if(s==t){nd=num[mp[s]];return ;}else nd=0;maketree(s,(s+t)/2,now<<1);maketree((s+t)/2+1,t,now<<1|1);}void init(int nn,int *mps){n=nn;mp=mps;maketree(1,n,1);}void update(int s,int t,int x){update(s,t,x,1,n,1);}int query(int pos){return query(pos,1,n,1);}void update(int s,int t,int x,int l,int r,int now=1){if(s<=l&&t>=r) {nd+=x;return ;}ndl+=nd,ndr+=nd;nd=0;int mid=(l+r)/2;if(s<=mid) update(s,t,x,l,mid,now<<1);if(t>mid) update(s,t,x,mid+1,r,now<<1|1);}int query(int pos,int l,int r,int now=1){if(l==r) return nd;ndl+=nd,ndr+=nd;nd=0;int mid=(l+r)/2;if(pos<=mid) return query(pos,l,mid,now<<1);else  return query(pos,mid+1,r,now<<1|1);}
}tree;
namespace chain{struct data_e{int to,next;}edge[maxn<<1];int head[maxn],nume,mp[maxn];inline void addedge(int a,int b){edge[++nume]={b,head[a]};head[a]=nume;}int ltop[maxn],fa[maxn],deep[maxn];int sz[maxn],remp[maxn];int son[maxn],cnt;void init(){rep(i,1,n) head[i]=0;cnt=0,nume=1;}void dfs1(int now,int pre,int d){deep[now]=d,fa[now]=pre,sz[now]=1,son[now]=0;forn(i,now){int to=edge[i].to;if(to!=pre) {dfs1(to,now,d+1);sz[now]+=sz[to];if(sz[to]>sz[son[now]]) son[now]=to;}}}void dfs2(int now,int pre,int sp){ltop[now]=sp;mp[now]=++cnt;remp[cnt]=now;if(son[now])  dfs2(son[now],now,sp);forn(i,now){int to=edge[i].to;if(to!=son[now]&&to!=pre) dfs2(to,now,to);}}void update(int a,int b,int val){while(ltop[a]!=ltop[b]){if(deep[ltop[a]]<deep[ltop[b]])swap(a,b);tree.update(mp[ltop[a]],mp[a],val);a=fa[ltop[a]];}if(deep[a]>deep[b])swap(a,b);tree.update(mp[a],mp[b],val);}
};int main() {while(~scanf("%d %d %d",&n,&m,&k)){rep(i,1,n) scanf("%d",num+i);chain::init();while(m--){int a,b;scanf("%d%d",&a,&b);chain::addedge(a,b);chain::addedge(b,a);}chain::dfs1(1,0,1);chain::dfs2(1,1,1);tree.init(n,chain::remp);while(k--){char x[10];int a,b,c;scanf("%s",x);if(x[0]=='Q'){scanf("%d",&a);printf("%d\n",tree.query(chain::mp[a]));}else if(x[0]=='I'){scanf("%d %d %d",&a,&b,&c);chain::update(a,b,c);}else {scanf("%d %d %d",&a,&b,&c);chain::update(a,b,-c);}}}
}

?

轉載于:https://www.cnblogs.com/nervendnig/p/10638239.html

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

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

相關文章

VUE config/index.js文件配置

&#xfeff;&#xfeff; 當我們需要和后臺分離部署的時候&#xff0c;必須配置config/index.js: 用vue-cli 自動構建的目錄里面 &#xff08;環境變量及其基本變量的配置&#xff09;123456789101112131415var path require(path)module.exports {build: {index: path.res…

數據規則列表加導入導出

1.進入bos&#xff0c;打開數據規則&#xff0c;進入列表菜單 2.點擊事件-新增操作 3.點擊新增 4.點擊操作類型&#xff0c;輸入%引入 5.點擊確定&#xff0c;保存后生效&#xff0c;導出 、引入模板設置同理轉載于:https://www.cnblogs.com/RogerLu/p/10643521.html

Lecture 6 Order Statistics

Given n elements in array, find kth smallest element (element of rank k) Worst-case linear time order statistics --by Blum, Floyd, Pratt, Rivest, Tarjan --idea: generate good pivot recursively. Not so hot, because the constant is pretty big.

C++ qsort() 函數調用時實參與形參不兼容的問題解決

《劍指OFFER》刷題筆記 —— 撲克牌順子 LL今天心情特別好,因為他去買了一副撲克牌,發現里面居然有2個大王,2個小王(一副牌原本是54張^_^)...他隨機從中抽出了5張牌,想測測自己的手氣,看看能不能抽到順子,如果抽到的話,他決定去買體育彩票,嘿嘿&#xff01;&#xff01;“紅心A…

linux jenkins部署之路之,ftp部署怎么匿名還好用咋解決思密達

怎么安裝就不說了&#xff0c;網上一堆 這噶搭是配置 目錄是/etc/vsftpd/vsftpd.conf # Example config file /etc/vsftpd/vsftpd.conf# # The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more u…

powerCat進行常規tcp端口轉發

實戰中&#xff0c;我們也會遇到需要我們進行端口轉發的情況&#xff0c;比如已經拿下的目標機1是在dmz區&#xff0c;而目標1所在內網的其他目標只能通過目標1去訪問&#xff0c;這時候我們就需要端口轉發或者代理來進行后滲透。這次就要介紹一個加強版的nc&#xff0c;基于po…

Lecture 7 Hashing Table I

Hash |---Hash function: Division, Multiplication |---Collision: Chaining, Open addressing(Linear,Double hasing) Symbol-table problem: Table S holding n records pointer --> key|satelite data (record) Hashing: Hash function h maps keys “randomly”…

SpringCloud 微服務

一微服務架構概述1.1 微服務特性以及優點每個服務可以獨立運行在自己的進程里一系列獨立運行的微服務(goods,order,pay,user,search…)共同構建了整個系統每個服務為獨立的業務開發&#xff0c;一個微服務只關注某個特定的功能&#xff0c;例如用戶管理&#xff0c;商品管理微服…

window起別名

http://www.bagualu.net/wordpress/archives/1714 轉載于:https://www.cnblogs.com/wei-huan/p/10654026.html

vue在ie9中的兼容問題

問題總結 https://github.com/vuejs-templates/webpack/issues/260 首先npm install --save babel-polyfill然后在main.js中的最前面引入babel-polyfillimport babel-polyfill在index.html 加入以下代碼&#xff08;非必須&#xff09;<meta http-equiv"X-UA-Compatib…

Lecture 9 Random built Binary Search Trees BSTs

Random built Binary Search Trees BSTs E[hight] near 3logn Quick Sort? Relation to Quick Sort: BST sort & Quick sort make same comparisons but in a different order. Randomized BST Sort: 1. Randomly permute A 2. BST sort(A)

spring boot 帶遠程調試啟動方式

比如啟動service-system-0.0.1-SNAPSHOT.jar和service-file-0.0.1-SNAPSHOT.jar nohup java -Xdebug -Xrunjdwp:servery,transportdt_socket,address7999,suspendn -jar service-system-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &nohup java -Xdebug -Xrunjdwp:se…

文件讀寫

讀寫文件通常都是IO操作&#xff0c;Python內置了讀文件的函數&#xff0c;用法和C是兼容的。 讀寫文件前&#xff0c;我們先必須了解一下&#xff0c;在磁盤上讀寫文件的功能都是有操作系統提供的&#xff0c;現代操作系統不允許普通的程序直接操作磁盤&#xff0c;所以&#…

Vue項目中遇到了大文件分片上傳的問題

Vue項目中遇到了大文件分片上傳的問題&#xff0c;之前用過webuploader&#xff0c;索性就把Vue2.0與webuploader結合起來使用&#xff0c;封裝了一個vue的上傳組件&#xff0c;使用起來也比較舒爽。 上傳就上傳吧&#xff0c;為什么搞得那么麻煩&#xff0c;用分片上傳&#x…

NDK學習筆記-使用現有so動態庫

前面將的都是如何使用C/C文件生成so動態庫&#xff0c;那么在使用別人的so動態庫的時候應該怎么做呢&#xff1f;這篇文章就是使用一個變聲功能的動態庫&#xff0c;完成對于以有so動態庫的說明。 動態庫來源 在互聯網中&#xff0c;有著許許多多動態庫&#xff0c;很多廠商也對…

Spring cloud系列十四 分布式鏈路監控Spring Cloud Sleuth

1. 概述 Spring Cloud Sleuth實現對Spring cloud 分布式鏈路監控 本文介紹了和Sleuth相關的內容&#xff0c;主要內容如下&#xff1a; Spring Cloud Sleuth中的重要術語和意義&#xff1a;Span、Trance、AnnotationZipkin中圖形化展示分布式鏈接監控數據并說明字段意義Spring …

Linux源碼編譯安裝程序

一、程序的組成部分 Linux下程序大都是由以下幾部分組成&#xff1a; 二進制文件&#xff1a;也就是可以運行的程序文件 庫文件&#xff1a;就是通常我們見到的lib目錄下的文件 配置文件&#xff1a;這個不必多說&#xff0c;都知道 幫助文檔&#xff1a;通常是我們在linux下用…

selenium用法詳解

selenium用法詳解 selenium主要是用來做自動化測試&#xff0c;支持多種瀏覽器&#xff0c;爬蟲中主要用來解決JavaScript渲染問題。 模擬瀏覽器進行網頁加載&#xff0c;當requests,urllib無法正常獲取網頁內容的時候一、聲明瀏覽器對象 注意點一&#xff0c;Python文件名或者…