답안 #418483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
418483 2021-06-05T11:57:15 Z gs14004 Rooted MST (innopolis2021_final_E) C++17
100 / 100
1000 ms 117708 KB
// shirley smokes weed
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 600005;

struct edg{
	int s, e, x;
	bool operator<(const edg &e)const{
		return x < e.x;
	}
};

struct disj{
	int pa[MAXN];
	void init(int n){
		iota(pa, pa + n + 1, 0);
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		pa[p] = q; return 1;
	}
}disj;

int n, m;
int l[MAXN], r[MAXN], a[MAXN];
vector<lint> vect;
int rev[MAXN];

void dfs(int v){
	if(v <= n){
		vect.push_back(a[v]);
		rev[v] = sz(vect) - 1;
		return;
	}
	dfs(l[v]);
	vect.push_back(-a[v]);
	dfs(r[v]);
}

struct node{
	lint a[2][2];
	void init_pos(lint x){
		a[0][0] = 0;
		a[0][1] = x;
		a[1][0] = 1e18;
		a[1][1] = x;
	}
	void init_neg(lint x){
		a[0][0] = a[1][0] = 0;
		a[0][1] = 1e18;
		a[1][1] = x;
	}
	void init(int pos, lint x){
		if(pos % 2) init_neg(x);
		else init_pos(x);
	}
	node operator+(const node &n)const{
		node x;
		memset(x.a, 0x3f, sizeof(x.a));
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){
				for(int k=0; k<2; k++){
					x.a[i][k] = min(x.a[i][k], a[i][j] + n.a[j][k]);
				}
			}
		}
		return x;
	}
}tree[1 << 21];

void init(int s, int e, int p){
	if(s == e){
		tree[p].init(s, vect[s]);
		return;
	}
	int m = (s+e)/2;
	init(s, m, 2*p);
	init(m+1, e, 2*p+1);
	tree[p] = tree[2*p] + tree[2*p+1];
}

void upd(int pos, int s, int e, int p){
	if(s == e){
		tree[p].init(s, vect[s]);
		return;
	}
	int m = (s+e)/2;
	if(pos <= m) upd(pos, s, m, 2*p);
	else upd(pos, m+1, e, 2*p+1);
	tree[p] = tree[2*p] + tree[2*p+1];
}

