#include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <set> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; #define ST first #define ND second #define MP make_pair #define PB push_back typedef long long LL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) //#define DEBUG const int INF=INT_MAX; const int MAXN=200000+5000; const int MAXK=500000+5; int n, m, k; struct node; struct substance{ int g; int last; int next; int weight; int list_id; node* root; int pos; }; substance ss[MAXN]; struct edge { int c,d; int step; int i; }; edge reaction[MAXK]; int edgei; int cmpEdges (const void * a, const void * b) { edge *e1=(edge*)a; edge *e2=(edge*)b; if (e1->step!=e2->step) { return e1->step-e2->step; } return e1->i-e2->i; } void joinLists(int t, int f, int w){ int lastt, lastf; lastt=ss[t].last; lastf=ss[f].last; ss[lastt].next=f; ss[lastt].weight=w; ss[t].last=lastf; ss[f].last=0; // empty } struct node { int left, right; node *ln; node *rn; // node *up; int maxVal; }; //node nodes[4*MAXN]; int nodesi; node *getNode() { // return &nodes[nodesi++]; return new node; } node basenodes[MAXN]; node *buildTree(int li, int ri){ node *root; #ifdef DEBUG assert(li!=ri); #endif if (li+1==ri) { root=&basenodes[li]; root->left=li; root->right=ri; root->ln=root->rn=0; } else { root=getNode(); root->left=li; root->right=ri; int half=(li+ri)/2; root->ln=buildTree(li, half); root->rn=buildTree(half, ri); root->maxVal=max(root->ln->maxVal,root->rn->maxVal); } return root; } int getMaxVal(node *root, int li, int ri) { if (root->left>=li && root->right<=ri) { return root->maxVal; } #ifdef DEBUG assert(li<root->right && ri>root->left); #endif int maxVal=-1; int half=(root->left+root->right)/2; if (li<half) { int branchMax=getMaxVal(root->ln, li, ri); if (maxVal<branchMax) { maxVal=branchMax; } } if (ri>half) { int branchMax=getMaxVal(root->rn, li, ri); if (maxVal<branchMax) { maxVal=branchMax; } } #ifdef DEBUG assert(maxVal>-1); #endif return maxVal; } int main() { //ios_base::sync_with_stdio(0); SI(n); SI(m); SI(k); FOR(i,1,n) { SI(ss[i].g); ss[i].last=i; } REP(i, m) { int t,f; SI(f); SI(t); joinLists(t,f,i); } int bnodes_count=0; FOR(i, 1, n) { if (ss[i].last!=0) { #ifdef DEBUG assert(ss[i].list_id==0); #endif int firstNode=bnodes_count; ss[i].list_id=i; int ii=i; while (ss[ii].next) { basenodes[bnodes_count].maxVal=ss[ii].weight; bnodes_count++; ii=ss[ii].next; ss[ii].pos=bnodes_count; #ifdef DEBUG assert(ss[ii].list_id==0); #endif ss[ii].list_id=i; } if (firstNode<bnodes_count) { ss[i].pos=firstNode; ss[i].root=buildTree(firstNode, bnodes_count); } } } edgei=0; // first free REP(i, k) { int c,d; SI(c); SI(d); if (ss[c].list_id==ss[d].list_id) { edge &r=reaction[edgei]; edgei++; r.c=c; r.d=d; r.i=i; node *root=ss[ss[c].list_id].root; #ifdef DEBUG assert(root); #endif if (ss[c].pos<ss[d].pos) { r.step=getMaxVal(root,ss[c].pos,ss[d].pos); } else { r.step=getMaxVal(root,ss[d].pos,ss[c].pos); } } } qsort(reaction, edgei, sizeof(edge), cmpEdges); LL residue=0; REP(i, edgei) { edge &r=reaction[i]; LL q=min(ss[r.c].g,ss[r.d].g); ss[r.c].g-=q; ss[r.d].g-=q; residue+=2*q; } printf("%lld\n",residue); return 0; }
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <set> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; #define ST first #define ND second #define MP make_pair #define PB push_back typedef long long LL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) //#define DEBUG const int INF=INT_MAX; const int MAXN=200000+5000; const int MAXK=500000+5; int n, m, k; struct node; struct substance{ int g; int last; int next; int weight; int list_id; node* root; int pos; }; substance ss[MAXN]; struct edge { int c,d; int step; int i; }; edge reaction[MAXK]; int edgei; int cmpEdges (const void * a, const void * b) { edge *e1=(edge*)a; edge *e2=(edge*)b; if (e1->step!=e2->step) { return e1->step-e2->step; } return e1->i-e2->i; } void joinLists(int t, int f, int w){ int lastt, lastf; lastt=ss[t].last; lastf=ss[f].last; ss[lastt].next=f; ss[lastt].weight=w; ss[t].last=lastf; ss[f].last=0; // empty } struct node { int left, right; node *ln; node *rn; // node *up; int maxVal; }; //node nodes[4*MAXN]; int nodesi; node *getNode() { // return &nodes[nodesi++]; return new node; } node basenodes[MAXN]; node *buildTree(int li, int ri){ node *root; #ifdef DEBUG assert(li!=ri); #endif if (li+1==ri) { root=&basenodes[li]; root->left=li; root->right=ri; root->ln=root->rn=0; } else { root=getNode(); root->left=li; root->right=ri; int half=(li+ri)/2; root->ln=buildTree(li, half); root->rn=buildTree(half, ri); root->maxVal=max(root->ln->maxVal,root->rn->maxVal); } return root; } int getMaxVal(node *root, int li, int ri) { if (root->left>=li && root->right<=ri) { return root->maxVal; } #ifdef DEBUG assert(li<root->right && ri>root->left); #endif int maxVal=-1; int half=(root->left+root->right)/2; if (li<half) { int branchMax=getMaxVal(root->ln, li, ri); if (maxVal<branchMax) { maxVal=branchMax; } } if (ri>half) { int branchMax=getMaxVal(root->rn, li, ri); if (maxVal<branchMax) { maxVal=branchMax; } } #ifdef DEBUG assert(maxVal>-1); #endif return maxVal; } int main() { //ios_base::sync_with_stdio(0); SI(n); SI(m); SI(k); FOR(i,1,n) { SI(ss[i].g); ss[i].last=i; } REP(i, m) { int t,f; SI(f); SI(t); joinLists(t,f,i); } int bnodes_count=0; FOR(i, 1, n) { if (ss[i].last!=0) { #ifdef DEBUG assert(ss[i].list_id==0); #endif int firstNode=bnodes_count; ss[i].list_id=i; int ii=i; while (ss[ii].next) { basenodes[bnodes_count].maxVal=ss[ii].weight; bnodes_count++; ii=ss[ii].next; ss[ii].pos=bnodes_count; #ifdef DEBUG assert(ss[ii].list_id==0); #endif ss[ii].list_id=i; } if (firstNode<bnodes_count) { ss[i].pos=firstNode; ss[i].root=buildTree(firstNode, bnodes_count); } } } edgei=0; // first free REP(i, k) { int c,d; SI(c); SI(d); if (ss[c].list_id==ss[d].list_id) { edge &r=reaction[edgei]; edgei++; r.c=c; r.d=d; r.i=i; node *root=ss[ss[c].list_id].root; #ifdef DEBUG assert(root); #endif if (ss[c].pos<ss[d].pos) { r.step=getMaxVal(root,ss[c].pos,ss[d].pos); } else { r.step=getMaxVal(root,ss[d].pos,ss[c].pos); } } } qsort(reaction, edgei, sizeof(edge), cmpEdges); LL residue=0; REP(i, edgei) { edge &r=reaction[i]; LL q=min(ss[r.c].g,ss[r.d].g); ss[r.c].g-=q; ss[r.d].g-=q; residue+=2*q; } printf("%lld\n",residue); return 0; } |