QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#664635#6531. Base Station ConstructionAyayaAC ✓116ms31316kbC++142.3kb2024-10-21 21:32:222024-10-21 21:32:22

Judging History

你现在查看的是最新测评结果

  • [2024-10-21 21:32:22]
  • 评测
  • 测评结果:AC
  • 用时:116ms
  • 内存:31316kb
  • [2024-10-21 21:32:22]
  • 提交

answer

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<unordered_map>
#include<vector>
#include<bitset>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<random>
#include<numeric>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define lc (x<<1)
#define rc (x<<1|1)
#define pii pair<int,int>
#define mkp make_pair
#define fi first
#define se second
#define cout (cerr<<">> ",cout)
const int Mx=500005,p=998244353;
int read(){
	char ch=getchar();
	int Alice=0,Aya=1;
	while(ch<'0'||ch>'9'){
		if(ch=='-') Aya=-Aya;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
		Alice=(Alice<<3)+(Alice<<1)+(ch^48),ch=getchar();
	return (Aya==1?Alice:-Alice);
}
int n,m;
int cost[Mx];
struct Seg{
	int l,r;
	bool operator <(const Seg&o)const{
		return l==o.l?r<o.r:l<o.l;
	}
}a[Mx],c[Mx];
int f[Mx];
int t[Mx];
void Ins(int i,int k){
	for(;i<=m+1;i+=(i&-i)) t[i]=min(t[i],k);
}
int Qry(int i){
	int res=1e18;
	for(;i;i&=(i-1)) res=min(res,t[i]);
	return res;
}
void Solve(){
	n=read();
	for(int i=1;i<=n;i++) cost[i]=read();
	m=read();
	for(int i=0;i<=m+1;i++) t[i]=1e18;
	for(int i=1;i<=m;i++){
		a[i].l=read(),a[i].r=read();
	}
	sort(a+1,a+m+1);
	int M=1;
	for(int i=2;i<=m;i++){
		if(a[i].l==a[M].l) continue;
		while(M&&a[i].r<=a[M].r) M--;
		a[++M]=a[i];
	}
	m=M;
	for(int i=1;i<=m;i++){
		if(a[i].l<=a[i-1].l||a[i].r<=a[i-1].r){
			exit(114);
		}
	}
	int ans=1e18;
	Ins(m+1,0);
	for(int i=1;i<=n;i++){
		int L=1,R=m,ok=-1;
		while(L<=R){
			int mid=((L+R)>>1);
			if(a[mid].r>=i) R=mid-1,ok=mid;
			else L=mid+1;
		}
		c[i].l=ok;
		L=1,R=m,ok=-1;
		while(L<=R){
			int mid=((L+R)>>1);
			if(a[mid].l<=i) L=mid+1,ok=mid;
			else R=mid-1;
		}
		c[i].r=ok;
		//cout<<c[i].l<<" "<<c[i].r<<endl;
		if(c[i].l==-1||c[i].r==-1){
			f[i]=1e18;
			continue;
		}
		f[i]=Qry(m+2-c[i].l)+cost[i];
		Ins(m+2-(c[i].r+1),f[i]);
		if(c[i].r==m) ans=min(ans,f[i]);
	}
	printf("%lld\n",ans);
}
signed main(){
	int T=read();
	while(T--) Solve();
	return 0;
}
/*
Hello!!
Sample:
1
10
1 2 2 3 1 3 3 1 1 2
8
4 8
7 10
3 5
2 10
3 6
6 7
5 5
6 8
-------------------
*/


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11984kb

input:

2
5
3 2 4 1 100
3
1 3
2 4
5 5
5
7 3 4 2 2
3
1 4
2 3
4 5

output:

102
5

result:

ok 2 number(s): "102 5"

Test #2:

score: 0
Accepted
time: 34ms
memory: 16540kb

input:

6201
12
88 78 46 95 84 98 55 3 68 42 6 18
19
6 9
10 11
12 12
8 11
8 12
2 3
2 3
1 5
9 9
7 8
6 11
2 4
12 12
2 4
2 9
7 10
8 8
1 7
6 11
5
76 27 48 66 61
2
1 4
3 5
8
64 81 20 6 86 9 4 55
1
7 7
9
1 43 18 81 11 22 9 61 83
14
5 6
2 6
5 8
1 4
9 9
9 9
7 7
2 5
8 9
5 6
4 8
5 8
9 9
6 6
10
66 41 84 7 80 31 22 96 ...

output:

141
48
4
126
303
141
23
170
159
139
153
194
136
89
230
93
287
89
124
130
148
27
1
193
194
93
239
303
236
150
177
57
46
18
24
66
83
160
108
62
35
122
156
81
115
138
54
242
126
28
171
86
311
244
262
87
302
81
86
173
30
129
75
91
90
179
81
224
142
154
77
111
194
247
211
53
66
17
213
101
308
137
177
24
...

result:

ok 6201 numbers

Test #3:

score: 0
Accepted
time: 89ms
memory: 31316kb

input:

1
500000
1009859 4748096 475634 4928248 1927808 4875072 3158867 2937890 2595515 1026685 468307 4240390 4887597 3586447 3764525 1365644 156469 188306 350786 1308832 4695957 562147 3427221 937909 2590963 1478310 357775 361535 993561 1967811 1718075 1555000 4533750 3412453 1936715 4173340 1350235 10673...

output:

188505494

result:

ok 1 number(s): "188505494"

Test #4:

score: 0
Accepted
time: 99ms
memory: 31192kb

input:

1
500000
524125987 923264237 374288891 535590429 751244358 124321145 232930851 266089174 543529670 773363571 319728747 580543238 582720391 468188689 490702144 598813561 138628383 284660056 733781508 155605777 931759705 245485733 723534730 257812292 794937524 596788519 188451996 981010588 14483682 59...

output:

39443815978

result:

ok 1 number(s): "39443815978"

Test #5:

score: 0
Accepted
time: 114ms
memory: 31248kb

input:

1
500000
3130340 4038109 1010367 3292779 4255104 2541121 1216121 550625 1649111 1988327 4432115 1543614 1408744 954710 379428 4483347 2623043 103560 3009625 721764 4731279 2841516 1631728 655210 4309060 3805541 2477086 1497937 1904242 4848840 413802 3718778 4393465 317154 1387439 2402170 4147983 439...

output:

164768989764

result:

ok 1 number(s): "164768989764"

Test #6:

score: 0
Accepted
time: 116ms
memory: 31260kb

input:

1
500000
514295292 338500638 997461918 380687304 81795547 983506527 313307836 792757707 536042370 68004076 379698431 72065046 337640225 656505532 750559695 591885418 871006967 997652216 377918439 370086908 141012444 581275008 33544152 187616244 486523832 609395558 34776709 996997103 517207561 833539...

output:

44054772879073

result:

ok 1 number(s): "44054772879073"

Test #7:

score: 0
Accepted
time: 46ms
memory: 29160kb

input:

1
499999
82019357 40331689 57426776 98479692 92109131 43667828 59480396 53287163 59259926 72632620 73858687 59258742 10722051 36949296 71261434 29492475 40567178 52512884 79985243 36668501 93969225 90888291 14848144 83839998 27170750 91058370 65593415 98660387 31915026 26008900 13128265 69914578 497...

output:

20000677

result:

ok 1 number(s): "20000677"