void upd(int pos, lint val){
	vect[pos] += val;
	upd(pos, 0, sz(vect) - 1, 1);
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
	vector<edg> v;
	for(int i=0; i<m; i++){
		int s, e, x; scanf("%d %d %d",&s,&e,&x);
		if(s > e) swap(s, e);
		v.push_back({s, e, x});
	}
	for(int i = 2; i <= n; i++) v.push_back({1, i, int(2e9)});
	sort(all(v));
	disj.init(2*n);
	int q = n;
	lint kek = 0;
	for(auto &i : v){
		int L = disj.find(i.s);
		int R = disj.find(i.e);
		if(L == R) continue;
		q++;
		kek += i.x;
		l[q] = L; r[q] = R; a[q] = i.x;
		disj.uni(L, q);
		disj.uni(R, q);
	}
	dfs(q);
	vect.push_back(-1e18);
	kek -= vect.back();
	for(int i=0; i+1<sz(vect); i++){
		vect[i] += vect[i + 1];
		if(i % 2 == 1) vect[i] *= -1;
	}
	init(0, sz(vect) - 1, 1);
	int Q; scanf("%d",&Q);
	while(Q--){
		int pos, val;
		scanf("%d %d",&pos,&val);
		int delta = val - a[pos];
		a[pos] += delta;
		upd(rev[pos], delta);
		if(rev[pos]) upd(rev[pos] - 1, -delta);
		printf("%lld\n", tree[1].a[0][0]  + kek);
	}
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |  scanf("%d %d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~~
Main.cpp:109:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |  for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
      |                              ~~~~~^~~~~~~~~~~~
Main.cpp:112:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   int s, e, x; scanf("%d %d %d",&s,&e,&x);
      |                ~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |  int Q; scanf("%d",&Q);
      |         ~~~~~^~~~~~~~~
Main.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |   scanf("%d %d",&pos,&val);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 4 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 812 ms 101092 KB Output is correct
2 Correct 723 ms 99732 KB Output is correct
3 Correct 340 ms 6668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 378 ms 6576 KB Output is correct
2 Correct 328 ms 9516 KB Output is correct
3 Correct 273 ms 4400 KB Output is correct
4 Correct 887 ms 115664 KB Output is correct
5 Correct 823 ms 110392 KB Output is correct
6 Correct 764 ms 110988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 109956 KB Output is correct
2 Correct 360 ms 116336 KB Output is correct
3 Correct 370 ms 116116 KB Output is correct
4 Correct 361 ms 16036 KB Output is correct
5 Correct 325 ms 13184 KB Output is correct
6 Correct 257 ms 9156 KB Output is correct
7 Correct 529 ms 114416 KB Output is correct
8 Correct 638 ms 114420 KB Output is correct
9 Correct 541 ms 114424 KB Output is correct
10 Correct 529 ms 114444 KB Output is correct
11 Correct 644 ms 114704 KB Output is correct
12 Correct 655 ms 115268 KB Output is correct
13 Correct 543 ms 115732 KB Output is correct
14 Correct 651 ms 116704 KB Output is correct
15 Correct 659 ms 116700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 109956 KB Output is correct
2 Correct 360 ms 116336 KB Output is correct
3 Correct 370 ms 116116 KB Output is correct
4 Correct 361 ms 16036 KB Output is correct
5 Correct 325 ms 13184 KB Output is correct
6 Correct 257 ms 9156 KB Output is correct
7 Correct 529 ms 114416 KB Output is correct
8 Correct 638 ms 114420 KB Output is correct
9 Correct 541 ms 114424 KB Output is correct
10 Correct 529 ms 114444 KB Output is correct
11 Correct 644 ms 114704 KB Output is correct
12 Correct 655 ms 115268 KB Output is correct
13 Correct 543 ms 115732 KB Output is correct
14 Correct 651 ms 116704 KB Output is correct
15 Correct 659 ms 116700 KB Output is correct
16 Correct 457 ms 116380 KB Output is correct
17 Correct 456 ms 114708 KB Output is correct
18 Correct 463 ms 114776 KB Output is correct
19 Correct 370 ms 13892 KB Output is correct
20 Correct 334 ms 10516 KB Output is correct
21 Correct 258 ms 7764 KB Output is correct
22 Correct 876 ms 109976 KB Output is correct
23 Correct 653 ms 112436 KB Output is correct
24 Correct 648 ms 112308 KB Output is correct
25 Correct 637 ms 111124 KB Output is correct
26 Correct 661 ms 109580 KB Output is correct
27 Correct 659 ms 115252 KB Output is correct
28 Correct 671 ms 115848 KB Output is correct
29 Correct 648 ms 114388 KB Output is correct
30 Correct 661 ms 111632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 904 ms 98580 KB Output is correct
2 Correct 911 ms 107628 KB Output is correct
3 Correct 900 ms 106644 KB Output is correct
4 Correct 898 ms 106772 KB Output is correct
5 Correct 934 ms 105240 KB Output is correct
6 Correct 917 ms 106696 KB Output is correct
7 Correct 943 ms 110816 KB Output is correct
8 Correct 1000 ms 109348 KB Output is correct
9 Correct 1000 ms 108604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 4 ms 716 KB Output is correct
6 Correct 377 ms 55584 KB Output is correct
7 Correct 388 ms 55400 KB Output is correct
8 Correct 377 ms 56028 KB Output is correct
9 Correct 483 ms 55616 KB Output is correct
10 Correct 203 ms 6520 KB Output is correct
11 Correct 182 ms 7212 KB Output is correct
12 Correct 157 ms 5820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 4 ms 716 KB Output is correct
6 Correct 812 ms 101092 KB Output is correct
7 Correct 723 ms 99732 KB Output is correct
8 Correct 340 ms 6668 KB Output is correct
9 Correct 378 ms 6576 KB Output is correct
10 Correct 328 ms 9516 KB Output is correct
11 Correct 273 ms 4400 KB Output is correct
12 Correct 887 ms 115664 KB Output is correct
13 Correct 823 ms 110392 KB Output is correct
14 Correct 764 ms 110988 KB Output is correct
15 Correct 358 ms 109956 KB Output is correct
16 Correct 360 ms 116336 KB Output is correct
17 Correct 370 ms 116116 KB Output is correct
18 Correct 361 ms 16036 KB Output is correct
19 Correct 325 ms 13184 KB Output is correct
20 Correct 257 ms 9156 KB Output is correct
21 Correct 529 ms 114416 KB Output is correct
22 Correct 638 ms 114420 KB Output is correct
23 Correct 541 ms 114424 KB Output is correct
24 Correct 529 ms 114444 KB Output is correct
25 Correct 644 ms 114704 KB Output is correct
26 Correct 655 ms 115268 KB Output is correct
27 Correct 543 ms 115732 KB Output is correct
28 Correct 651 ms 116704 KB Output is correct
29 Correct 659 ms 116700 KB Output is correct
30 Correct 457 ms 116380 KB Output is correct
31 Correct 456 ms 114708 KB Output is correct
32 Correct 463 ms 114776 KB Output is correct
33 Correct 370 ms 13892 KB Output is correct
34 Correct 334 ms 10516 KB Output is correct
35 Correct 258 ms 7764 KB Output is correct
36 Correct 876 ms 109976 KB Output is correct
37 Correct 653 ms 112436 KB Output is correct
38 Correct 648 ms 112308 KB Output is correct
39 Correct 637 ms 111124 KB Output is correct
40 Correct 661 ms 109580 KB Output is correct
41 Correct 659 ms 115252 KB Output is correct
42 Correct 671 ms 115848 KB Output is correct
43 Correct 648 ms 114388 KB Output is correct
44 Correct 661 ms 111632 KB Output is correct
45 Correct 904 ms 98580 KB Output is correct
46 Correct 911 ms 107628 KB Output is correct
47 Correct 900 ms 106644 KB Output is correct
48 Correct 898 ms 106772 KB Output is correct
49 Correct 934 ms 105240 KB Output is correct
50 Correct 917 ms 106696 KB Output is correct
51 Correct 943 ms 110816 KB Output is correct
52 Correct 1000 ms 109348 KB Output is correct
53 Correct 1000 ms 108604 KB Output is correct
54 Correct 377 ms 55584 KB Output is correct
55 Correct 388 ms 55400 KB Output is correct
56 Correct 377 ms 56028 KB Output is correct
57 Correct 483 ms 55616 KB Output is correct
58 Correct 203 ms 6520 KB Output is correct
59 Correct 182 ms 7212 KB Output is correct
60 Correct 157 ms 5820 KB Output is correct
61 Correct 0 ms 332 KB Output is correct
62 Correct 767 ms 117708 KB Output is correct
63 Correct 774 ms 116052 KB Output is correct
64 Correct 785 ms 115620 KB Output is correct
65 Correct 399 ms 14024 KB Output is correct
66 Correct 359 ms 10240 KB Output is correct
67 Correct 288 ms 8924 KB Output is correct
68 Correct 979 ms 116672 KB Output is correct