Submission #317494

# Submission time Handle Problem Language Result Execution time Memory
317494 2020-10-29T19:52:27 Z andremfq Bulldozer (JOI17_bulldozer) C++17
100 / 100
1143 ms 17128 KB
#include <bits/stdc++.h>
 
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define ll long long
 
const int MAXN = 2010 ;
 
using namespace std ;
 
struct Seg
{
	
	long long soma[MAXN*4] ;
	long long bestLeft[MAXN*4] ;
	long long bestRight[MAXN*4] ;
	long long bestSum[MAXN*4] ;
 
	int m(int l, int r ) { return (l+r)>>1 ; }
  
	void upd(int pos, int l, int r, int k, long long val ) //vamos de seg de setar?
	{
		if( l == r ) 
		{
			soma[pos] = val ;
			bestLeft[pos] = bestRight[pos] = bestSum[pos] = max(val, 0LL ) ;
			return ;
		}
 
		if( k <= m(l,r) ) upd(pos<<1 , l , m(l,r) , k, val ) ;
		else upd(pos<<1|1 , m(l,r)+1, r, k, val ) ;
 
		soma[pos] = soma[pos<<1] + soma[pos<<1|1] ;
		bestLeft[pos] = max(bestLeft[pos<<1|1] , soma[pos<<1|1] + bestLeft[pos<<1] ) ;
		bestRight[pos] = max(bestRight[pos<<1], soma[pos<<1] + bestRight[pos<<1|1] ) ;
		bestSum[pos] = bestLeft[pos<<1] + bestRight[pos<<1|1] ;
 
		bestSum[pos] = max(bestSum[pos<<1] ,max(bestSum[pos] , bestSum[pos<<1|1] ) ) ;
 
	}
 
	long long getAns() { return bestSum[1] ; }
 
} seg ;
 
int n ;
int pos[MAXN] ;
long long x[MAXN], y[MAXN] ;
long long w[MAXN] ;
 
int getFakeQuadrant(int i, int j )
{
	
	if( x[i] > x[j] ) return 0 ;
	if(x[i] == x[j] ) return 1 ;
	return 2 ;
 
}
 
struct Event
{
 
	int i , j ;
 
	Event(int i = 0 , int j = 0 ) : i(i) , j(j) {}
	
	bool operator < (Event other) const 
	{
		
		/* Tenho certeza que:
			i) y[i] > y[j]
			ii) y[i] == y[j] && x[i] > x[j]
 
			O numerador sempre vai ser positivo ou zero
		*/
 
		pair<long long,long long> mySlope = make_pair(y[i]-y[j], x[i]-x[j] ) ;
		pair<long long,long long> otherSlope = make_pair(y[other.i]-y[other.j], x[other.i]-x[other.j] ) ;
 
		int myFakeQuadrant =  getFakeQuadrant(i,j) ;
		int otherFakeQuadrant = getFakeQuadrant(other.i , other.j ) ;
 
		if(myFakeQuadrant != otherFakeQuadrant ) 
			return myFakeQuadrant < otherFakeQuadrant ;
 
		//Remember the denominators can be negative
		//And, therefore, the comparison will be weird	
 
		if( mySlope.first * otherSlope.second == mySlope.second * otherSlope.first )
		{
			//This will work even if there isn't a slope at all
 
			if( mySlope.first == 0 )
				return make_pair( x[i] , x[j] ) > make_pair( x[other.i] , x[other.j] ) ;
 
			return make_pair(y[i], y[j] ) > make_pair(y[other.i],  y[other.j] ) ;
		}
 
		if(mySlope.second < 0 ) 
		{
			mySlope.first = -mySlope.first ;
			mySlope.second = -mySlope.second ;
		}
		if(otherSlope.second < 0 )
		{
			otherSlope.first = -otherSlope.first ;
			otherSlope.second = -otherSlope.second ;
		}
 
		return mySlope.first * otherSlope.second < mySlope.second * otherSlope.first ;
		
	}
} ;
 
bool isEqual(Event a, Event b )
{
 
	pair<long long,long long> slope_a = make_pair( y[a.i] - y[a.j] , x[a.i] - x[a.j] ) ;
	pair<long long,long long> slope_b = make_pair( y[b.i] - y[b.j], x[b.i] - x[b.j] ) ;
 
	if( getFakeQuadrant(a.i, a.j ) != getFakeQuadrant(b.i, b.j ) )
		return false ;
 
	return slope_a.first * slope_b.second == slope_a.second * slope_b.first ;
 
}
     
int main()
{
	scanf("%d", &n ) ;
	for(int i = 1 ; i <= n ; i++ ) scanf("%lld %lld %lld", &x[i], &y[i] , &w[i] ) ;
 
	vector<int> initialSort(n) ;
	iota(all(initialSort),1) ;
	sort(all(initialSort), [&](int i, int j ) { 
	
		if( y[i] != y[j] ) return y[i] > y[j] ;
		return x[i] > x[j] ; 
 
	} ) ;
 
	vector<Event> sweep ;
	
	for(int i = 0 ; i < n ; i++ ) pos[ initialSort[i] ] = i+1 ;
	
	for(int i = 0 ; i < n ; i++ )
		for(int j = i+1 ; j < n ; j++ )
			sweep.push_back(Event(initialSort[i],initialSort[j]) ) ;
 
	sort(all(sweep));
 
	for(int i = 1 ; i <= n ; i++ ) seg.upd(1,1,n, pos[i] , w[i] ) ;
	
	long long ans = 0LL ;
	ans = max(ans, seg.getAns() ) ;
 
	for(int i = 0 ; i < sz(sweep ) ;  )
	{
		//initiating batch process
		int j = i ;
 
		while( j < sz(sweep) && isEqual(sweep[i], sweep[j] ) )
		{
			int u = sweep[j].i ;
			int v = sweep[j].j ;
 
			seg.upd(1 , 1 , n , pos[u] , w[v] ) ;
			seg.upd(1,1,n, pos[v], w[u] ) ;
 
			swap(pos[u], pos[v] ) ;
 
			j++ ;
		}		
 
		i = j ;
 
		ans = max(ans, seg.getAns() ) ;
	}
 
	printf("%lld\n", ans ) ;
 
}

Compilation message

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:130:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  130 |  scanf("%d", &n ) ;
      |  ~~~~~^~~~~~~~~~~
bulldozer.cpp:131:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  131 |  for(int i = 1 ; i <= n ; i++ ) scanf("%lld %lld %lld", &x[i], &y[i] , &w[i] ) ;
      |                                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 416 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 416 KB Output is correct
33 Correct 1072 ms 17000 KB Output is correct
34 Correct 1060 ms 17000 KB Output is correct
35 Correct 1073 ms 17128 KB Output is correct
36 Correct 1105 ms 17000 KB Output is correct
37 Correct 1054 ms 17000 KB Output is correct
38 Correct 1108 ms 17128 KB Output is correct
39 Correct 1059 ms 17000 KB Output is correct
40 Correct 1090 ms 17000 KB Output is correct
41 Correct 1063 ms 17000 KB Output is correct
42 Correct 1101 ms 17000 KB Output is correct
43 Correct 1022 ms 17000 KB Output is correct
44 Correct 1026 ms 17000 KB Output is correct
45 Correct 1018 ms 17000 KB Output is correct
46 Correct 1028 ms 16992 KB Output is correct
47 Correct 1041 ms 17000 KB Output is correct
48 Correct 1055 ms 17128 KB Output is correct
49 Correct 1077 ms 17000 KB Output is correct
50 Correct 1061 ms 16992 KB Output is correct
51 Correct 1065 ms 17000 KB Output is correct
52 Correct 1060 ms 17128 KB Output is correct
53 Correct 1056 ms 16988 KB Output is correct
54 Correct 1073 ms 17128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 416 KB Output is correct
33 Correct 1072 ms 17000 KB Output is correct
34 Correct 1060 ms 17000 KB Output is correct
35 Correct 1073 ms 17128 KB Output is correct
36 Correct 1105 ms 17000 KB Output is correct
37 Correct 1054 ms 17000 KB Output is correct
38 Correct 1108 ms 17128 KB Output is correct
39 Correct 1059 ms 17000 KB Output is correct
40 Correct 1090 ms 17000 KB Output is correct
41 Correct 1063 ms 17000 KB Output is correct
42 Correct 1101 ms 17000 KB Output is correct
43 Correct 1022 ms 17000 KB Output is correct
44 Correct 1026 ms 17000 KB Output is correct
45 Correct 1018 ms 17000 KB Output is correct
46 Correct 1028 ms 16992 KB Output is correct
47 Correct 1041 ms 17000 KB Output is correct
48 Correct 1055 ms 17128 KB Output is correct
49 Correct 1077 ms 17000 KB Output is correct
50 Correct 1061 ms 16992 KB Output is correct
51 Correct 1065 ms 17000 KB Output is correct
52 Correct 1060 ms 17128 KB Output is correct
53 Correct 1056 ms 16988 KB Output is correct
54 Correct 1073 ms 17128 KB Output is correct
55 Correct 1058 ms 17000 KB Output is correct
56 Correct 1068 ms 17128 KB Output is correct
57 Correct 1069 ms 17000 KB Output is correct
58 Correct 1075 ms 17000 KB Output is correct
59 Correct 1088 ms 17000 KB Output is correct
60 Correct 1060 ms 17000 KB Output is correct
61 Correct 1088 ms 17000 KB Output is correct
62 Correct 1076 ms 17000 KB Output is correct
63 Correct 1143 ms 17000 KB Output is correct
64 Correct 1052 ms 17128 KB Output is correct
65 Correct 1050 ms 17000 KB Output is correct
66 Correct 1089 ms 17000 KB Output is correct
67 Correct 1057 ms 17000 KB Output is correct
68 Correct 1098 ms 17000 KB Output is correct
69 Correct 1038 ms 17000 KB Output is correct
70 Correct 1061 ms 17128 KB Output is correct
71 Correct 1057 ms 17000 KB Output is correct
72 Correct 1062 ms 17000 KB Output is correct
73 Correct 1071 ms 17000 KB Output is correct
74 Correct 1040 ms 17000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 416 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 0 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 2 ms 512 KB Output is correct
38 Correct 3 ms 512 KB Output is correct
39 Correct 3 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 416 KB Output is correct
48 Correct 1072 ms 17000 KB Output is correct
49 Correct 1060 ms 17000 KB Output is correct
50 Correct 1073 ms 17128 KB Output is correct
51 Correct 1105 ms 17000 KB Output is correct
52 Correct 1054 ms 17000 KB Output is correct
53 Correct 1108 ms 17128 KB Output is correct
54 Correct 1059 ms 17000 KB Output is correct
55 Correct 1090 ms 17000 KB Output is correct
56 Correct 1063 ms 17000 KB Output is correct
57 Correct 1101 ms 17000 KB Output is correct
58 Correct 1022 ms 17000 KB Output is correct
59 Correct 1026 ms 17000 KB Output is correct
60 Correct 1018 ms 17000 KB Output is correct
61 Correct 1028 ms 16992 KB Output is correct
62 Correct 1041 ms 17000 KB Output is correct
63 Correct 1055 ms 17128 KB Output is correct
64 Correct 1077 ms 17000 KB Output is correct
65 Correct 1061 ms 16992 KB Output is correct
66 Correct 1065 ms 17000 KB Output is correct
67 Correct 1060 ms 17128 KB Output is correct
68 Correct 1056 ms 16988 KB Output is correct
69 Correct 1073 ms 17128 KB Output is correct
70 Correct 1058 ms 17000 KB Output is correct
71 Correct 1068 ms 17128 KB Output is correct
72 Correct 1069 ms 17000 KB Output is correct
73 Correct 1075 ms 17000 KB Output is correct
74 Correct 1088 ms 17000 KB Output is correct
75 Correct 1060 ms 17000 KB Output is correct
76 Correct 1088 ms 17000 KB Output is correct
77 Correct 1076 ms 17000 KB Output is correct
78 Correct 1143 ms 17000 KB Output is correct
79 Correct 1052 ms 17128 KB Output is correct
80 Correct 1050 ms 17000 KB Output is correct
81 Correct 1089 ms 17000 KB Output is correct
82 Correct 1057 ms 17000 KB Output is correct
83 Correct 1098 ms 17000 KB Output is correct
84 Correct 1038 ms 17000 KB Output is correct
85 Correct 1061 ms 17128 KB Output is correct
86 Correct 1057 ms 17000 KB Output is correct
87 Correct 1062 ms 17000 KB Output is correct
88 Correct 1071 ms 17000 KB Output is correct
89 Correct 1040 ms 17000 KB Output is correct
90 Correct 1074 ms 17000 KB Output is correct
91 Correct 1076 ms 17000 KB Output is correct
92 Correct 1094 ms 17000 KB Output is correct
93 Correct 1061 ms 17000 KB Output is correct
94 Correct 1057 ms 17000 KB Output is correct
95 Correct 1040 ms 17128 KB Output is correct
96 Correct 1054 ms 17000 KB Output is correct
97 Correct 1102 ms 17000 KB Output is correct
98 Correct 1059 ms 17000 KB Output is correct
99 Correct 1068 ms 17000 KB Output is correct
100 Correct 953 ms 17000 KB Output is correct
101 Correct 1057 ms 17000 KB Output is correct
102 Correct 927 ms 17000 KB Output is correct
103 Correct 949 ms 17000 KB Output is correct
104 Correct 933 ms 17000 KB Output is correct
105 Correct 1008 ms 17000 KB Output is correct
106 Correct 994 ms 17000 KB Output is correct
107 Correct 1002 ms 17116 KB Output is correct
108 Correct 967 ms 17000 KB Output is correct
109 Correct 979 ms 17000 KB Output is correct
110 Correct 950 ms 17000 KB Output is correct
111 Correct 961 ms 17000 KB Output is correct
112 Correct 968 ms 17000 KB Output is correct
113 Correct 967 ms 17000 KB Output is correct
114 Correct 961 ms 16992 KB Output is correct
115 Correct 960 ms 17000 KB Output is correct
116 Correct 970 ms 16988 KB Output is correct
117 Correct 969 ms 17024 KB Output is correct
118 Correct 962 ms 17000 KB Output is correct
119 Correct 969 ms 17000 KB Output is correct
120 Correct 1 ms 384 KB Output is correct
121 Correct 1 ms 384 KB Output is correct
122 Correct 1038 ms 17128 KB Output is correct
123 Correct 1028 ms 17000 KB Output is correct
124 Correct 1057 ms 17000 KB Output is correct
125 Correct 1034 ms 17128 KB Output is correct
126 Correct 1037 ms 17000 KB Output is correct
127 Correct 1026 ms 17000 KB Output is correct
128 Correct 1062 ms 17000 KB Output is correct
129 Correct 1077 ms 17000 KB Output is correct
130 Correct 1091 ms 17000 KB Output is correct
131 Correct 1060 ms 17128 KB Output is correct
132 Correct 1062 ms 17000 KB Output is correct
133 Correct 1077 ms 16992 KB Output is correct