답안 #1102164

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102164 2024-10-17T15:08:43 Z peacebringer1667 나일강 (IOI24_nile) C++17
100 / 100
98 ms 21168 KB
#include "nile.h"
#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 2e9;

struct CTDL{
	int u = 0,v = 0,dist = 0;
	bool operator <(const CTDL&other) const{
		return (dist < other.dist);
	}
}; 

struct segment_tree{
	int seg[4*maxn];
	
	void init(int l,int r,int v){
		seg[v] = inf;
		
		if (l == r) return;
		int w = (l + r)/2;
		init(l,w,2*v + 1);
		init(w + 1,r,2*v + 2);
	}
	
	void update(int l,int r,int v,int p,int val){
		if (l > p || r < p) return;
		if (l == r){
			seg[v] = min(seg[v],val);
			return;
		}
		
		int w = (l + r)/2;
		update(l,w,2*v + 1,p,val);
		update(w + 1,r,2*v + 2,p,val);
		seg[v] = min(seg[2*v + 1],seg[2*v + 2]);
	}
	
	int calc(int l,int r,int v,int lx,int rx){
		if (l > rx || r < lx) return inf;
		if (l >= lx && r <= rx) return seg[v];
		
		int w = (l + r)/2;
		return min(calc(l,w,2*v + 1,lx,rx),calc(w + 1,r,2*v + 2,lx,rx));
	}
} seg[2];

vector <CTDL> lst,skip_list;
int a[maxn],W[maxn],A[maxn],B[maxn];
pir Q[maxn],arr[maxn];


struct DSU{
	int par[maxn],M[maxn][2],L[maxn],R[maxn],sz[maxn];
	ll sum[maxn],cost[maxn];
	
	int findp(int x){
		return (x == par[x]) ? x : par[x] = findp(par[x]);
	}
	
	void init(int n){
		for (int i = 1 ; i <= n ; i++){
			sum[i] = a[i];
			sz[i] = 1;
			M[i][i % 2] = abs(a[i]);
			M[i][(i % 2) ^ 1] = INT_MAX;
			
			L[i] = i;
			R[i] = i;
			par[i] = i;
			
			cost[i] = 0;
		}
	}
	
	ll add(int u,int v,int n){
		int x = findp(u),y = findp(v);
		if (x != y){
			ll res = sum[x] + sum[y];
			ll tmp = cost[x] + cost[y];
			
			sz[x] += sz[y];
			sum[x] += sum[y];
			
			M[x][0] = min(M[x][0],M[y][0]);
			M[x][1] = min(M[x][1],M[y][1]);
			
			L[x] = min(L[x],L[y]);
			R[x] = max(R[x],R[y]);
			
			par[y] = x;
			
			if (sz[x] % 2){
			  res += M[x][L[x] % 2];
		      
		      int id = 1 - (L[x] % 2);
		      res = min(res,sum[x] + (ll)seg[id].calc(1,n,0,L[x],R[x]));
			}
		
			cost[x] = res;
			return res - tmp;
		}
		
		return 0;
	}
	
	ll recalc(int u,int n){
		int x = findp(u);
		
		ll tmp = cost[x];
		ll res = cost[x];
		
		if (sz[x] % 2 && sz[x] > 1){
			int id = (L[x] % 2) ^ 1;
		    res = min(res,sum[x] + (ll)seg[id].calc(1,n,0,L[x],R[x]));
		}
		cost[x] = res;
		return res - tmp;
	}
	
} dsu;

