提出 #42149608


ソースコード 拡げる

#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;
#define REP(i, m, n) for (int i = (int) (m); i < (int) (n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
#define pint pair<int, int>
#define pll pair<ll, ll>
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

using mint = modint998244353;


int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
//    rep(i, n)a[i] = 0;
    rep(i, n) {
        cin >> a[i];
    }
    vector<ll> sum(n + 1);
    rep(i, n) {
        if (i % 2 == 1) {
            sum[i + 1] = sum[i] + (a[i + 1] - a[i]);
        } else {
            sum[i + 1] = sum[i];
        }
    }
//    rep(i, n + 1)cout << sum[i] << endl;
    ll q;
    cin >> q;
    rep(i, q) {
        ll l, r;
        cin >> l >> r;
        ll below = std::lower_bound(a.begin(), a.end(), l) - a.begin();
        ll above = std::upper_bound(a.begin(), a.end(), r) - a.begin();
        if (above == n)above--;
        while (a[above] > r) {
            above--;
        }
        ll ans = sum[above] - sum[below];
//        cout << below << ' ' << above << endl;
//        cout << l << ' ' << r << endl;
        if (below % 2 == 0) {
            ans += (a[below] - l);
        }
        if(above % 2 == 1){
            ans += (r - a[above]);
        }
        cout << ans << endl;
    }


    return 0;
}

提出情報

提出日時
問題 D - Sleep Log
ユーザ hnakashima
言語 C++ (GCC 9.2.1)
得点 450
コード長 1446 Byte
結果 AC
実行時間 510 ms
メモリ 6388 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 2
AC × 20
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 02_max_09.txt, 02_max_10.txt, 02_max_11.txt, 02_max_12.txt, 02_max_13.txt, 02_max_14.txt, 02_max_15.txt, 03_edge_16.txt, 03_edge_17.txt, 03_edge_18.txt, 03_edge_19.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 3 ms 3560 KiB
00_sample_01.txt AC 3 ms 3488 KiB
01_random_02.txt AC 50 ms 5204 KiB
01_random_03.txt AC 168 ms 4356 KiB
01_random_04.txt AC 368 ms 5488 KiB
01_random_05.txt AC 86 ms 4440 KiB
01_random_06.txt AC 237 ms 5400 KiB
01_random_07.txt AC 418 ms 4092 KiB
01_random_08.txt AC 313 ms 5232 KiB
02_max_09.txt AC 509 ms 6324 KiB
02_max_10.txt AC 502 ms 6240 KiB
02_max_11.txt AC 508 ms 6332 KiB
02_max_12.txt AC 510 ms 6192 KiB
02_max_13.txt AC 508 ms 6288 KiB
02_max_14.txt AC 504 ms 6284 KiB
02_max_15.txt AC 504 ms 6388 KiB
03_edge_16.txt AC 400 ms 3504 KiB
03_edge_17.txt AC 399 ms 3496 KiB
03_edge_18.txt AC 438 ms 6240 KiB
03_edge_19.txt AC 437 ms 6200 KiB