1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| #include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std;
typedef long long LL; typedef unsigned long long ULL;
const int maxn=2e5+5, maxq=2e5+5;
struct SEG { int l,r,minw,num; SEG(int l0,int r0,int minw0,int num0) {l=l0, r=r0, minw=minw0, num=num0;} }; bool operator < (const SEG &a,const SEG &b) {return a.r<b.r;}
int n,m,q,qk[maxq],qx[maxq]; vector<pair<int,int>> e[maxn]; ULL w[maxn];
LL dis[maxn]; int d[maxn],d0; void dijkstra() { memset(dis,127,sizeof(dis)); dis[1]=0; priority_queue<pair<LL,int>> Q; Q.push(make_pair(0ll,1)); while (!Q.empty()) { auto tp=Q.top(); Q.pop(); int cur=tp.second; if (-tp.first!=dis[cur]) continue; d[++d0]=cur; for(auto go:e[cur]) if (dis[go.first]>dis[cur]+go.second) { dis[go.first]=dis[cur]+go.second; Q.push(make_pair(-dis[go.first],go.first)); } } }
vector<int> son[maxn]; set<SEG> S1,S2; int indg[maxn],fa[maxn],st[maxn],keynum[maxn]; ULL ans; void init() { fo(i,1,n) for(auto go:e[i]) if (dis[i]+go.second==dis[go.first]) { indg[go.first]++; fa[go.first]=i; } SEG min1(1,1,1,0), min2(1,1,1,0); int j=1; memset(st,127,sizeof(st)); fo(i,2,n) { int cur=d[i]; int minw1=min1.minw, minw2=min2.minw; for(; j<=n && dis[d[j]]<dis[cur]; j++) { st[d[j]]=i; if (w[d[j]]<w[minw1]) minw2=minw1, minw1=d[j]; else if (w[d[j]]<w[minw2]) minw2=d[j]; } if (minw1!=min1.minw || minw2!=min2.minw) { if (min2.l<=min2.r) S2.insert(min2); min2=SEG(min2.r+1,i,minw2,0); } if (minw1!=min1.minw) { if (min1.l<=min1.r) S1.insert(min1); min1=SEG(min1.r+1,i,minw1,0); } keynum[i]=keynum[i-1]+(indg[cur]==1); if (indg[cur]==1) { son[fa[cur]].push_back(i); if (fa[cur]!=1 && fa[cur]==minw1) { min2.num++; ans+=w[minw2]; } else { min1.num++; ans+=w[minw1]; } } min1.r=min2.r=i; } S1.insert(min1); S2.insert(min2); }
int get_num(int l,int r,int minw) { return (minw==1) ?0 :upper_bound(son[minw].begin(),son[minw].end(),r)-upper_bound(son[minw].begin(),son[minw].end(),l-1); }
void cut_cur(set<SEG> &S,int pos,int ty) { set<SEG>::iterator it=S.lower_bound(SEG(0,pos,0,0)); if (it==S.end() || it->l==pos) return; SEG t=*it; S.erase(it); if (ty==1) { S.insert(SEG(t.l, pos-1, t.minw, keynum[pos-1]-keynum[t.l-1]-get_num(t.l,pos-1,t.minw))); S.insert(SEG(pos, t.r, t.minw, keynum[t.r]-keynum[pos-1]-get_num(pos,t.r,t.minw))); } else { S.insert(SEG(t.l, pos-1, t.minw, get_num(t.l,pos-1,(S1.lower_bound(SEG(0,t.l,0,0)))->minw))); S.insert(SEG(pos, t.r, t.minw, get_num(pos,t.r,(S1.lower_bound(SEG(0,pos,0,0)))->minw))); } }
ULL Ans[maxn],w_old[maxn]; int main() { scanf("%d %d %d",&n,&m,&q); fo(i,1,n) { LL x; scanf("%lld",&x); w[i]=x; } fo(i,1,m) { int u,v,d; scanf("%d %d %d",&u,&v,&d); e[u].push_back(make_pair(v,d)), e[v].push_back(make_pair(u,d)); } fo(i,1,q) { scanf("%d %d",&qk[i],&qx[i]); w[qk[i]]+=qx[i]; } dijkstra(); init(); memcpy(w_old,w,sizeof(w)); fd(i,q,1) { Ans[i]=ans; int cur=qk[i]; w[cur]-=qx[i]; cut_cur(S1,st[cur],1); cut_cur(S2,st[cur],2); int tl=st[cur], tr=tl-1, tnum=0; set<SEG>::iterator it=S1.lower_bound(SEG(0,tl,0,0)); while (it!=S1.end() && w[cur]<w_old[it->minw]) { SEG t=*it; if (t.minw==cur) { ans-=(ULL)t.num*qx[i]; it++; tl=t.r+1, tr=t.r; continue; } ans-=t.num*w_old[t.minw]; S1.erase(it++); set<SEG>::iterator it2=S2.lower_bound(SEG(0,t.l,0,0)); while (it2!=S2.end() && it2->l<=t.r) { ans-=w_old[it2->minw]*it2->num; S2.erase(it2++); } int num=get_num(t.l,t.r,cur); ans+=w[t.minw]*num; S2.insert(SEG(t.l,t.r,t.minw,num)); tnum+=keynum[t.r]-keynum[t.l-1]-num; tr=t.r; } if (tl<=tr) { ans+=tnum*w[cur]; S1.insert(SEG(tl,tr,cur,tnum)); } tl=tr+1; tnum=0; it=S2.lower_bound(SEG(0,tl,0,0)); while (it!=S2.end() && w[cur]<w_old[it->minw]) { tr=it->r; tnum+=it->num; ans-=it->num*w_old[it->minw]; S2.erase(it++); } if (tl<=tr) { ans+=tnum*w[cur]; S2.insert(SEG(tl,tr,cur,tnum)); } w_old[cur]-=qx[i]; } Ans[0]=ans; fo(i,0,q) printf("%llu\n",Ans[i]); }
|