結果

問題 No.631 Noelちゃんと電車旅行
ユーザー tancahn2380tancahn2380
提出日時 2019-01-03 18:44:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 3,666 bytes
コンパイル時間 1,666 ms
コンパイル使用メモリ 171,580 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-04-10 07:55:58
合計ジャッジ時間 6,338 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 182 ms
5,248 KB
testcase_01 AC 221 ms
8,832 KB
testcase_02 AC 219 ms
8,832 KB
testcase_03 AC 231 ms
8,832 KB
testcase_04 AC 229 ms
8,704 KB
testcase_05 AC 224 ms
8,832 KB
testcase_06 AC 90 ms
5,760 KB
testcase_07 AC 42 ms
6,016 KB
testcase_08 AC 173 ms
8,320 KB
testcase_09 AC 208 ms
5,888 KB
testcase_10 AC 136 ms
8,320 KB
testcase_11 AC 126 ms
6,272 KB
testcase_12 AC 155 ms
8,448 KB
testcase_13 AC 154 ms
5,376 KB
testcase_14 AC 75 ms
5,376 KB
testcase_15 AC 135 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

struct SegmentTree {
private:
	int n;
	vector<LL> segMax, segAdd;
public:
	SegmentTree(vector<LL> v) {
		int sz = v.size();
		n = 1; while (n < sz) n *= 2;
		segMax.resize(2 * n - 1, 0);
		segAdd.resize(2 * n - 1, 0);
		for (int i = 0; i<sz; i++) segMax[i + n - 1] = v[i];
		for (int i = n - 2; i >= 0; i--) segMax[i] = max(segMax[2 * i + 1], segMax[2 * i + 2]);
	}

	//区間[a, b)に値xを加算する.
	void add(int a, int b, LL x, int k = 0, int l = 0, int r = -1){
		if (r < 0)r = n;

		if (r <= a || b <= l) return; //もし交差しない区間であれば終える.

		if (a <= l && r <= b) { //もし今みている区間[l, r)が[a, b)に完全に内包されていれば
			segAdd[k] += x;  //区間[l, r)にkを加算する.
			return;
		}

		add(a, b, x, k * 2 + 1, l, (l + r) / 2); //子の区間に(必要があれば)xを加算する.
		add(a, b, x, k * 2 + 2, (l + r) / 2, r); //〃

		//親の区間の最小値は, 子の区間の最小値 + 自分に一様に加算されている値 である.一様に加算される値は更新しなくて良い.
		segMax[k] = max(segMax[k * 2 + 1] + segAdd[k * 2 + 1], segMax[k * 2 + 2] + segAdd[k * 2 + 2]);
	}

	LL getMax(int a, int b, int k = 0, int l = 0, int r = -1){
		if (r < 0)r = n;

		if (r <= a || b <= l) return (-INF<int>());

		if (a <= l && r <= b) return (segMax[k] + segAdd[k]); //完全に内包されていれば,その区間の最小値を返す.

		LL left = getMax(a, b, k * 2 + 1, l, (l + r) / 2); //子の区間の最小値を求める.
		LL right = getMax(a, b, k * 2 + 2, (l + r) / 2, r); //子の区間の最小値を求める

		return (max(left, right) + segAdd[k]); //親の区間の最小値は, 子の区間の最小値 + 自分に一様に加算されている値 である (大切なので2回書きました!!)

	}
};

int n;
vector<LL> t;
int m;
int l, r, d;

int main(){
    INIT;
    cin >> n;
    t.resize(n + 10);
    REP(i, n - 1){
        cin >> t[i];
        t[i] += (n - i - 1)*3;
    }
    t[n] += 3;
    SegmentTree seg(t);
    cin >> m;
    REP(i, m){
        cin >> l >> r >> d;
        l--, r--;
        seg.add(l, r + 1, d);
        cout << seg.getMax(0, n)<< endl;
    }
}
0