Submission #398035

#TimeUsernameProblemLanguageResultExecution timeMemory
398035Charis02Election Campaign (JOI15_election_campaign)C++14
100 / 100
461 ms67912 KiB
/* https://oj.uz/problem/view/JOI15_election_campaign O(NM) Code */ #include<iostream> #include<vector> #include<string.h> #define ll long long #define pi pair < ll,ll > #define mp(a,b) make_pair(a,b) #define MAX_N 200004 using namespace std; vector < int > graph[MAX_N]; vector < pair < pi,ll > > tours[MAX_N]; ll jump_memo[MAX_N][21]; ll tin[MAX_N],tout[MAX_N],cur_time; ll dp[MAX_N],S[MAX_N]; ll seg[4*MAX_N]; int n,m; int jump(int x,int i) // find 2^i -th ancestor of x { if(jump_memo[x][i] != -1) return jump_memo[x][i]; return jump_memo[x][i] = jump(jump(x,i-1),i-1); } bool is_ancestor(int x,int y) // returns if x is ancestor of y { return (tin[x] <= tin[y] && tout[x] >= tout[y]); } int get_lca(int x,int y) { if(is_ancestor(x,y)) return x; if(is_ancestor(y,x)) return y; for(int i = 20;i >= 0;i--) { if(!is_ancestor(jump(x,i),y)) x = jump(x,i); } return jump(x,0); } void dfs(int cur,int par) { tin[cur] = cur_time++; jump_memo[cur][0] = par; for(int i = 0;i < graph[cur].size();i++) { int v = graph[cur][i]; if(v == par) continue; dfs(v,cur); } tout[cur] = cur_time++; return; } void initialize() { memset(jump_memo,-1,sizeof jump_memo); memset(dp,-1,sizeof dp); dfs(1,1); return; } void input() { int a,b,c; cin >> n; for(int i = 0;i < n-1;i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } initialize(); cin >> m; for(int i = 0;i < m;i++) { cin >> a >> b >> c; tours[get_lca(a,b)].push_back(mp(mp(a,b),c)); } return; } void update(int low,int high,int pos,int slow,int val) { if(low == high && low == slow) { seg[pos] = val; return; } if(low>slow||high<slow) return; int mid = (low+high)/2; update(low,mid,pos*2+1,slow,val); update(mid+1,high,pos*2+2,slow,val); seg[pos] = seg[pos*2+1]+seg[pos*2+2]; return; } ll query(int low,int high,int pos,int slow,int shigh) { if(low >= slow && high <= shigh) return seg[pos]; if(low>shigh || high < slow) return 0; int mid = (low+high)/2; return query(low,mid,pos*2+1,slow,shigh) + query(mid+1,high,pos*2+2,slow,shigh); } ll go(int cur,int target) { return query(0,2*n,0,tin[cur],tin[target])+S[target]; } ll calc(int cur,int par) { if(dp[cur] != -1) return dp[cur]; for(int i = 0;i < graph[cur].size();i++) { int v = graph[cur][i]; if(v==par) continue; S[cur] += calc(v,cur); } dp[cur] = S[cur]; for(int i = 0;i < graph[cur].size();i++) { int v = graph[cur][i]; if(v==par) continue; update(0,2*n,0,tin[v],S[cur]-dp[v]); update(0,2*n,0,tout[v],-S[cur]+dp[v]); } for(int i = 0;i < tours[cur].size();i++) { int x = tours[cur][i].first.first; int y = tours[cur][i].first.second; int C = tours[cur][i].second; dp[cur] = max(dp[cur],go(cur,x)+go(cur,y) - S[cur] + C); // S[cur] is counted twice } return dp[cur]; } void solve() { cout << calc(1,1) << endl; } int main() { input(); solve(); return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'long long int calc(int, int)':
election_campaign.cpp:147:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:158:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:168:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |     for(int i = 0;i < tours[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
#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...