Submission #138124

# Submission time Handle Problem Language Result Execution time Memory
138124 2019-07-29T11:51:51 Z claudy Horses (IOI15_horses) C++14
100 / 100
1059 ms 58104 KB
# include "horses.h"
//# pragma GCC optimize("Ofast,no-stack-protector")
//# pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
# pragma GCC optimize("Ofast")
# pragma GCC optimization ("unroll-loops")
# include "bits/stdc++.h"
/*
# include <ext/pb_ds/tree_policy.hpp>
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/rope>
*/
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define rcg(s) cout << s;exit(0)
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define all(s) s.begin(),s.end()
# define sz(x) (int)((x).size())
# define int ll
using namespace std;

// 13:00

const int mod = 1e9 + 7;

set<int>s,S;
int fen[1 << 19],x[1 << 19],y[1 << 19],t[1 << 21],n,b[1 << 19];

void upd(int nod,int l,int r,int pos,int val)
{
	if(l == r)
	{
		t[nod] = val;
		return ;
	}
	int mid = l + r >> 1;
	if(pos <= mid) upd(nod << 1,l,mid,pos,val);
	else upd(nod << 1 | 1,mid + 1,r,pos,val);
	t[nod] = max(t[nod << 1],t[nod << 1 | 1]);
}

int qry(int nod,int tl,int tr,int l,int r)
{
	if(l > r) return 0;
	if(tl == l && tr == r) return t[nod];
	int mid = tl + tr >> 1;
	return max(qry(nod << 1,tl,mid,l,min(mid,r)),qry(nod << 1 | 1,mid + 1,tr,max(mid + 1,l),r));
}

void UPD(int pos,int val)
{
	pos++;
	for(int i = pos;i <= n;i += (i & (-i)))
	{
		fen[i] *= val;
		fen[i] %= mod;
	}
}

int QRY(int pos)
{
	pos++;
	int ans = 1;
	for(int i = pos;i;i -= (i & (-i)))
	{
		ans *= fen[i];
		ans %= mod;
	}
	return ans;
}


int f()
{
	auto it = --(S.end());
	for(int i = 0;i <= 35;i++)
	{
		if(it != S.begin()) it--;
	}
	int pos = (*it) + 1;
	vector<pair<int,int>>vec;
	vector<int>hm;
	int i = pos;
	while(i < n)
	{
		if(x[i] == 1)
		{
			int r = *(S.upper_bound(i)) - 1;
			vec.pb(mp(x[i],qry(1,0,n - 1,i,r)));
			hm.pb(i);
			i = r + 1;
		}
		else
		{
			vec.pb(mp(x[i],y[i]));
			hm.pb(i);
			i++;
		}
	}
	for(int i = 0;i < sz(vec);i++) b[i] = 1;
	for(int i = 0;i < sz(vec);i++)
	{
		int j = i + 1;
		int prod = 1;
		while(j < sz(vec))
		{
			prod *= vec[j].first;
			if(prod >= vec[i].second)
			{
				b[i] = 0;
				break;
			}
			else if(prod * vec[j].second >= vec[i].second)
			{
				b[i] = 0;
			}
			else b[j] = 0;
			j++;
		}
	}
	int f = 0;
	for(int i = 0;i < sz(vec);i++)
	{
		if(b[i]) f = i;
	}
	return ((vec[f].second * QRY(hm[f])) % mod);
}

# undef int
int init(int N, int X[], int Y[])
{
# define int ll
	n = N;
	S.insert(n);S.insert(-1);
	for(int i = 0;i <= n + 1;i++) fen[i] = 1;
	for(int i = 0;i < n;i++)
	{
		x[i] = X[i];
		y[i] = Y[i];
		UPD(i,x[i]);
		if(X[i] > 1) S.insert(i);
		upd(1,0,n - 1,i,Y[i]);
	}
	//cout << f() << '\n';
	return f();
}

int pw(int val,int p)
{
	int ans = 1;
	for(int i = 0;(1LL << i) <= p;i++)
	{
		if(p & (1LL << i))
		{
			ans *= val;
			ans %= mod;
		}
		val *= val;
		val %= mod;
	}
	return ans;
}

# undef int
int updateX(int pos, int val)
{
# define int ll
	if(x[pos] > 1 && S.find(pos) != S.end()) S.erase(S.find(pos));
	if(val > 1) S.insert(pos);
	UPD(pos,(val * pw(x[pos],mod - 2)) % mod);
	x[pos] = val;
	//cout << f() << '\n';	
	return f();
}

# undef int
int updateY(int pos, int val)
{
# define int ll
	upd(1,0,n - 1,pos,val);
	y[pos] = val;
	//cout << f() << '\n';	
	return f();
}

