QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#457004#8577. 평균 최대화oolimry5 796ms923352kbC++145.2kb2024-06-28 20:27:552024-06-28 20:27:56

Judging History

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

  • [2024-06-28 20:27:56]
  • 评测
  • 测评结果:5
  • 用时:796ms
  • 内存:923352kb
  • [2024-06-28 20:27:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define showlist(x) cerr << #x << " is "; for(auto p : x) cerr << p << " "; cerr << endl;
typedef long long lint;
typedef pair<lint,lint> ii;
#define num first
#define denom second

const lint inf = 1e15;
int n;
lint arr[300005];
lint psum[300005];

map<ii, int> nodeRanges;
int nodeNumber = 0;
vector<int> child[600006];

int L[600006];
int R[600006];

struct frac{
	lint num = 0, denom = 0;
	bool operator < (const frac &f) const {
		if(f.num == 0) return false;
		if(num == 0) return true; 
		return num*f.denom < f.num*denom;
	};
};
frac lastPoint[600006];
multiset<frac> candidates[600006];
map<frac, int> discretizeFracs;

frac precompAns[600006];

const int N = 300003;
ii rangemin[600015];
void pointminupdate(int i, lint x){
	rangemin[i+N] = ii(x,i); 
	for(i = (i+N)/2;i;i >>= 1) rangemin[i] = min(rangemin[i<<1], rangemin[i<<1|1]);
}
ii rangeminquery(int l, int r){
	ii res = {inf,inf};
	for(l += N,r += N+1;l < r;l >>= 1, r >>= 1){
		if(l&1) res = min(res, rangemin[l++]);
		if(r&1) res = min(res, rangemin[--r]);
	}
	return res;
}


lint rangeSum(int l, int r){
	return psum[r] - psum[l-1];
}

void recurse(int l, int r, int parent){
	
	L[nodeNumber] = l;
	R[nodeNumber] = r;
	int thisNodeNumber = nodeNumber;
	nodeRanges[ii(l,r)] = thisNodeNumber; 
	if(parent != -1) child[parent].push_back(thisNodeNumber);
	nodeNumber++;
	
	
	lint minVal = rangeminquery(l+1,r-1).first;
	if(minVal >= inf/2) return;
		
	vector<int> pos = {l};
	
	while(true){
		ii minValPos = rangeminquery(l+1,r-1);
		if(minValPos.first != minVal) break;
		
		int p = minValPos.second;
		pointminupdate(p, inf);
		pos.push_back(p);
	}
	
	pos.push_back(r);
	
	//showlist(pos);
	
	for(int i = 1;i < sz(pos);i++){
		recurse(pos[i-1], pos[i], thisNodeNumber);
	}
	
}

///step 1, discretize all segments
///step 2, do the thing
struct node{
	int s, e, m;
	node *l = nullptr, *r = nullptr;
	frac val = {0,0};
	
	node(int S, int E){
		s = S, e = E, m = (s+e)/2;
	}
	
	void create(){
		if(s == e) return;
		if(l != nullptr) return;
		l = new node(s,m);
		r = new node(m+1,e);
	}
	
	void update(int X, lint NUM, lint DENOM){
		create();
		val.num += NUM, val.denom += DENOM;
		if(s != e){
			if(X <= m) l->update(X, NUM, DENOM);
			else r->update(X, NUM, DENOM);
		}
	}
	
	frac findBest(frac base){
		create();	
		if(s == e) return base;
		
		frac midpoint = {base.num - l->val.num, base.denom - l->val.denom};
		
		//show2(base.num, base.denom);
		//show2(l->val.num, l->val.denom);
		
		
		if(base < midpoint) return r->findBest(midpoint);
		else return l->findBest(base);
	}
	
} *root[10006];

int run = 1;
void addSlope(int u, frac f){
	candidates[u].insert(f);
	if(run == 1) discretizeFracs[f] = -1;
	if(run == 2){
		//assert(discretizeFracs[f] != 0);
		root[u]->update(discretizeFracs[f], f.num, f.denom);
	}
}
void removeSlope(int u, frac f){
	candidates[u].erase(candidates[u].find(f));
	if(run == 2){
		//assert(discretizeFracs[f] != 0);
		root[u]->update(discretizeFracs[f], -f.num, -f.denom);
	}
}

void dfs(int u){
	int r = R[u], l = L[u];
	
	if(sz(child[u]) == 0){
		precompAns[u] = {arr[l] + arr[r], 2};
		lastPoint[u] = {0,0};
		return;
	}

	for(int v : child[u]){
		dfs(v);
		if(sz(candidates[v]) > sz(candidates[u])){
			swap(candidates[u], candidates[v]);
			swap(root[u], root[v]);
		}
		
		for(frac f : candidates[v]) addSlope(u, f);

		lastPoint[u].num += lastPoint[v].num;
		lastPoint[u].denom += lastPoint[v].denom;
	}
	
	frac slope = {rangeSum(l+1,r-1) - lastPoint[u].num, r-l-1 - lastPoint[u].denom};
	
	while(sz(candidates[u]) > 0){
		auto it = candidates[u].end(); it--;
		
		if(slope < *it){
			slope.num += it->num;
			slope.denom += it->denom;
			removeSlope(u, *it);
		}
		else break;
	}
	
	addSlope(u, slope);
	lastPoint[u] = {rangeSum(l+1,r-1), r-l-1};
	
	if(run == 2){
		lint total = rangeSum(l,r);
		lint k = r-l+1;
		precompAns[u] = root[u]->findBest({total,k});
	}
	
	/*
	show2(l,r);
	for(frac f : candidates[u]) show2(f.num, f.denom);
	cerr << endl;
	//*/
}

void initialize(std::vector<int> A){

	n = (int)(A.size());
	for(int i = 1;i <= n;i++) arr[i] = A[i-1];
	for(int i = 1;i <= n+1;i++) psum[i] = arr[i] + psum[i-1];	
	for(int i = 0;i <= n+1;i++) pointminupdate(i, arr[i]);

	recurse(0,n+1,-1);
	dfs(0);
	
	int discCounter = 1;
	for(auto &it : discretizeFracs){
		//show2(it.first.num, it.first.denom);
		it.second = discCounter++;
	}
	
	show("RUN 2");
	for(int i = 0;i <= nodeNumber;i++){
		candidates[i].clear();
		lastPoint[i] = {0,0};
		root[i] = new node(0,sz(discretizeFracs));
	}
	run = 2;
	
	dfs(0);
}

std::array<long long, 2> maximum_average(int l, int r){
    l++, r++;
    
    frac res = precompAns[nodeRanges[ii(l,r)]];
    
    return {res.num, res.denom};
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 7ms
memory: 63552kb

input:

10
2 4 3 9 9 9 9 9 9 1
2
0 2
0 9

output:

9 3
60 9

result:

ok correct!

Test #2:

score: 0
Accepted
time: 10ms
memory: 63552kb

input:

15
4596730 8340349 4612555 5692442 3914918 5213545 5248236 1276073 3844119 2943960 9231647 5091649 2239006 9139001 4735414
100
7 8
5 6
2 4
0 4
8 9
10 11
3 4
0 1
10 11
10 11
3 4
4 5
12 13
0 2
2 4
11 12
12 14
2 3
7 8
12 14
6 7
4 5
11 12
10 11
7 12
8 9
8 9
0 2
2 3
12 14
7 9
7 9
12 13
10 11
9 11
13 14
8...

output:

5120192 2
10461781 2
14219915 3
27156994 5
6788079 2
14323296 2
9607360 2
12937079 2
14323296 2
14323296 2
9607360 2
9128463 2
11378007 2
17549634 3
14219915 3
7330655 2
16113421 3
10304997 2
5120192 2
16113421 3
6524309 2
9128463 2
7330655 2
14323296 2
20782335 5
6788079 2
6788079 2
17549634 3
1030...

result:

ok correct!

Test #3:

score: 0
Accepted
time: 113ms
memory: 63248kb

input:

15
962724 8815662 7612372 5708998 125165 5107756 9366378 9514244 2381600 4299006 9423670 8225791 7458292 2315903 7210561
600000
7 8
0 4
6 8
9 10
11 12
4 5
13 14
8 13
9 11
2 3
7 8
9 12
6 8
0 4
0 2
12 13
1 2
8 13
0 3
9 10
4 5
6 7
6 8
6 7
0 2
4 13
3 4
6 7
8 9
6 7
11 12
5 8
0 3
9 13
4 8
4 8
8 9
8 13
4 1...

output:

11895844 2
23224921 5
21262222 3
13722676 2
15684083 2
5232921 2
9526464 2
34104262 6
21948467 3
13321370 2
11895844 2
29406759 4
21262222 3
23224921 5
17390758 3
9774195 2
16428034 2
34104262 6
23099756 4
13722676 2
5232921 2
18880622 2
21262222 3
18880622 2
17390758 3
58217805 10
5834163 2
1888062...

result:

ok correct!

Test #4:

score: 0
Accepted
time: 3ms
memory: 63280kb

input:

15
1 8446287 2 999999 3000000 5533975 3000000 3816891 3000000 7671276 3000000 999999 5836790 8574548 1
23
0 14
1 2
0 1
2 14
0 2
3 11
2 3
4 6
3 4
5 6
4 5
6 8
7 8
6 7
8 10
9 10
8 9
10 11
11 14
12 14
11 12
13 14
12 13

output:

53879769 15
8446289 2
8446288 2
45433481 13
8446290 3
31022140 9
1000001 2
11533975 3
3999999 2
8533975 2
8533975 2
9816891 3
6816891 2
6816891 2
13671276 3
10671276 2
10671276 2
3999999 2
15411338 4
14411339 3
6836789 2
8574549 2
14411338 2

result:

ok correct!

Test #5:

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

input:

15
1 15 16 14 18 13 20 12 22 11 24 10 26 9 1
27
0 14
1 3
0 1
2 3
1 2
3 5
0 3
4 5
3 4
5 7
0 5
6 7
5 6
7 9
0 7
8 9
7 8
9 11
0 9
10 11
9 10
11 13
0 11
12 13
11 12
13 14
0 13

output:

212 15
45 3
16 2
30 2
31 2
45 3
46 4
31 2
32 2
45 3
77 6
32 2
33 2
45 3
109 8
33 2
34 2
45 3
142 10
34 2
35 2
45 3
176 12
35 2
36 2
10 2
211 14

result:

ok correct!

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #6:

score: 6
Accepted
time: 6ms
memory: 63296kb

input:

48
225555 4046145 5839635 7194994 4703765 1253415 2526352 3198926 6313532 2368195 5024833 9436074 1792945 7650559 3393464 2402026 7697170 5205463 9830460 5392966 1687150 9984223 3014343 8856776 1412298 9773499 6469768 5802450 758943 2748325 7110370 4498454 2674137 8596714 8823659 9855644 6654297 367...

output:

24413198 4
16509941 2
17738394 3
45052203 9
17031286 4
24413198 4
11608824 2
45052203 9
17738394 3
13283417 3
9885780 2
23458015 4
14460907 2
5795490 2
25333600 3
11880653 3
7923098 2
23037954 5
131217114 26
22841241 3
10545933 2
17738394 3
13283417 3
18983962 3
17655565 3
11270851 2
8261027 2
13121...

result:

ok correct!

Test #7:

score: 0
Accepted
time: 120ms
memory: 63292kb

input:

50
7121308 7345583 6899063 282017 6341784 3680369 5436234 9663519 633330 6333746 7783999 6482701 567072 4276742 8011254 1944632 5712778 8002712 306241 4160326 5728910 1328677 6357927 2565549 4232827 255999 3544802 2039097 494486 2383883 9963617 175242 2913048 5502915 9123911 4881811 2516781 8926134 ...

output:

5394962 2
12522742 3
14466891 2
12347500 2
4567430 2
14631630 3
6947205 2
14631630 3
15966363 4
75715129 17
15966363 4
12522742 3
6967076 2
3800801 2
6334384 4
2533583 2
69914912 15
4488826 2
75459130 16
14626826 2
2533583 2
15733083 3
15099753 2
10138859 2
75459130 16
8415963 2
4567430 2
10252153 3...

result:

ok correct!

Test #8:

score: -6
Wrong Answer
time: 0ms
memory: 61500kb

input:

50
1 3859136 7745573 6119170 3863010 2 3 4 3498508 5 6608915 6662164 999999 3000000 7880751 3000000 4473437 3000000 7609368 3000000 4750778 3000000 8554401 3000000 5166495 3000000 4156171 3000000 9941061 3000000 7323881 3000000 3334344 3000000 3959127 3000000 999999 6 5 5322820 9189056 4 8127987 797...

output:

181704758 47
21586891 5
3859137 2
13864743 2
11604709 2
9982180 2
17723879 3
3863012 2
21586889 4
160117867 41
21586892 6
141746793 36
5 2
121483149 31
7 2
3498513 2
3498512 2
106971265 27
3498517 3
14271078 3
6608920 2
7662163 2
13271079 2
93700170 22
14271083 4
13880751 3
3999999 2
10880751 2
1088...

result:

wrong answer Wrong Answer on query #1: 181704758/47 != 185663885/48

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #15:

score: 0
Wrong Answer
time: 302ms
memory: 240756kb

input:

300000
1 2 4 4 4 4 3 2 4 4 3 4 4 4 4 4 4 4 4 4 3 4 3 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 3 4 4 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 4 3 4 4 4 4 4 4 4 3 2 4 4 4 4 4 4 4 4 4 4 4 4 4 3 4 4 4 4 4 4 4 4 4 4 4 2 4 4 2 4 4 3 4 4 4 2 3 4 4 4 4 4 4 3 2 4 4 4 2 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 3 4 4 3 4 4 4 4 4 4 4 4 4...

output:

938442 249902
23 7
3 2
8 2
6 2
8 2
8 2
7 2
5 2
21 6
224 60
8 2
6 2
7 2
42 11
13 4
8 2
7 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
7 2
10 3
7 2
7 2
38 10
8 2
7 2
8 2
8 2
8 2
8 2
8 2
8 2
7 2
6 2
18 5
8 2
7 2
8 2
7 2
10 3
7 2
7 2
46 12
8 2
7 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
7 2
6 2
18 5
8 2
7 2
8 2
7 2
14 4
8 2
7...

result:

wrong answer Wrong Answer on query #1: 938442/249902 != 1041675/278497

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 796ms
memory: 923352kb

input:

300000
1 300000 300001 299999 300003 299998 300005 299997 300007 299996 300009 299995 300011 299994 300013 299993 300015 299992 300017 299991 300019 299990 300021 299989 300023 299988 300025 299987 300027 299986 300029 299985 300031 299984 300033 299983 300035 299982 300037 299981 300039 299980 3000...

output:

891042547 2380

result:

wrong answer Wrong Answer on query #1: 891042547/2380 != 917250302/2450

Subtask #7:

score: 0
Skipped

Dependency #4:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%