[LOJ#2255][BZOJ5017][Snoi2017]炸弹

匿名 (未验证) 提交于 2019-12-02 23:48:02

#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #define int LL #define LL long long #define mod 1000000007 #define MAXN 500010 using namespace std; struct edge { 	int u,v,nxt; 	#define u(x)  ed[x].u 	#define v(x)  ed[x].v 	#define n(x)  ed[x].nxt 	#define u2(x) ed2[x].u 	#define v2(x) ed2[x].v 	#define n2(x) ed2[x].nxt }ed[1000000],ed2[1000000]; int first[MAXN],num_e; #define f(x) first[x] int first2[MAXN],num_e2; #define f2(x) first2[x] int n,du[MAXN]; int x[MAXN],r[MAXN]; int dfn[MAXN],low[MAXN],cnt; int stack[MAXN],top; int belong[MAXN],tot; bool v[MAXN]; vector<int> scc[MAXN]; void tarjan(int x) { 	dfn[x]=low[x]=++cnt; 	stack[++top]=x;v[x]=1; 	for(int i=f(x);i;i=n(i)) 	if(!dfn[v(i)])tarjan(v(i)),low[x]=min(low[x],low[v(i)]); 	else if(v[v(i)])low[x]=min(low[x],dfn[v(i)]); 	if(low[x]==dfn[x]) 	{	 		++tot;v[x]=0; 		while(stack[top]!=x) 		{ 			v[stack[top]]=0; 			belong[stack[top]]=tot; 			scc[tot].push_back(stack[top--]); 		} 		belong[x]=tot; 		scc[tot].push_back(stack[top--]); 	} } int L[MAXN],R[MAXN]; inline void add(int u,int v); inline void add2(int u,int v); signed main() { //	freopen("in.txt","r",stdin);  	scanf("%lld",&n); 	for(int i=1;i<=n;i++) 	scanf("%lld%lld",&x[i],&r[i]); 	for(int i=1;i<=n;i++) 	{ 		for(int j=i-1;j;j--)   if(x[i]<=x[j]+r[j]){add(i,j);break;} 		for(int j=i+1;j<=n;j++)if(x[i]>=x[j]-r[j]){add(i,j);break;} 	} 	for(int i=1;i<=n;i++) 	if(!dfn[i])tarjan(i); 	for(int i=1;i<=num_e;i++) 	if(belong[u(i)]!=belong[v(i)]) 	add2(belong[u(i)],belong[v(i)]),du[belong[v(i)]]++; 	memset(L,0x7f,sizeof(L)); 	memset(R,-0x7f,sizeof(R)); 	for(int i=1;i<=tot;i++) 	for(int j=0;j<scc[i].size();j++) 	L[i]=min(L[i],scc[i][j]),R[i]=max(R[i],scc[i][j]); 	queue<int> q; 	for(int i=1;i<=tot;i++) 	if(!du[i])q.push(i); 	while(!q.empty()) 	{ 		int k=q.front();q.pop(); 		for(int i=f2(k);i;i=n2(i)) 		{	 			du[v2(i)]--; 			if(!du[v2(i)])q.push(v2(i)); 			L[v2(i)]=min(L[v2(i)],L[k]); 			R[v2(i)]=max(R[v2(i)],R[k]); 		} 	} 	LL ans=0; 	for(int i=1;i<=n;i++) 		ans=(ans+i*(R[belong[i]]-L[belong[i]]+1))%mod; 	printf("%lld\n",ans); } inline void add(int u,int v) { 	++num_e; 	u(num_e)=u; 	v(num_e)=v; 	n(num_e)=f(u); 	f(u)=num_e; } inline void add2(int u,int v) { 	++num_e2; 	u2(num_e2)=u; 	v2(num_e2)=v; 	n2(num_e2)=f2(u); 	f2(u)=num_e2; } 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!