QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#141180#6531. Base Station Constructioncy1999TL 1ms30364kbC++111.4kb2023-08-17 09:15:302023-08-17 09:15:32

Judging History

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

  • [2023-08-17 09:15:32]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:30364kb
  • [2023-08-17 09:15:30]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int t,n,m;
int f[N][30];
int a[N];
map<pair<int,int> ,int> q[N];
struct node
{
	int l,r;
}w[N];
void yuchuli()
{
	for(int i=1;i<=n;i++)
	{
		f[i][0]=a[i];
	}
	int p=log(n)/log(2);
	for(int j=1;j<=p;j++)
	{
		for(int i=1;i<=n-(1<<j)+1;i++)
		{
			f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
		}
	}
}
int ask(int l,int r)
{
	int p=log(r-l+1)/log(2);
	return min(f[l][p],f[r-(1<<p)+1][p]);
}
int find1(int l,int r,int p)
{
	if(q[p][make_pair(l,r)])
	{
		return q[p][make_pair(l,r)];
	}
	if(p==m+1)
	{
		return q[p][make_pair(l,r)]=ask(l,r);
	}
	if(r<w[p].l)
	{
		return q[p][make_pair(l,r)]=ask(l,r)+find1(w[p].l,w[p].r,p+1);
	}
	if(w[p].l>=l&&w[p].r<=r)
	{
		return q[p][make_pair(l,r)]=find1(w[p].l,w[p].r,p+1);
	}
	int ans1=ask(l,r)+find1(w[p].l,w[p].r,p+1);
	//int ans2=ask(w[p].l,w[p].r)+find1(l,r,p+1);
	int ans3=find1(w[p].l,r,p+1);
	int ans=min(ans1,ans3);
	return q[p][make_pair(l,r)]=ans;
}
signed main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			q[i].clear();
		}
		yuchuli();
		cin>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>w[i].l>>w[i].r;
		}
		a[n+1]=0;
		w[m+1].l=w[m+1].r=n+1;
		int ans=find1(0,0,1);
		cout<<ans<<endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 30364kb

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: -100
Time Limit Exceeded

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:

61
48
4
19
167
68
23
2
43
113
74
86
68
48
185
32
59
89
98
84
35
23
1
6
123
93
236
145
56
100
100
179
46
18
17
24
10
112
20
36
35
103
44
73
90
33
16
126
122
29
88
79
302
182
109
55
389
34
65
16
30
33
24
67
72
129
228
120
106
14
75
57
71
38
211
53
5
17
171
82
180
97
67
24
183
53
158
8
30
35
123
64
116...

result: