답안 #74040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
74040 2018-08-29T16:30:39 Z aleksam 말 (IOI15_horses) C++14
100 / 100
884 ms 156912 KB
#include "horses.h"
#include <bits/stdc++.h>
#define MAX_N 500000
#define MOD 1000000007
#define INF 1000000000
#define debug 0
using namespace std;

int segtree[4*MAX_N], N;
long long X[MAX_N], Y[MAX_N];
long long partX[4*MAX_N];

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs>rhs;}
};

set<int, classcomp> S;//Skup indexa na kojima X[i]>1
int partXX(int a);
void printtree(){
	if(debug){
		for(int i=0; i<4*N; ++i){
			printf("%*d ", 3, i);
		}
		printf("\n");
		for(int i=0; i<4*N; ++i){	
			printf("%*d ", 3, segtree[i]);
		}
		printf("\n");
		for(int i=0; i<4*N; ++i){
			printf("%*d ", 3, partX[i]);
		}
		printf("\n");
	}
}

void maketree(int l, int r, int v){
	if(l+1>=r){
		if(l==r-1){
			segtree[v]=l;
			partX[v]=X[l];
			return;
		}
		/*segtree[v]=-1;
		partX[v]=1;*/
		assert(0);
		return;
	}
	int mid=(l+r)/2;
	maketree(l, mid, 2*v+1);
	maketree(mid, r, 2*v+2);
	partX[v]=partX[2*v+1]*partX[2*v+2];
	partX[v]%=MOD;
	if(segtree[2*v+1]==-1){
		segtree[v]=segtree[2*v+2];
		return;
	}
	if(segtree[2*v+2]==-1){
		segtree[v]=segtree[2*v+1];
		return;
	}
	segtree[v]=Y[segtree[2*v+1]]>Y[segtree[2*v+2]]?segtree[2*v+1]:segtree[2*v+2];
	return;
}

int query(int ql, int qr, int l, int r, int v){
	//if(debug)printf("rmq(%d, %d, %d, %d)\n", ql, qr, l, r);
	if(ql>=r || qr<=l){
		return -1;
	}
	if(ql<=l  && r<=qr){
		return segtree[v];
	}
	int q1, q2;
	q1=query(ql, qr, l, (l+r)/2, 2*v+1);
	q2=query(ql, qr, (l+r)/2, r, 2*v+2);
	if(q1==-1)return q2;
	if(q2==-1)return q1;
	return Y[q1]>Y[q2]?q1:q2;
}

int partquery(int ql, int qr, int l, int r, int v){
	if(ql>=r || qr<=l){
		return 1;
	}
	if(ql<=l  && r<=qr){
		return partX[v];
	}
	long long q1, q2;
	q1=partquery(ql, qr, l, (l+r)/2, 2*v+1);
	q2=partquery(ql, qr, (l+r)/2, r, 2*v+2);
	return (q1*q2)%MOD;
}

void updatetree(int l, int r, int v, int x){
	if(l+1>=r){
		return;
	}
	if(l<=x && x<r){
		updatetree(l, (l+r)/2, 2*v+1, x);
		updatetree((l+r)/2, r, 2*v+2, x);
		if(segtree[2*v+1]==-1){
			segtree[v]=segtree[2*v+2];
			return;
		}
		if(segtree[2*v+2]==-1){
			segtree[v]=segtree[2*v+1];
			return;
		}
		segtree[v]=Y[segtree[2*v+1]]>Y[segtree[2*v+2]]?segtree[2*v+1]:segtree[2*v+2];
	}
}

void updatepart(int l, int r, int v, int x){
	//printf("updatepart(%d, %d, %d);\n", l, r, x);
	if(l+1>=r){
		if(l==x)partX[v]=X[x];
		return;
	}
	if(l<=x && x<r){
		updatepart(l, (l+r)/2, 2*v+1, x);
		updatepart((l+r)/2, r, 2*v+2, x);
		partX[v]=(partX[2*v+1]*partX[2*v+2])%MOD;
	}
}

int rmq(int ql, int qr){//Vraca index!!!
	//printf("rmq(%d, %d)\n", ql, qr);
	/*int max=0, indmax=-1;
	for(int i=ql; i<qr; ++i){
		if(Y[i]>max){
			max=Y[i];
			indmax=i;
		}
	}
	return indmax;*/
	return query(ql, qr, 0, N, 0);
}

int partXX(int pos){
	/*long long ret=1;
	for(int i=0; i<pos+1; ++i){
		ret*=X[i];
		if(ret>MOD)ret%=MOD;
	}
	return ret;*/
	return partquery(0, pos+1, 0, N, 0);
}

int buff[50];//Indexi svih X-eva koji su veci od 1
int num_big=0;

int solve(){
	//Moze brze
	long long pi=1;
	int ind=0;
	for(auto i:S){
		pi*=X[i];
		buff[ind++]=i;
		if(pi>INF)break;
	}
	if(pi<INF)buff[ind++]=0;
	reverse(buff, buff+ind);
	/*if(pi<INF){
		big.clear();
		big.push_back(0);
		for(auto i:S){
			big.push_back(i);
		}
	}*/
	pi=1;
	int *big=buff;
	int d=ind;
	/*if(debug){
		for(int i=0; i<d; ++i)printf("%d ", buff[i]);
		printf("\n");
	}*/
	long long totmax=0, indmax=-1;
	for(int i=0; i<d; ++i){
		long long maxy;
		if(i!=d-1){
			maxy=rmq(big[i], big[i+1]);
		}
		else {
			maxy=rmq(big[i], N);
			//printf("ovde\n");
		}
		//if(debug){printf("Iteracija i=%d, big[i]=%d, maxy=%d, pi=%d\n", i, big[i], maxy, pi);}
		if(i!=0)pi*=X[big[i]];
		if((long long)Y[maxy]*pi>totmax){
			totmax=(long long)Y[maxy]*pi;
			indmax=maxy;
		}
		
	}
	if(debug){
		//printf("totmax=%d, indmax=%d\n", totmax, indmax);
		//printf("partXX(indmax)=%d\n", partXX(indmax));
	}
	long long res=(long long)Y[indmax]*partXX(indmax);
	return res%MOD;
}

int init(int n, int x[], int y[]) {
	N=n;
	for(int i=0; i<N; ++i){
		X[i]=x[i];
		Y[i]=y[i];
		if(X[i]>1){
			S.insert(i);
		}
	}
	maketree(0, N, 0);
	//printtree();
	return solve();
}

int updateX(int pos, int val) {
	if(X[pos]==1 && val>1){
		S.insert(pos);
		X[pos]=val;
		updatepart(0, N, 0, pos);
	}
	else if(X[pos]>1 && val==1){
		S.erase(pos);
		X[pos]=1;
		updatepart(0, N, 0, pos);
	}
	else if(X[pos]>1 && val>1){
		X[pos]=val;
		updatepart(0, N, 0, pos);
	}
	//printtree();
	return solve();
}

int updateY(int pos, int val) {
	Y[pos]=val;
	updatetree(0, N, 0, pos);
	//printtree();
	return solve();
}

Compilation message

horses.cpp: In function 'void printtree()':
horses.cpp:31:30: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long long int' [-Wformat=]
    printf("%*d ", 3, partX[i]);
                      ~~~~~~~~^
horses.cpp: In function 'int partquery(int, int, int, int, int)':
horses.cpp:87:17: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
   return partX[v];
          ~~~~~~~^
horses.cpp:92:16: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
  return (q1*q2)%MOD;
                ^
horses.cpp: In function 'int solve()':
horses.cpp:200:50: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
  long long res=(long long)Y[indmax]*partXX(indmax);
                                                  ^
horses.cpp:201:12: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
  return res%MOD;
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 3 ms 680 KB Output is correct
10 Correct 2 ms 680 KB Output is correct
11 Correct 3 ms 680 KB Output is correct
12 Correct 3 ms 680 KB Output is correct
13 Correct 2 ms 680 KB Output is correct
14 Correct 2 ms 680 KB Output is correct
15 Correct 2 ms 680 KB Output is correct
16 Correct 3 ms 680 KB Output is correct
17 Correct 5 ms 680 KB Output is correct
18 Correct 3 ms 680 KB Output is correct
19 Correct 2 ms 680 KB Output is correct
20 Correct 3 ms 680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 680 KB Output is correct
2 Correct 2 ms 680 KB Output is correct
3 Correct 2 ms 680 KB Output is correct
4 Correct 2 ms 728 KB Output is correct
5 Correct 2 ms 728 KB Output is correct
6 Correct 2 ms 728 KB Output is correct
7 Correct 3 ms 728 KB Output is correct
8 Correct 2 ms 728 KB Output is correct
9 Correct 2 ms 728 KB Output is correct
10 Correct 2 ms 728 KB Output is correct
11 Correct 2 ms 728 KB Output is correct
12 Correct 2 ms 728 KB Output is correct
13 Correct 3 ms 728 KB Output is correct
14 Correct 2 ms 728 KB Output is correct
15 Correct 3 ms 728 KB Output is correct
16 Correct 2 ms 728 KB Output is correct
17 Correct 2 ms 728 KB Output is correct
18 Correct 2 ms 732 KB Output is correct
19 Correct 3 ms 732 KB Output is correct
20 Correct 3 ms 732 KB Output is correct
21 Correct 4 ms 732 KB Output is correct
22 Correct 2 ms 732 KB Output is correct
23 Correct 4 ms 732 KB Output is correct
24 Correct 4 ms 732 KB Output is correct
25 Correct 6 ms 732 KB Output is correct
26 Correct 4 ms 780 KB Output is correct
27 Correct 6 ms 780 KB Output is correct
28 Correct 5 ms 780 KB Output is correct
29 Correct 3 ms 780 KB Output is correct
30 Correct 4 ms 780 KB Output is correct
31 Correct 5 ms 780 KB Output is correct
32 Correct 5 ms 780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 884 ms 49260 KB Output is correct
2 Correct 443 ms 49432 KB Output is correct
3 Correct 434 ms 49432 KB Output is correct
4 Correct 412 ms 49432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 49432 KB Output is correct
2 Correct 2 ms 49432 KB Output is correct
3 Correct 2 ms 49432 KB Output is correct
4 Correct 3 ms 49432 KB Output is correct
5 Correct 2 ms 49432 KB Output is correct
6 Correct 2 ms 49432 KB Output is correct
7 Correct 3 ms 49432 KB Output is correct
8 Correct 2 ms 49432 KB Output is correct
9 Correct 3 ms 49432 KB Output is correct
10 Correct 3 ms 49432 KB Output is correct
11 Correct 3 ms 49432 KB Output is correct
12 Correct 3 ms 49432 KB Output is correct
13 Correct 3 ms 49432 KB Output is correct
14 Correct 2 ms 49432 KB Output is correct
15 Correct 2 ms 49432 KB Output is correct
16 Correct 2 ms 49432 KB Output is correct
17 Correct 2 ms 49432 KB Output is correct
18 Correct 2 ms 49432 KB Output is correct
19 Correct 3 ms 49432 KB Output is correct
20 Correct 3 ms 49432 KB Output is correct
21 Correct 2 ms 49432 KB Output is correct
22 Correct 3 ms 49432 KB Output is correct
23 Correct 3 ms 49432 KB Output is correct
24 Correct 4 ms 49432 KB Output is correct
25 Correct 4 ms 49432 KB Output is correct
26 Correct 4 ms 49432 KB Output is correct
27 Correct 6 ms 49432 KB Output is correct
28 Correct 3 ms 49432 KB Output is correct
29 Correct 3 ms 49432 KB Output is correct
30 Correct 4 ms 49432 KB Output is correct
31 Correct 6 ms 49432 KB Output is correct
32 Correct 7 ms 49432 KB Output is correct
33 Correct 58 ms 49432 KB Output is correct
34 Correct 60 ms 49432 KB Output is correct
35 Correct 260 ms 49432 KB Output is correct
36 Correct 260 ms 49432 KB Output is correct
37 Correct 120 ms 49432 KB Output is correct
38 Correct 158 ms 49432 KB Output is correct
39 Correct 52 ms 49432 KB Output is correct
40 Correct 260 ms 54204 KB Output is correct
41 Correct 79 ms 54204 KB Output is correct
42 Correct 109 ms 54204 KB Output is correct
43 Correct 271 ms 64808 KB Output is correct
44 Correct 258 ms 71180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 71180 KB Output is correct
2 Correct 2 ms 71180 KB Output is correct
3 Correct 3 ms 71180 KB Output is correct
4 Correct 3 ms 71180 KB Output is correct
5 Correct 3 ms 71180 KB Output is correct
6 Correct 2 ms 71180 KB Output is correct
7 Correct 2 ms 71180 KB Output is correct
8 Correct 2 ms 71180 KB Output is correct
9 Correct 2 ms 71180 KB Output is correct
10 Correct 2 ms 71180 KB Output is correct
11 Correct 2 ms 71180 KB Output is correct
12 Correct 2 ms 71180 KB Output is correct
13 Correct 3 ms 71180 KB Output is correct
14 Correct 3 ms 71180 KB Output is correct
15 Correct 2 ms 71180 KB Output is correct
16 Correct 2 ms 71180 KB Output is correct
17 Correct 3 ms 71180 KB Output is correct
18 Correct 2 ms 71180 KB Output is correct
19 Correct 2 ms 71180 KB Output is correct
20 Correct 3 ms 71180 KB Output is correct
21 Correct 3 ms 71180 KB Output is correct
22 Correct 3 ms 71180 KB Output is correct
23 Correct 3 ms 71180 KB Output is correct
24 Correct 3 ms 71180 KB Output is correct
25 Correct 4 ms 71180 KB Output is correct
26 Correct 3 ms 71180 KB Output is correct
27 Correct 6 ms 71180 KB Output is correct
28 Correct 4 ms 71180 KB Output is correct
29 Correct 2 ms 71180 KB Output is correct
30 Correct 3 ms 71180 KB Output is correct
31 Correct 6 ms 71180 KB Output is correct
32 Correct 5 ms 71180 KB Output is correct
33 Correct 821 ms 72200 KB Output is correct
34 Correct 398 ms 72200 KB Output is correct
35 Correct 445 ms 72200 KB Output is correct
36 Correct 490 ms 72200 KB Output is correct
37 Correct 78 ms 72200 KB Output is correct
38 Correct 64 ms 72200 KB Output is correct
39 Correct 264 ms 72200 KB Output is correct
40 Correct 303 ms 72200 KB Output is correct
41 Correct 106 ms 72200 KB Output is correct
42 Correct 154 ms 72200 KB Output is correct
43 Correct 48 ms 72200 KB Output is correct
44 Correct 239 ms 77208 KB Output is correct
45 Correct 79 ms 77208 KB Output is correct
46 Correct 103 ms 77208 KB Output is correct
47 Correct 270 ms 87768 KB Output is correct
48 Correct 263 ms 93924 KB Output is correct
49 Correct 224 ms 93924 KB Output is correct
50 Correct 206 ms 93924 KB Output is correct
51 Correct 533 ms 116912 KB Output is correct
52 Correct 424 ms 128348 KB Output is correct
53 Correct 696 ms 128348 KB Output is correct
54 Correct 379 ms 128348 KB Output is correct
55 Correct 141 ms 128348 KB Output is correct
56 Correct 413 ms 145484 KB Output is correct
57 Correct 512 ms 145484 KB Output is correct
58 Correct 694 ms 145484 KB Output is correct
59 Correct 303 ms 156912 KB Output is correct