Submission #568887

#TimeUsernameProblemLanguageResultExecution timeMemory
568887tqbfjotldReconstruction Project (JOI22_reconstruction)C++14
42 / 100
5095 ms468736 KiB
#include <bits/stdc++.h> using namespace std; set<pair<int,int> > mst[505]; int A[100005]; int B[100005]; int W[100005]; int find_min(int node, int dest, int pa){ if (node==dest) return -1; for (auto x : mst[node]){ if (x.first==pa) continue; int res = find_min(x.first,dest,node); if (res!=-2){ if (res==-1) return x.second; if (W[res]<W[x.second]) return res; else return x.second; } } return -2; } int n; vector<pair<int,int> > add,del; const long long INF = 1000000010; unordered_map<int,long long> fenw[2]; void upd(int pos, int val, int id){ pos++; while (pos<INF){ fenw[id][pos]+=val; pos+=(pos&-pos); } } long long qu(int pos, int id){ long long ans = 0; pos++; while (pos>0){ ans += fenw[id][pos]; pos -= pos&-pos; } return ans; } long long ans[1000005]; int main(){ scanf("%d",&n); int m; scanf("%d",&m); vector<pair<int,pair<int,int>>> edgel; for (int x = 0; x<m; x++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); edgel.push_back({c,{a,b}}); } sort(edgel.begin(),edgel.end()); for (int x = 0; x<m; x++){ A[x] = edgel[x].second.first; B[x] = edgel[x].second.second; W[x] = edgel[x].first; } for (int x = 0; x<m; x++){ int t = find_min(A[x],B[x],-1); if (t==-2){ add.push_back({-1,x}); mst[A[x]].insert({B[x],x}); mst[B[x]].insert({A[x],x}); continue; } //printf("swap %d to %d at q %d\n",t,x,(W[x]+W[t])/2); del.push_back({(W[x]+W[t])/2,t}); mst[A[t]].erase(mst[A[t]].lower_bound({B[t],t})); mst[B[t]].erase(mst[B[t]].lower_bound({A[t],t})); add.push_back({(W[x]+W[t])/2,x}); mst[A[x]].insert({B[x],x}); mst[B[x]].insert({A[x],x}); } sort(add.begin(),add.end()); sort(del.begin(),del.end()); int q; scanf("%d",&q); vector<pair<int,int> > queries; for (int x = 0; x<q; x++){ int a; scanf("%d",&a); queries.push_back({a,x}); } sort(queries.begin(),queries.end()); int i1 = 0; int i2 = 0; for (auto x : queries){ while (i1<add.size() && x.first>add[i1].first){ auto t = add[i1]; upd(W[t.second],W[t.second],0); upd(W[t.second],1,1); //printf("add %d\n",t.second); i1++; } while (i2<del.size() && x.first>del[i2].first){ auto t = del[i2]; upd(W[t.second],-W[t.second],0); upd(W[t.second],-1,1); //printf("del %d\n",t.second); i2++; } //printf("qu %d\n",x.first); long long sum1 = qu(x.first,0); long long sumtot = qu(INF-5,0); long long num1 = qu(x.first,1); long long numtot = n-1; ans[x.second] = num1*x.first-sum1 + (sumtot-sum1)-x.first*(numtot-num1); } for (int x = 0; x<q; x++){ printf("%lld\n",ans[x]); } }

Compilation message (stderr)

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:96:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         while (i1<add.size() && x.first>add[i1].first){
      |                ~~^~~~~~~~~~~
reconstruction.cpp:103:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         while (i2<del.size() && x.first>del[i2].first){
      |                ~~^~~~~~~~~~~
reconstruction.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |     scanf("%d",&m);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         scanf("%d%d%d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
reconstruction.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         scanf("%d",&a);
      |         ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...