void prepare(vector <int> W,vector <int> A,vector <int> B,vector <int> E){
	int n = W.size();
	int q = E.size();
	
	lst.clear();
	skip_list.clear();
	
	seg[0].init(1,n,0);
	seg[1].init(1,n,0);
	
	//
	for (int i = 1 ; i <= n ; i++) arr[i] = {W[i - 1],B[i - 1] - A[i - 1]};
	sort(arr + 1,arr + 1 + n);
	
	for (int i = 1 ; i <= n ; i++)
	  a[i] = arr[i].se;
	
	for (int i = 1 ; i <= q ; i++)
		Q[i] = {E[i - 1],i};
	sort(Q + 1,Q + 1 + q);
	//////
	
	//////// connected components
	for (int i = 1 ; i < n ; i++){
	  lst.push_back({i,i + 1,arr[i + 1].fi - arr[i].fi});
      if (i + 2 <= n)
        skip_list.push_back({i + 1,i + 1,arr[i + 2].fi - arr[i].fi});
	}
	
	sort(skip_list.begin(),skip_list.end());
	sort(lst.begin(),lst.end());
	
	dsu.init(n);
	///////
}

vector <ll> calculate_costs(vector <int> W,vector <int> A,vector <int> B,vector <int> E){
	vector <ll> res;
	int n = W.size(),q = E.size();
	res.resize(q);
	prepare(W,A,B,E);
	
	ll cur = 0;
	for (int x : A) cur += x;
	
	int p = 0,p_skip = 0;
	for (int i = 1 ; i <= q ; i++){
		int D = Q[i].fi,id = Q[i].se;
		
		while (p_skip < skip_list.size() && skip_list[p_skip].dist <= D){
			int u = skip_list[p_skip].u;
			seg[u % 2].update(1,n,0,u,abs(a[u]));
			
			cur += dsu.recalc(u,n);
			
			p_skip++;
		}
		
		while (p < lst.size() && lst[p].dist <= D){
			cur += dsu.add(lst[p].u,lst[p].v,n);
			p++;
		}
		
		res[id - 1] = cur;
	}
	
	return res;
}

Compilation message

nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:179:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<CTDL>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |   while (p_skip < skip_list.size() && skip_list[p_skip].dist <= D){
      |          ~~~~~~~^~~~~~~~~~~~~~~~~~
nile.cpp:188:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<CTDL>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  188 |   while (p < lst.size() && lst[p].dist <= D){
      |          ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 3 ms 8784 KB Output is correct
3 Correct 3 ms 8784 KB Output is correct
4 Correct 2 ms 8680 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 18224 KB Output is correct
2 Correct 47 ms 18088 KB Output is correct
3 Correct 49 ms 18168 KB Output is correct
4 Correct 47 ms 18224 KB Output is correct
5 Correct 47 ms 18224 KB Output is correct
6 Correct 48 ms 18224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 16952 KB Output is correct
2 Correct 52 ms 17052 KB Output is correct
3 Correct 71 ms 17104 KB Output is correct
4 Correct 66 ms 17204 KB Output is correct
5 Correct 56 ms 17212 KB Output is correct
6 Correct 75 ms 17200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 3 ms 8784 KB Output is correct
3 Correct 3 ms 8784 KB Output is correct
4 Correct 2 ms 8680 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 3 ms 8784 KB Output is correct
9 Correct 4 ms 8784 KB Output is correct
10 Correct 3 ms 8956 KB Output is correct
11 Correct 2 ms 8660 KB Output is correct
12 Correct 3 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 3 ms 8784 KB Output is correct
3 Correct 3 ms 8784 KB Output is correct
4 Correct 2 ms 8680 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 48 ms 18224 KB Output is correct
8 Correct 47 ms 18088 KB Output is correct
9 Correct 49 ms 18168 KB Output is correct
10 Correct 47 ms 18224 KB Output is correct
11 Correct 47 ms 18224 KB Output is correct
12 Correct 48 ms 18224 KB Output is correct
13 Correct 50 ms 16952 KB Output is correct
14 Correct 52 ms 17052 KB Output is correct
15 Correct 71 ms 17104 KB Output is correct
16 Correct 66 ms 17204 KB Output is correct
17 Correct 56 ms 17212 KB Output is correct
18 Correct 75 ms 17200 KB Output is correct
19 Correct 3 ms 8784 KB Output is correct
20 Correct 3 ms 8784 KB Output is correct
21 Correct 4 ms 8784 KB Output is correct
22 Correct 3 ms 8956 KB Output is correct
23 Correct 2 ms 8660 KB Output is correct
24 Correct 3 ms 8784 KB Output is correct
25 Correct 61 ms 18484 KB Output is correct
26 Correct 60 ms 18636 KB Output is correct
27 Correct 75 ms 18480 KB Output is correct
28 Correct 74 ms 18488 KB Output is correct
29 Correct 63 ms 18484 KB Output is correct
30 Correct 82 ms 18560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 16952 KB Output is correct
2 Correct 52 ms 17052 KB Output is correct
3 Correct 71 ms 17104 KB Output is correct
4 Correct 66 ms 17204 KB Output is correct
5 Correct 56 ms 17212 KB Output is correct
6 Correct 75 ms 17200 KB Output is correct
7 Correct 75 ms 19256 KB Output is correct
8 Correct 76 ms 19628 KB Output is correct
9 Correct 88 ms 19508 KB Output is correct
10 Correct 85 ms 19508 KB Output is correct
11 Correct 98 ms 19512 KB Output is correct
12 Correct 88 ms 19504 KB Output is correct
13 Correct 87 ms 19504 KB Output is correct
14 Correct 72 ms 19248 KB Output is correct
15 Correct 85 ms 19516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 3 ms 8784 KB Output is correct
4 Correct 3 ms 8784 KB Output is correct
5 Correct 2 ms 8680 KB Output is correct
6 Correct 3 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 48 ms 18224 KB Output is correct
9 Correct 47 ms 18088 KB Output is correct
10 Correct 49 ms 18168 KB Output is correct
11 Correct 47 ms 18224 KB Output is correct
12 Correct 47 ms 18224 KB Output is correct
13 Correct 48 ms 18224 KB Output is correct
14 Correct 50 ms 16952 KB Output is correct
15 Correct 52 ms 17052 KB Output is correct
16 Correct 71 ms 17104 KB Output is correct
17 Correct 66 ms 17204 KB Output is correct
18 Correct 56 ms 17212 KB Output is correct
19 Correct 75 ms 17200 KB Output is correct
20 Correct 3 ms 8784 KB Output is correct
21 Correct 3 ms 8784 KB Output is correct
22 Correct 4 ms 8784 KB Output is correct
23 Correct 3 ms 8956 KB Output is correct
24 Correct 2 ms 8660 KB Output is correct
25 Correct 3 ms 8784 KB Output is correct
26 Correct 61 ms 18484 KB Output is correct
27 Correct 60 ms 18636 KB Output is correct
28 Correct 75 ms 18480 KB Output is correct
29 Correct 74 ms 18488 KB Output is correct
30 Correct 63 ms 18484 KB Output is correct
31 Correct 82 ms 18560 KB Output is correct
32 Correct 75 ms 19256 KB Output is correct
33 Correct 76 ms 19628 KB Output is correct
34 Correct 88 ms 19508 KB Output is correct
35 Correct 85 ms 19508 KB Output is correct
36 Correct 98 ms 19512 KB Output is correct
37 Correct 88 ms 19504 KB Output is correct
38 Correct 87 ms 19504 KB Output is correct
39 Correct 72 ms 19248 KB Output is correct
40 Correct 85 ms 19516 KB Output is correct
41 Correct 82 ms 20800 KB Output is correct
42 Correct 78 ms 21040 KB Output is correct
43 Correct 93 ms 21040 KB Output is correct
44 Correct 92 ms 21040 KB Output is correct
45 Correct 93 ms 21044 KB Output is correct
46 Correct 98 ms 21168 KB Output is correct
47 Correct 93 ms 21040 KB Output is correct
48 Correct 83 ms 20928 KB Output is correct
49 Correct 95 ms 21040 KB Output is correct