結果

問題 No.728 ギブ and テイク
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-06 05:58:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 4,811 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 142,604 KB
実行使用メモリ 24,268 KB
最終ジャッジ日時 2024-03-06 05:58:30
合計ジャッジ時間 7,368 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 17 ms
6,676 KB
testcase_14 AC 26 ms
6,676 KB
testcase_15 AC 12 ms
6,676 KB
testcase_16 AC 24 ms
6,676 KB
testcase_17 AC 22 ms
6,676 KB
testcase_18 AC 234 ms
13,596 KB
testcase_19 AC 250 ms
13,860 KB
testcase_20 AC 323 ms
19,844 KB
testcase_21 AC 261 ms
14,096 KB
testcase_22 AC 96 ms
8,228 KB
testcase_23 AC 60 ms
6,676 KB
testcase_24 AC 188 ms
12,644 KB
testcase_25 AC 187 ms
12,624 KB
testcase_26 AC 93 ms
8,164 KB
testcase_27 AC 371 ms
20,676 KB
testcase_28 AC 215 ms
24,268 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
template<class T,T (*op)(T,T),T (*e)()> struct segtree
{
	private:
	int size,N;
	vector<T> node;

	public:
	segtree(int N = 0) : segtree(vector<T>(N,e())) {}

	segtree(const vector<T> &V)
	{
		N = (int)V.size();
		size = 1;
		while(size < N)
		{
			size <<= 1;
		}
		node.assign(2 * size,e());
		for(int i = 0;i < N;i++)
		{
			node[i + size] = V[i];
		}
		for(int i = size - 1;i >= 1;i--)
		{
			node[i] = op(node[2 * i],node[2 * i + 1]);
		}
	}

	void replace(int pos,T x)
	{
		func_update(pos,x,[](T u,T v){return u = v;});
	}

	void add(int pos,T x)
	{
		func_update(pos,x,[](T u,T v){return u + v;});
	}

	void func_update(int pos,T x)
	{
		func_update(pos,x,op);
	}

	void func_update(int pos,T x,T (*func)(T,T))
	{
		assert(0 <= pos && pos < N);
		pos += size;
		node[pos] = func(node[pos],x);
		while(pos >> 1)
		{
			pos >>= 1;
			node[pos] = op(node[2 * pos],node[2 * pos + 1]);
		}
	}

	T operator[](int pos)
	{
		assert(0 <= pos && pos < N);
		return node[pos + size];
	}

	int get_log()
	{
		int res = 0;
		while(1 << res < size)
		{
			res++;
		}
		assert((1 << res) == size);
		return res;
	}

	vector<vector<T>> tree_list()
	{
		vector<vector<T>> res;
		int log = get_log();
		res.reserve(log + 1);
		for(int i = 0;i <= log;i++)
		{
			int L = 1 << log,R = L << 1;
			vector<T> cur(node.begin() + L,node.begin() + R);
			res.push_back(cur);
		}
		return res;
	}

	//suppose that class T has ostream &operator<<
	void print(int k)
	{
		assert(0 <= k && k <= get_log());
		int L = 1 << k,R = L << 1;
		for(int i = L;i < R;i++)
		{
			cerr << node[i] << (i + 1 == R ? "\n":" ");
		}
	}

	void print()
	{
		int log = get_log();
		for(int k = 0;k <= log;k++)
		{
			int L = 1 << k,R = L << 1;
			const string unit((1 << (log - k)) - 1,' ');
			for(int i = L;i < R;i++)
			{
				if(i == L)
				{
					cerr << unit;
				}
				cerr << node[i];
				if(i + 1 < R)
				{
					cerr << unit << unit << ' ';
				}
				else
				{
					cerr << unit;
				}
			}
			cerr << "\n";
		}
	}

	T prod(int l,int r)
	{
		assert(0 <= l && l <= r && r <= N);
		T L = e(),R = e();
		l += size,r += size;
		while(l < r)
		{
			if(l & 1)
			{
				L = op(L,node[l++]);
			}
			if(r & 1)
			{
				R = op(node[--r],R);
			}
			l >>= 1,r >>= 1;
		}
		return op(L,R);
	}
	T prod()
	{
		return node[1];
	}

	template<class F> int max_right(int l,F f)
	{
		assert(0 <= l && l <= N);
		assert(f(e()));
		if(l == N)
		{
			return N;
		}
		l += size;
		T val = e();
		do
		{
			while(!(l & 1))
			{
				l >>= 1;
			}
			if(!f(op(val,node[l])))
			{
				while(l < size)
				{
					l <<= 1;
					if(f(op(val,node[l])))
					{
						val = op(val,node[l++]);
					}
				}
				return l - size;
			}
			val = op(val,node[l++]);
		} while((l & -l) != l);
		return N;
	}

	template<class F> int min_left(int r,F f)
	{
		assert(0 <= r && r <= N);
		assert(f(e()));
		if(r == 0)
		{
			return 0;
		}
		r += size;
		T val = e();
		do
		{
			r--;
			while(r > 1 && (r & 1))
			{
				r >>= 1;
			}
			if(!f(op(node[r],val)))
			{
				while(r < size)
				{
					r <<= 1;
					r++;
					if(f(op(node[r],val)))
					{
						val = op(node[r--],val);
					}
				}
				return r + 1 - size;
			}
			val = op(node[r],val);
		} while((r & -r) != r);
		return 0;
	}
};
int op(int a,int b)
{
	return a + b;
}
int e()
{
	return 0;
}
void Main()
{
	int N;
	cin >> N;
	vector<int> A(N),L(N),R(N);
	for(int i = 0;i < N;i++)
	{
		cin >> A[i];
	}
	for(int i = 0;i < N;i++)
	{
		cin >> L[i] >> R[i];
	}
	vector<int> comp;
	comp.reserve(2 * N);
	for(int i = 0;i < N;i++)
	{
		comp.push_back(A[i]);
		comp.push_back(A[i] - L[i]);
	}
	sort(comp.begin(),comp.end());
	comp.erase(unique(comp.begin(),comp.end()),comp.end());
	long long ans = 0;
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> Q;
	segtree<int,op,e> seg(comp.size());
	for(int i = 0;i < N;i++)
	{
		while(!Q.empty())
		{
			auto [v,id] = Q.top();
			if(v >= A[i])
			{
				break;
			}
			Q.pop();
			seg.add(id,-1);
		}
		{
			int id = lower_bound(comp.begin(),comp.end(),A[i] - L[i]) - comp.begin();
			ans += seg.prod(id,comp.size());
		}
		{
			int id = lower_bound(comp.begin(),comp.end(),A[i]) - comp.begin();
			seg.add(id,1);
			Q.push(make_pair(A[i] + R[i],id));
		}
	}
	cout << ans << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0