Compilation message

horses.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 # pragma GCC optimization ("unroll-loops")
 
horses.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:40:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = l + r >> 1;
            ~~^~~
horses.cpp: In function 'long long int qry(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:50:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = tl + tr >> 1;
            ~~~^~~~
horses.cpp: In function 'long long int f()':
horses.cpp:104:10: warning: declaration of 'i' shadows a previous local [-Wshadow]
  for(int i = 0;i < sz(vec);i++) b[i] = 1;
          ^
horses.cpp:87:6: note: shadowed declaration is here
  int i = pos;
      ^
horses.cpp:105:10: warning: declaration of 'i' shadows a previous local [-Wshadow]
  for(int i = 0;i < sz(vec);i++)
          ^
horses.cpp:87:6: note: shadowed declaration is here
  int i = pos;
      ^
horses.cpp:126:10: warning: declaration of 'i' shadows a previous local [-Wshadow]
  for(int i = 0;i < sz(vec);i++)
          ^
horses.cpp:87:6: note: shadowed declaration is here
  int i = pos;
      ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:149:10: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
  return f();
         ~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:177:10: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
  return f();
         ~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:187:10: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
  return f();
         ~^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 380 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 8 ms 504 KB Output is correct
24 Correct 5 ms 376 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
26 Correct 5 ms 632 KB Output is correct
27 Correct 7 ms 504 KB Output is correct
28 Correct 6 ms 504 KB Output is correct
29 Correct 4 ms 376 KB Output is correct
30 Correct 5 ms 504 KB Output is correct
31 Correct 4 ms 376 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 496 ms 48632 KB Output is correct
2 Correct 630 ms 48652 KB Output is correct
3 Correct 595 ms 52436 KB Output is correct
4 Correct 761 ms 56284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 296 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 380 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 7 ms 376 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
26 Correct 5 ms 504 KB Output is correct
27 Correct 7 ms 476 KB Output is correct
28 Correct 6 ms 504 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 5 ms 504 KB Output is correct
31 Correct 4 ms 504 KB Output is correct
32 Correct 6 ms 376 KB Output is correct
33 Correct 181 ms 28408 KB Output is correct
34 Correct 122 ms 28460 KB Output is correct
35 Correct 359 ms 56636 KB Output is correct
36 Correct 345 ms 56628 KB Output is correct
37 Correct 167 ms 26616 KB Output is correct
38 Correct 276 ms 39292 KB Output is correct
39 Correct 105 ms 26428 KB Output is correct
40 Correct 332 ms 53820 KB Output is correct
41 Correct 104 ms 26372 KB Output is correct
42 Correct 143 ms 26556 KB Output is correct
43 Correct 310 ms 54092 KB Output is correct
44 Correct 309 ms 54264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 380 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 1 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 8 ms 504 KB Output is correct
24 Correct 6 ms 504 KB Output is correct
25 Correct 6 ms 504 KB Output is correct
26 Correct 4 ms 504 KB Output is correct
27 Correct 6 ms 376 KB Output is correct
28 Correct 6 ms 508 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 5 ms 504 KB Output is correct
31 Correct 4 ms 376 KB Output is correct
32 Correct 6 ms 504 KB Output is correct
33 Correct 498 ms 52472 KB Output is correct
34 Correct 627 ms 57784 KB Output is correct
35 Correct 610 ms 52600 KB Output is correct
36 Correct 761 ms 56440 KB Output is correct
37 Correct 181 ms 28408 KB Output is correct
38 Correct 121 ms 28372 KB Output is correct
39 Correct 361 ms 56640 KB Output is correct
40 Correct 344 ms 56772 KB Output is correct
41 Correct 167 ms 26488 KB Output is correct
42 Correct 276 ms 39376 KB Output is correct
43 Correct 105 ms 26312 KB Output is correct
44 Correct 337 ms 53852 KB Output is correct
45 Correct 104 ms 26488 KB Output is correct
46 Correct 145 ms 26456 KB Output is correct
47 Correct 307 ms 54108 KB Output is correct
48 Correct 314 ms 54168 KB Output is correct
49 Correct 935 ms 31480 KB Output is correct
50 Correct 357 ms 31480 KB Output is correct
51 Correct 708 ms 58104 KB Output is correct
52 Correct 540 ms 57976 KB Output is correct
53 Correct 844 ms 29816 KB Output is correct
54 Correct 1059 ms 43400 KB Output is correct
55 Correct 247 ms 27472 KB Output is correct
56 Correct 595 ms 55544 KB Output is correct
57 Correct 238 ms 27972 KB Output is correct
58 Correct 637 ms 28608 KB Output is correct
59 Correct 310 ms 54136 KB Output is correct