标签:
题目链接:
Time Limit: 12000/6000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar()); for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + ‘0‘); putchar(‘\n‘); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=2e5+20; const int maxn=1e4+220; const double eps=1e-12; int n,m,w[N],a[N],b[N],p[N]; int x,y; vector<int>st[N],en[N]; int findset(int x) { if(x==p[x])return x; return p[x]=findset(p[x]); } int same(int x,int y) { int fx=findset(x),fy=findset(y); if(fx>fy)p[fy]=fx; else if(fx<fy)p[fx]=fy; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { For(i,1,n-1)read(w[i]); int high=max(n,m)+10; for(int i=0;i<=high;i++) { p[i]=i; st[i].clear(); en[i].clear(); } For(i,1,m) { read(a[i]);read(b[i]); if(a[i]>b[i])swap(a[i],b[i]); int r=a[i]; while(1) { r=findset(r); if(r>=b[i])break; same(r,r+1); st[i].push_back(r); } } for(int i=0;i<=high;i++)p[i]=i; for(int i=m;i>0;i--)//找到第i天要关闭的线段 { int r=a[i]; while(1) { r=findset(r); if(r>=b[i])break; same(r,r+1); en[i].push_back(r); } } int ans=0; for(int i=1;i<=m;i++) { int len=st[i].size(); for(int j=0;j<len;j++)//加上这一天要打开的线段 { int x=st[i][j]; ans+=w[x]; } print(ans); len=en[i].size(); for(int j=0;j<len;j++)//关闭这天需要关闭的线段; { int x=en[i][j]; ans-=w[x]; } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5785910.html