結果

問題 No.3330 Many Point Chmax Range Sum
ユーザー shobonvip
提出日時 2025-12-11 16:52:01
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 1,324 ms / 3,500 ms
コード長 3,518 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,750 ms
コンパイル使用メモリ 267,588 KB
実行使用メモリ 116,972 KB
最終ジャッジ日時 2025-12-11 16:52:32
合計ジャッジ時間 27,921 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/**
	author:  shobonvip
	created: 2025.12.11 16:28:50
**/

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, k, q;
	cin >> n >> k >> q;
	vector<ll> a(n);
	rep(i,0,n) {
		cin >> a[i];
	}

	const int sz = 1 << 18;
	vector<ll> rt(k);
	vector<int> p(k);

	// make range tree
	vector v(2*sz, vector<int>(0));
	rep(i,0,k) {
		cin >> p[i] >> rt[i];
		p[i]--;
		int x = p[i] + sz;
		while (x > 0) {
			v[x].emplace_back(i);
			x >>= 1;
		}
	}
	rep(i,0,2*sz) {
		sort(all(v[i]));
		v[i].erase(unique(all(v[i])), v[i].end());
	}
	vector<fenwick_tree<ll>> fw(2*sz, fenwick_tree<ll>(0));
	rep(i,0,2*sz) {
		fw[i] = fenwick_tree<ll>((int)v[i].size());
	}

	vector<int> l(q),r(q);
	vector<int> d(q),u(q);
	rep(i,0,q){
		cin >> l[i] >> r[i] >> d[i] >> u[i];
		l[i]--;
		d[i]--;
	}

	vector bucket(k, vector<int>(0));
	rep(i,0,q) {
		bucket[l[i]].emplace_back(i);
	}

	auto add = [&](int x, int y, ll hen) ->void {
		//cout << x << ' ' << y << ' ' << hen << endl;
		x += sz;
		while (x > 0) {
			int t = int(lower_bound(all(v[x]), y) - v[x].begin());
			fw[x].add(t, hen);
			x >>= 1;
		}
	};

	auto que = [&](int l, int r, int mx) -> ll {
		l += sz;
		r += sz;
		ll ans = 0;
		while (l < r) {
			if (l & 1) {
				int t = int(lower_bound(all(v[l]), mx) - v[l].begin());
				ans += fw[l].sum(0, t);
				l++;
			}
			if (r & 1) {
				r--;
				int t = int(lower_bound(all(v[r]), mx) - v[r].begin());
				ans += fw[r].sum(0, t);
			}
			l >>= 1;
			r >>= 1;
		}
		return ans;
	};

	vector<ll> ans(q);
	vector<ll> rui(n+1);
	rep(i,0,n) rui[i+1] = rui[i] + a[i];

	vector val(n, vector<ll>(1, ll(1e18)));
	vector ind(n, vector<int>(1, k));
	vector sub(n, vector<ll>(1, 0LL));
	rrep(i,0,k) {
		int x = p[i];
		if (a[x] < rt[i]) {
			while (rt[i] >= val[x].back()) {
				add(x, ind[x].back(), - sub[x].back());
				val[x].pop_back();
				ind[x].pop_back();
				sub[x].pop_back();
			}
			val[x].push_back(rt[i]);
			ind[x].push_back(i);
			sub[x].push_back(rt[i] - a[x]);
			add(
				x,
				ind[x].back(),
				sub[x].back()
			);
			ll new_sa = val[x][(int)val[x].size()-2] - val[x].back();
			if (ind[x][(int)ind[x].size()-2] < k) {
				add(
					x,
					ind[x][(int)ind[x].size()-2], 
					new_sa - sub[x][(int)sub[x].size()-2]
				);
				sub[x][(int)sub[x].size() - 2] = new_sa;
			}
		}

		for (int ind: bucket[i]) {
			ans[ind] = que(d[ind], u[ind], r[ind]) + rui[u[ind]] - rui[d[ind]];
		}
	}

	rep(i,0,q) {
		cout << ans[i] << '\n';
	}

}

0