日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 786B Legacy (线段树优化建图)

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 786B Legacy (线段树优化建图) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Codeforces 786B Legacy (線段樹優化建圖)


題意:\(n\)個點,有\(3\)種連邊操作:1.將\(u\)指向\(v\);2.將\(v\)指向編號在區間\([l,r]\)的點;3.將\([l,r]\)中的所有點指向\(v\)

做法:線段樹優化建圖。拓展一些新的節點來代表某些區間的點,然后,如果需要進行區間\([L,R]\)連邊,那么就可以將\([L,R]\)拆成一些區間的組合,以此減少邊的數目。于是可以利用線段樹,將每個線段樹上的節點新建出來,連接對應的區間即可,而因為如果只有一顆線段樹不能對所有點跑最短路,所以我們建兩棵線段樹,一顆指向序列,另一顆從序列指出來。原本對應的區間邊權設為0,跑最短路即可。

#include <cstdio> #include <algorithm> #include <iostream> #include <vector> #include <queue> #include <cstring> #define IOS ios::sync_with_stdio(false) #define pb push_back #define Pii pair<int,int> #define Ppi pair<Pii, int> #define x first #define y second typedef long long ll; const int N = 100020; inline void read(int &x) {x = 0; int f = 1; char c = getchar();while(!isdigit(c)) {if(c=='-')f=-1;c=getchar();}while(isdigit(c)) {x=x*10+c-'0';c=getchar();}x *= f; } using namespace std; bool ST; int n, q, st; struct edge {int e,w,nxt;} E[10000003]; int h[6000000], cc; void add(int u, int v, int w) {E[cc].e = v; E[cc].w = w; E[cc].nxt = h[u]; h[u] = cc; ++cc; } int ID[2][N << 2], id; void build(int p, int l, int r, int typ) {ID[typ][p] = ++id;if(!typ) for(int i = l; i <= r; ++i) add(ID[typ][p], i, 0);else for(int i = l; i <= r; ++i) add(i, ID[typ][p], 0);if(l == r) return;int mid = (l + r) >> 1;build(p<<1,l,mid,typ);build(p<<1|1,mid+1,r,typ); } void update(int p, int l, int r, int v, int L,int R, int w, int typ) {if(l == L && r == R) {if(!typ) add(v, ID[typ][p], w);else add(ID[typ][p], v, w);return;}int mid = (l + r) >> 1;if(R <= mid) update(p<<1,l,mid, v,L,R,w,typ);else if(L > mid) update(p<<1|1,mid+1,r, v,L,R,w,typ);else {update(p<<1,l,mid, v,L,mid,w,typ);update(p<<1|1,mid+1,r, v,mid+1,R,w,typ);} } struct node {int e; ll w;node(){}node(int a, ll b) {e=a;w=b;}bool operator < (const node &a) const {return w > a.w;} }; ll dis[6000001]; int vis[6000001]; void dij(int st) {for(int i = 1; i <= id; ++i) dis[i] = __LONG_LONG_MAX__;priority_queue< node > q;q.push(node(st,0)); dis[st] = 0;while(!q.empty()) {node u = q.top(); q.pop();if(vis[u.e]) continue;vis[u.e] = 1;for(int i = h[u.e]; ~i ; i = E[i].nxt) {int v = E[i].e; ll w = E[i].w;if(dis[u.e] != __LONG_LONG_MAX__ && dis[v] > dis[u.e] + w) {dis[v] = dis[u.e] + w;q.push(node(v,dis[v]));}}} } int main() {read(n), read(q), read(st);memset(h, -1, sizeof(h));id = n;build(1,1,n,0); // tree -> []build(1,1,n,1); // [] -> treefor(int opt,u,v,w,l,r,i = 1; i <= q; ++i) {read(opt);if(opt == 1) read(u), read(v), read(w), add(u, v, w);else if(opt == 2) read(v), read(l), read(r), read(w), update(1,1,n, v,l,r,w, 0);else read(v), read(l), read(r), read(w), update(1,1,n, v,l,r,w, 1);}dij(st);for(int i = 1; i <= n; ++i) printf("%lld ",dis[i] == __LONG_LONG_MAX__ ? -1 : dis[i]); puts(""); }

轉載于:https://www.cnblogs.com/RRRR-wys/p/10333247.html

總結

以上是生活随笔為你收集整理的Codeforces 786B Legacy (线段树优化建图)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。