//# 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;
/*
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
Tree<int>tr;
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# define LOCAL
# define sim template < class c
# define ris return * this
# define dor > debug & operator <<
# define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define show(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int gcd(int a, int b)
{
if(b) return gcd(b,a%b);
return a;
}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int n,h,k,ans,a[1 << 17];
vector<pair<int,int>>vec;
const int N = 100005;
void upd(int idx,int val)
{
for(int i = idx;i <= N;i += (i & (-i))) a[i] += val;
}
int qry(int idx)
{
int x = 0;
if(idx == 0) return 0;
for(int i = idx;i;i -= (i & (-i))) x += a[i];
return x;
}
// 1:00
int32_t main(){_
//freopen("input","r",stdin);
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> h >> k;
vec.pb(mp(h,k));
}
sort(vec.begin(),vec.end());
int mx = 1,cnt = 0;
for(auto it : vec)
{
h = it.first;k = it.second;
//cout << h << ' ' << k << '\n';
while(cnt < h)
{
cnt++;
upd(cnt,mx - qry(cnt));
}
//if(h == 5) for(int i = 1;i <= cnt;i++) cout << qry(i) - qry(i - 1) << ' ';
//cout << "\n";
int l = cnt - k + 1;
int r = cnt;
int f = qry(cnt) - qry(l - 1);
if(f == 0)
{
l = cnt + 1;
}
else
{
while(l < r)
{
int mid = l + r + 1 >> 1;
if(qry(cnt) - qry(mid - 1) == f) l = mid;
else r = mid - 1;
}
}
int d = l - (cnt - k + 1);
if(l != cnt + 1) upd(l,-1);
l = 1;
r = cnt - k + 1;
f = qry(cnt - k + 1);
while(l < r)
{
int mid = l + r >> 1;
if(qry(mid) == f) r = mid;
else l = mid + 1;
}
upd(l,-1);
upd(l + d,1);
if(!qry(1))
{
mx++;
upd(1,1);
}
/*cout << mx << '\n';
for(int i = 1;i <= cnt;i++) cout << qry(i) - qry(i - 1) << ' ';
cout << "\n\n";
*/
}
for(int i = 1;i <= cnt;i++)
{
mx -= qry(i) - qry(i - 1);
ans += (mx * (mx - 1)) / 2;
}
rc(ans);
return 0;
}
Compilation message
sails.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
# pragma GCC optimization ("unroll-loops")
sails.cpp: In function 'int32_t main()':
sails.cpp:120:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r + 1 >> 1;
~~~~~~^~~
sails.cpp:132:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
7 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
936 KB |
Output is correct |
2 |
Correct |
27 ms |
1240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
1192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
1680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
2588 KB |
Output is correct |
2 |
Correct |
85 ms |
2672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
2636 KB |
Output is correct |
2 |
Correct |
60 ms |
2800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
2792 KB |
Output is correct |
2 |
Correct |
66 ms |
2540 KB |
Output is correct |