1. 程式人生 > >bzoj千題計劃311:bzoj5017: [Snoi2017]炸彈(線段樹優化tarjan構圖)

bzoj千題計劃311:bzoj5017: [Snoi2017]炸彈(線段樹優化tarjan構圖)

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
 
using namespace std;
 
const int mod=1e9+7;
 
#define N 500001 
 
typedef long long LL;
 
int n;
 
int id[N*3],num;
int front[N*3],to[N*21],nxt[N*21],from[N*21],tot;
 
LL pos[N],rad[N];
 
int dfn[N*3],low[N*3],tim; int st[N*3],top; bool vis[N*3]; int cnt; int bl[N*3],mi[N*3],mx[N*3]; vector<int>V[N*3]; int in[N*3]; int q[N*3]; template<typename T> void read(T &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; } void add(int u,int v) { to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; from[tot]=u; //printf("%d %d\n",u,v); } /*void add2(int u,int v) { to2[++tot2]=v; nxt2[tot2]=front2[u]; front2[u]=tot2; }*/ void build(int k,int
l,int r) { id[k]=++num; if(l==r) { add(id[k],l); return; } int mid=l+r>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); add(id[k],id[k<<1]); add(id[k],id[k<<1|1]); } void burst(int k,int l,int r,int opl,int opr,int who) { if(l>=opl && r<=opr) { add(who,id[k]); return; } int mid=l+r>>1; if(opl<=mid) burst(k<<1,l,mid,opl,opr,who); if(opr>mid) burst(k<<1|1,mid+1,r,opl,opr,who); } void init() { read(n); num=n; build(1,1,n); for(int i=1;i<=n;++i) read(pos[i]),read(rad[i]); for(int i=1;i<=n;++i) burst(1,1,n,lower_bound(pos+1,pos+n+1,pos[i]-rad[i])-pos,upper_bound(pos+1,pos+n+1,pos[i]+rad[i])-pos-1,i); } void tarjan(int x) { dfn[x]=low[x]=++tim; st[++top]=x; vis[x]=true; for(int i=front[x];i;i=nxt[i]) if(!dfn[to[i]]) { tarjan(to[i]); low[x]=min(low[x],low[to[i]]); } else if(vis[to[i]]) low[x]=min(low[x],dfn[to[i]]); int y; if(low[x]==dfn[x]) { cnt++; mi[cnt]=n+1; mx[cnt]=0; while(x!=y) { y=st[top--]; vis[y]=false; bl[y]=cnt; if(y<=n) { mi[cnt]=min(mi[cnt],y); mx[cnt]=max(mx[cnt],y); } } } } void rebuild() { for(int i=1;i<=tot;++i) if(bl[from[i]]!=bl[to[i]]) { //add2(bl[from[i]],bl[to[i]]); V[bl[from[i]]].push_back(bl[to[i]]); in[bl[to[i]]]++; } } void topsort() { int head=1,tail=1; for(int i=1;i<=cnt;++i) if(!in[i]) q[tail++]=i; int now,siz,t; while(head<tail) { now=q[head++]; siz=V[now].size(); for(int i=0;i<siz;++i) { t=V[now][i]; in[t]--; if(!in[t]) q[tail++]=t; } } for(int i=tail-1;i;--i) { now=q[i]; siz=V[now].size(); for(int i=0;i<siz;++i) { t=V[now][i]; mi[now]=min(mi[now],mi[t]); mx[now]=max(mx[now],mx[t]); } } } void work() { // printf("%d %d\n",num,tot); for(int i=1;i<=num;++i) if(!dfn[i]) tarjan(i); rebuild(); topsort(); int ans=0; for(int i=1;i<=n;++i) { ans+=1LL*i*(mx[bl[i]]-mi[bl[i]]+1)%mod; ans-=ans>=mod ? mod : 0; } printf("%d",ans); } int main() { init(); work(); }

相關推薦

bzoj計劃311bzoj5017: [Snoi2017]炸彈線段優化tarjan構圖

#include<cstdio> #include<vector> #include<iostream> #include<algorithm> using namespace std; const int mod=1e9+7;

bzoj計劃319bzoj2865: 字串識別字尾自動機 + 線段

#include<map> #include<cstdio> #include<cstring> #include<algorithm> #define N 500001 using namespace std; char s[

bzoj計劃314bzoj3238: [Ahoi2013]差異字尾陣列+st表+單調棧

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 500001 int n

bzoj計劃308bzoj4589: Hard Nim倍增FWT+生成函式

#include<cstdio> #include<cstring> using namespace std; #define N 50001 const int mod=1e9+7; const int M=1<<16; int inv

bzoj計劃315bzoj3172: [Tjoi2013]單詞AC自動機

#include<queue> #include<cstdio> #include<cstring> using namespace std; #define N 2000001 using namespace std; int pos[20

bzoj計劃310bzoj5285: [Hnoi2018]尋寶遊戲(思維+雜湊)

#include<cstdio> #include<algorithm> #define N 5001 using namespace std; const int mod=1e9+7; int bit[N]; char s[N]; int has

bzoj計劃316bzoj3173: [Tjoi2013]最長上升子序列二分+狀陣列

#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 #define lowbit(x) x&-x

bzoj計劃309bzoj4332: JSOI2012 分零食分治+FFT

#include<cmath> #include<cstdio> #include<algorithm> using namespace std; const int M=1<<17; #define N 10001 int m,

bzoj計劃321bzoj5251: [2018多省省隊聯測]劈配網路流 + 二分

#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d

bzoj計劃312bzoj2119: 股市的預測字尾陣列+st表

#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d

bzoj計劃317bzoj4650: [Noi2016]優秀的拆分字尾陣列+差分

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 30002 using namespace std; int n;

bzoj計劃318bzoj1396: 識別子串字尾自動機 + 線段

#include<cstdio> #include<cstring> #include<algorithm> #define N 100001 using namespace std; char s[N]; int ch[N<&

bzoj計劃323bzoj1951: [Sdoi2010]古代豬文Lucas+CRT+尤拉定理

#include<cmath> #include<cstdio> #include<iostream> using namespace std; const int mod=999911659; const int phi=mod-1; typ

bzoj計劃322bzoj2561: 最小生成樹最小割

#include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> using namespace std;

bzoj計劃324bzoj5249: [2018多省省隊聯測]IIIDX線段

#include<cmath> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 500001 int d[N

bzoj計劃313bzoj3879: SvT字尾陣列+st表+單調棧

#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 500001 #define M 3000001 int n,m,mm;

bzoj計劃320bzoj4939: [Ynoi2016]掉進兔子洞莫隊 + bitset

#include<cmath> #include<cstdio> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> us

bzoj 3073】Journeys線段優化建圖

傳送門biu~ 線段樹的每個節點代表一個區間,建兩棵線段樹。 出線段樹每個點向父節點連邊0,表示如果能從這個區間出發也就可以從父區間出發。入線段樹每個點向子節點連邊0,表示如果能到達這個區間也就可以到達子區間。 入線段樹每個點向出線段樹的平行結點連邊0,

題解CF115E線段優化dp

定義 可能 bit tput space nbsp sans odi bsp 題目描述 你是一個賽車比賽的組織者,想在線性王國中安排一些比賽。 線性王國有n條連續的從左到右的道路。道路從左到右依次編號為從1到n,因此道路按照升序排列。在這些道路上可能會有幾場比賽,每一場

HDU1698Just a Hook線段

Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is