QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#881073#10024. Low Cost SetKevin5307AC ✓2ms10132kbC++234.8kb2025-02-04 10:58:402025-02-04 10:58:40

Judging History

This is the latest submission verdict.

  • [2025-02-04 10:58:40]
  • Judged
  • Verdict: AC
  • Time: 2ms
  • Memory: 10132kb
  • [2025-02-04 10:58:40]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi; 

#define maxn 4000005
#define inf 0x3f3f3f3f

#define N 805
int n,m;
struct edge{
	int u,v,w;
}e[N][N];
int lab[N],sl[N],q[N],hd,tl,rt[N],col[N],mat[N],frm[N][N],fa[N];
int vis[N],tim;
vi p[N];
int d(edge x){return lab[x.u]+lab[x.v]-e[x.u][x.v].w*2;}
void upd(int u,int v){if(!sl[v]||d(e[u][v])<d(e[sl[v]][v]))sl[v]=u;}
void updall(int u){
	sl[u]=0;
	For(i,1,n)if(e[i][u].w>0 && rt[i]!=u && col[rt[i]]==0) upd(i,u);
}
void push(int u){
	if(u<=n) q[++tl]=u;
	else for(int x:p[u]) push(x);
}
void setrt(int u,int r){
	rt[u]=r;
	if(u>n) for(int x:p[u]) setrt(x,r);
}
int rot(int b,int x){
	int pos=find(p[b].begin(),p[b].end(),x)-p[b].begin();
	if(pos%2==0)return pos;
	reverse(p[b].begin()+1,p[b].end());
	return p[b].size()-pos;
}
void match(int u,int v){
	mat[u]=e[u][v].v;
	if(u<=n)return;
	auto w=e[u][v];
	int x=frm[u][w.u],pos=rot(u,x);
	For(i,0,pos-1) match(p[u][i],p[u][i^1]);
	match(x,v);
	rotate(p[u].begin(),p[u].begin()+pos,p[u].end());
}
void aug(int u,int v){
	assert(u&&v);
	int t=rt[mat[u]];
	match(u,v);
	if(t) match(t,rt[fa[t]]),aug(rt[fa[t]],t);
}

int lca(int u,int v){
	++tim;
	while(u||v){
		if(vis[u]==tim)return u;
		vis[u]=tim;
		u=rt[mat[u]];
		if(u)u=rt[fa[u]];
		if(!u)swap(u,v);
	}return 0;
}

int newn(){
	int x=n+1;
	while(x<=m&&rt[x])++x; if(x>m)++m;
	p[x].clear(),lab[x]=mat[x]=rt[x]=col[x]=0;
	return x;
}
void blossom(int u,int v,int r){
	int x=newn();
	mat[x]=mat[r];
	p[x].pb(r);
	for(int i=u;i!=r;i=rt[fa[i]]){
		p[x].pb(i),p[x].pb(i=rt[mat[i]]);
		push(i);
	}
	reverse(p[x].begin()+1,p[x].end());
	for(int i=v;i!=r;i=rt[fa[i]]){
		p[x].pb(i),p[x].pb(i=rt[mat[i]]);
		push(i); 
	}
	setrt(x,x);
	For(i,1,m)e[x][i].w=e[i][x].w=0,frm[x][i]=0;
	for(int t:p[x]){
		For(i,1,m){
			if(!e[x][i].w||d(e[t][i])<d(e[x][i]))
				e[x][i]=e[t][i],e[i][x]=e[i][t];
		}
		For(i,1,n)if(frm[t][i])frm[x][i]=t;
	}
	updall(x);
}
void expand(int x){
	for(int u:p[x]) setrt(u,u);
	int u=frm[x][e[x][fa[x]].u],pos=rot(x,u);
	for(int i=0;i<pos;i+=2){
		int a=p[x][i],b=p[x][i+1];
		fa[a]=e[b][a].u;
		col[a]=1,col[b]=0;
		sl[a]=0;
		updall(b),push(b);
	}
	col[u]=1;
	fa[u]=fa[x];
	for(int i=pos+1;i<p[x].size();++i)
		col[p[x][i]]=-1,updall(p[x][i]);
	rt[x]=0;
}

