Submission #720167

# Submission time Handle Problem Language Result Execution time Memory
720167 2023-04-07T14:08:22 Z marvinthang Sails (IOI07_sails) C++17
100 / 100
56 ms 4704 KB
/*************************************
*    author: marvinthang             *
*    created: 23.08.2022 23:00:49    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                 div  ___div
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define             FULL(i)  (MASK(i) - 1)
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define          file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }

template <class T>             scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class T>            print_op(vector <T>)  { out << '{'; for (size_t i = 0; i + 1 < u.size(); ++i) out << u[i] << ", "; if (!u.empty()) out << u.back(); return out << '}'; }
template <class U, class V>   scan_op(pair <U, V>)  { return in >> u.fi >> u.se; }
template <class U, class V>  print_op(pair <U, V>)  { return out << '(' << u.fi << ", " << u.se << ')'; }

const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dir[] = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // {2, 1, -2, -1, -2, 1, 2, -1, 2};
const int MAX = 1e5 + 5;

int N;
pair <int, int> A[MAX];

void init(void) {
	cin >> N;
	for (int i = 1; i <= N; ++i) cin >> A[i];
	sort(A + 1, A + 1 + N);
}

int values[MAX];
set <int> pos;

void update(int u, int val) {
	values[u] += val;
	if (values[u]) pos.insert(u);
	else pos.erase(u);
}

void process(void) {
	update(0, MAX);
	for (int i = 1; i <= N; ++i) {
		int p = A[i].fi - A[i].se;
		__typeof(pos.begin()) it = pos.lower_bound(p);
		int up = it == pos.end() ? A[i].fi : *it;
		update(A[i].fi, 1);
		update(up, -1);
		it = prev(pos.upper_bound(p));
		update(*it + up - p, 1);
		update(*it, -1);
	}
	long long res = 0;
	for (int i = MAX, cur = 0; --i; ) {
		cur += values[i];
		res += 1LL * cur * (cur - 1) / 2;
	}
	cout << res << '\n';
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("sails");
	init();
	process();
	cerr << "Time elapsed: " << TIME << " s.\n";
	return (0^0);
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:22:69: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define          file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                             ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:73:2: note: in expansion of macro 'file'
   73 |  file("sails");
      |  ^~~~
sails.cpp:22:103: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define          file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                                                               ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:73:2: note: in expansion of macro 'file'
   73 |  file("sails");
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 596 KB Output is correct
2 Correct 17 ms 920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 2324 KB Output is correct
2 Correct 39 ms 2964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 4704 KB Output is correct
2 Correct 28 ms 1984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 3268 KB Output is correct
2 Correct 29 ms 2188 KB Output is correct