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,intl,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千題計劃311:bzoj5017: [Snoi2017]炸彈(線段樹優化tarjan構圖)
#include<cstdio> #include<vector> #include<iostream> #include<algorithm> using namespace std; const int mod=1e9+7;
bzoj千題計劃319:bzoj2865: 字串識別(字尾自動機 + 線段樹)
#include<map> #include<cstdio> #include<cstring> #include<algorithm> #define N 500001 using namespace std; char s[
bzoj千題計劃314:bzoj3238: [Ahoi2013]差異(字尾陣列+st表+單調棧)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 500001 int n
bzoj千題計劃308:bzoj4589: 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千題計劃315:bzoj3172: [Tjoi2013]單詞(AC自動機)
#include<queue> #include<cstdio> #include<cstring> using namespace std; #define N 2000001 using namespace std; int pos[20
bzoj千題計劃310:bzoj5285: [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千題計劃316:bzoj3173: [Tjoi2013]最長上升子序列(二分+樹狀陣列)
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 #define lowbit(x) x&-x
bzoj千題計劃309:bzoj4332: JSOI2012 分零食(分治+FFT)
#include<cmath> #include<cstdio> #include<algorithm> using namespace std; const int M=1<<17; #define N 10001 int m,
bzoj千題計劃321:bzoj5251: [2018多省省隊聯測]劈配(網路流 + 二分)
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d
bzoj千題計劃312:bzoj2119: 股市的預測(字尾陣列+st表)
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d
bzoj千題計劃317:bzoj4650: [Noi2016]優秀的拆分(字尾陣列+差分)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 30002 using namespace std; int n;
bzoj千題計劃318:bzoj1396: 識別子串(字尾自動機 + 線段樹)
#include<cstdio> #include<cstring> #include<algorithm> #define N 100001 using namespace std; char s[N]; int ch[N<&
bzoj千題計劃323:bzoj1951: [Sdoi2010]古代豬文(Lucas+CRT+尤拉定理)
#include<cmath> #include<cstdio> #include<iostream> using namespace std; const int mod=999911659; const int phi=mod-1; typ
bzoj千題計劃322:bzoj2561: 最小生成樹(最小割)
#include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> using namespace std;
bzoj千題計劃324:bzoj5249: [2018多省省隊聯測]IIIDX(線段樹)
#include<cmath> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 500001 int d[N
bzoj千題計劃313:bzoj3879: SvT(字尾陣列+st表+單調棧)
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 500001 #define M 3000001 int n,m,mm;
bzoj千題計劃320:bzoj4939: [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,因此道路按照升序排列。在這些道路上可能會有幾場比賽,每一場
HDU1698:Just 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