bool path(edge e){
	int u=rt[e.u],v=rt[e.v];
	assert(!d(e));
	if(col[v]==-1){
		fa[v]=e.u;
		col[v]=1;
		int t=rt[mat[v]];
		sl[v]=sl[t]=col[t]=0;
		push(t);
	}
	else if(!col[v]){
		int t=lca(u,v);
		if(!t)return aug(u,v),aug(v,u),1;
		else blossom(u,v,t);
	}
	return 0;
}
bool bfs()
{
	For(i,0,m) col[i]=-1,sl[i]=0;
	hd=1,tl=0;
	For(i,1,m)if(rt[i]==i&&!mat[i])fa[i]=col[i]=0,push(i);
	if(!tl)return 0;
	while(1){
		while(hd<=tl){
			int u=q[hd++];
			For(v,1,n)
				if(e[u][v].w>0&&rt[u]!=rt[v]){
					if(d(e[u][v])) upd(u,rt[v]);
					else if(path(e[u][v])) return 1; 
				}
		}
		int tmp=inf;
		For(i,1,n) if(col[rt[i]]==0) tmp=min(tmp,lab[i]);
		For(i,n+1,m) if(rt[i]==i && col[i]==1) tmp=min(tmp,lab[i]>>1);
		For(i,1,m) if(rt[i]==i && sl[i] && col[i]!=1) tmp=min(tmp,d(e[sl[i]][i])>>(col[i]+1));
		For(i,1,n){
			if(col[rt[i]]==0) lab[i]-=tmp;
			if(col[rt[i]]==1) lab[i]+=tmp;
			if(lab[i]<=0) return 0;
		}
		For(i,n+1,m){
			if(rt[i]==i){
				if(col[i]==0) lab[i]+=tmp*2;
				if(col[i]==1) lab[i]-=tmp*2;
			}
		}
		hd=1,tl=0;
		For(i,1,m)
			if(rt[i]==i && sl[i] && rt[sl[i]]!=i && !d(e[sl[i]][i]) && path(e[sl[i]][i])) return 1;
		For(i,n+1,m)
			if(rt[i]==i && col[i]==1 && !lab[i]) expand(i);
	}
}

signed main()
{
	n=read(),m=n;
	int mx=0;
	vector<int> vcost(n+n);
	For(i,1,n)For(j,1,n)e[i][j]=(edge){i,j,0},frm[i][j]=0;
	int TOTAL=0;
	vector<int> vl(n),vr(n);
	for(int i=0;i<n;i++)
	{
		vl[i]=read();
		vr[i]=read();
	}
	for(int i=1;i<n+n;i++) vcost[i]=read();
	for(int i=1;i<n+n;i++) vcost[i]+=vcost[i-1];
	for(int i=0;i<n;i++)
		TOTAL+=vcost[vr[i]-1]-vcost[vl[i]-1];
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(vl[i]<vl[j])
				if(vr[i]<vr[j])
				{
					e[i+1][j+1].w=vcost[vr[i]-1]-vcost[vl[j]-1];
					e[j+1][i+1].w=vcost[vr[i]-1]-vcost[vl[j]-1];
					mx=max(mx,vcost[vr[i]-1]-vcost[vl[j]-1]);
				}
	For(i,1,n)frm[i][i]=i;
	For(i,1,n)lab[i]=mx,rt[i]=i,p[i].clear();
	while(bfs());
	long long res=0;
	For(i,1,n)if(mat[i]&&i<mat[i])res+=e[i][mat[i]].w;
	cout<<TOTAL-res<<"\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5708kb

input:

3
1 4
2 6
3 5
1 2 3 5 8

output:

27

result:

ok answer is '27'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5716kb

input:

5
3 10
1 5
7 8
4 9
2 6
9 9 8 2 4 4 3 5 3

output:

82

result:

ok answer is '82'

Test #3:

score: 0
Accepted
time: 0ms
memory: 5712kb

input:

1
1 2
1

output:

1

result:

ok answer is '1'

Test #4:

score: 0
Accepted
time: 1ms
memory: 10008kb

input:

91
26 127
13 116
24 76
39 132
29 92
115 164
21 118
32 122
97 108
144 174
53 175
148 168
20 105
2 114
109 128
102 113
81 161
16 100
65 152
142 166
104 169
10 67
61 79
17 145
82 88
63 87
7 171
107 140
34 162
94 157
22 139
41 151
78 120
43 60
55 180
74 170
38 129
18 159
86 106
15 95
9 90
5 101
40 93
25...

output:

1758260427906

result:

ok answer is '1758260427906'

Test #5:

score: 0
Accepted
time: 1ms
memory: 8220kb

input:

97
141 193
31 161
5 188
68 153
58 105
7 87
167 172
10 100
47 92
78 142
154 159
19 138
26 157
135 180
6 72
41 173
103 160
45 95
43 48
102 112
143 168
83 165
33 178
50 90
1 132
49 79
14 17
94 126
99 120
129 164
53 182
123 136
139 144
116 119
88 93
44 61
171 183
4 80
12 137
91 108
40 181
2 118
73 122
1...

output:

1740982605951

result:

ok answer is '1740982605951'

Test #6:

score: 0
Accepted
time: 1ms
memory: 10128kb

input:

73
38 135
1 66
6 105
13 32
62 102
26 101
61 129
79 106
83 134
80 95
47 138
58 143
44 119
24 82
39 133
77 128
97 103
19 109
122 125
10 43
100 145
52 108
7 84
85 89
41 137
56 73
64 116
120 132
22 141
28 124
17 94
63 115
40 146
45 51
4 74
48 127
23 57
2 121
9 70
33 42
59 60
31 65
5 110
71 104
87 117
99...

output:

1266775167032

result:

ok answer is '1266775167032'

Test #7:

score: 0
Accepted
time: 0ms
memory: 9960kb

input:

73
82 102
33 135
120 123
129 130
70 100
49 124
42 126
67 117
36 132
22 106
40 101
26 43
21 91
32 134
14 50
4 76
1 24
41 109
54 111
45 103
28 78
13 87
59 92
74 110
6 142
27 46
47 125
17 20
68 112
29 53
131 138
38 58
23 94
30 77
10 93
2 105
39 145
113 137
80 139
8 44
52 84
72 114
35 127
85 121
19 71
8...

output:

1035696411700

result:

ok answer is '1035696411700'

Test #8:

score: 0
Accepted
time: 0ms
memory: 9904kb

input:

95
67 91
78 156
26 136
29 138
125 177
131 143
4 124
24 41
97 110
2 108
10 106
25 145
14 126
19 152
112 179
20 27
31 74
159 184
39 76
82 107
92 182
48 100
37 90
69 85
6 135
21 141
9 130
160 188
33 60
105 128
86 93
8 75
96 162
16 181
66 180
89 151
18 154
113 189
63 65
71 150
36 80
49 79
17 121
73 137
...

output:

1796851254129

result:

ok answer is '1796851254129'

Test #9:

score: 0
Accepted
time: 0ms
memory: 8192kb

input:

94
55 171
182 188
43 122
133 185
44 116
72 154
18 62
19 94
38 175
23 180
80 113
97 124
73 108
36 159
16 184
8 45
110 129
138 155
52 141
32 164
1 28
13 14
85 114
121 148
143 167
126 161
9 115
5 30
86 165
107 135
20 158
46 134
130 173
29 131
140 153
68 178
26 82
61 119
34 78
60 77
63 88
40 176
168 187...

output:

1691043263559

result:

ok answer is '1691043263559'

Test #10:

score: 0
Accepted
time: 1ms
memory: 8212kb

input:

93
76 159
133 142
31 81
78 128
3 52
179 180
134 174
93 166
82 131
106 171
24 141
13 119
38 127
28 86
19 145
71 110
91 105
67 102
42 69
103 147
165 170
23 72
61 66
32 168
7 118
18 153
11 77
35 185
113 135
124 176
10 108
22 39
9 167
157 186
6 143
101 152
151 164
162 173
12 59
46 90
112 121
27 132
57 6...

output:

1604113414154

result:

ok answer is '1604113414154'

Test #11:

score: 0
Accepted
time: 1ms
memory: 8220kb

input:

100
129 179
12 66
33 189
55 68
172 200
82 117
100 134
113 169
10 168
104 121
77 80
47 109
40 158
8 110
98 122
114 124
34 186
4 78
95 180
141 164
143 153
25 166
75 123
84 108
146 171
83 150
53 149
74 191
130 199
127 159
56 194
32 193
50 178
58 145
42 51
35 156
3 154
99 182
36 38
44 61
23 133
7 187
28...

output:

2000409667214

result:

ok answer is '2000409667214'

Test #12:

score: 0
Accepted
time: 1ms
memory: 8236kb

input:

98
35 115
106 157
92 191
73 123
7 10
47 48
3 77
131 176
37 98
108 148
6 46
51 192
114 160
136 147
23 75
63 177
36 89
38 64
155 180
57 150
60 193
52 151
125 126
72 167
130 179
94 168
78 85
104 111
127 133
97 149
16 28
74 143
102 174
164 183
61 144
20 146
17 117
30 83
134 195
1 132
9 118
11 172
21 152...

output:

1938725160537

result:

ok answer is '1938725160537'

Test #13:

score: 0
Accepted
time: 0ms
memory: 8196kb

input:

100
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 ...

output:

45777426775

result:

ok answer is '45777426775'

Test #14:

score: 0
Accepted
time: 2ms
memory: 10132kb

input:

100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
10 110
11 111
12 112
13 113
14 114
15 115
16 116
17 117
18 118
19 119
20 120
21 121
22 122
23 123
24 124
25 125
26 126
27 127
28 128
29 129
30 130
31 131
32 132
33 133
34 134
35 135
36 136
37 137
38 138
39 139
40 140
41 141
42 142
43 143
44 1...

output:

2306815679934

result:

ok answer is '2306815679934'

Test #15:

score: 0
Accepted
time: 0ms
memory: 5712kb

input:

3
1 4
2 6
3 5
1 2 3 5 1000000000

output:

1000000019

result:

ok answer is '1000000019'