답안 #699780

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699780 2023-02-18T02:24:27 Z Ran Art Exhibition (JOI18_art) C++17
100 / 100
309 ms 43468 KB
#include <bits/stdc++.h>

#define fi first
#define se second

using namespace std;

const long long N = 5e5 + 100;

int n;

pair <long long, long long> art[N];

long long f[N];

struct segTree
{
    long long st[4 * N], lazy[4 * N];
    segTree()
    {
        for (int i = 0; i < 4 * N; i++)
        {
            st[i] = -1e18;
            lazy[i] = 0;
        }
    }
    void build(int id, int l, int r)
    {
        if (l > r)
            return;
        if (l == r)
        {
            st[id] = f[l] - art[l].fi;
            return;
        }
        int mid = l + r >> 1;
        build(id * 2, l, mid);
        build(id * 2 + 1, mid + 1, r);
        st[id] = max(st[id * 2], st[id * 2 + 1]);
    }
    void down(int id)
    {
        st[id * 2] += lazy[id];
        st[id * 2 + 1] += lazy[id];
        lazy[id * 2] += lazy[id];
        lazy[id * 2 + 1] += lazy[id];
        lazy[id] = 0;
    }
    void update(int id, int l, int r, int u, int v, long long val)
    {
        if (v < l || r < u)
            return;
        if (u <= l && r <= v)
        {
            lazy[id] += val;
            st[id] += val;
            return;
        }
        int mid = l + r >> 1;
        down(id);
        update(id * 2, l, mid, u, v, val);
        update(id * 2 + 1, mid + 1, r, u, v, val);
        st[id] = max(st[id * 2], st[id * 2 + 1]);
    }
    long long get(int id, int l, int r, int u, int v)
    {
        if (v < l || r < u)
            return -1e18;
        if (u <= l && r <= v)
            return st[id];
        int mid = l + r >> 1;
        down(id);
        return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
    }
} tree;

void sub3()
{
    long long ans = -1e18;
    for (int i = 1; i <= n; i++)
    {
        long long sum = 0;
        for (int j = i; j <= n; j++)
        {
            sum = sum + art[j].se;
            ans = max(ans, sum - (art[j].fi - art[i].fi));
        }
    }
    cout << ans;
}

void sub()
{
    long long ans = -1e18, sum = 0;
    for (int i = 1; i <= n; i++)
        f[i] = f[i - 1] + art[i].se;
    tree.build(1, 1, n);
    for (int i = 1; i <= n; i++)
    {
        long long tmp = tree.get(1, 1, n, i, n);
        ans = max(ans, tmp + art[i].fi);
        tree.update(1, 1, n, i, n, -art[i].se);
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> art[i].fi >> art[i].se;
    sort(art + 1, art + 1 + n);
    if (n <= 5000)
        sub3();
    else
        sub();
    return 0;
}

Compilation message

art.cpp: In member function 'void segTree::build(int, int, int)':
art.cpp:36:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |         int mid = l + r >> 1;
      |                   ~~^~~
art.cpp: In member function 'void segTree::update(int, int, int, int, int, long long int)':
art.cpp:59:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |         int mid = l + r >> 1;
      |                   ~~^~~
art.cpp: In member function 'long long int segTree::get(int, int, int, int, int)':
art.cpp:71:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   71 |         int mid = l + r >> 1;
      |                   ~~^~~
art.cpp: In function 'void sub()':
art.cpp:94:28: warning: unused variable 'sum' [-Wunused-variable]
   94 |     long long ans = -1e18, sum = 0;
      |                            ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31572 KB Output is correct
2 Correct 13 ms 31572 KB Output is correct
3 Correct 12 ms 31572 KB Output is correct
4 Correct 12 ms 31556 KB Output is correct
5 Correct 12 ms 31628 KB Output is correct
6 Correct 13 ms 31568 KB Output is correct
7 Correct 12 ms 31572 KB Output is correct
8 Correct 13 ms 31580 KB Output is correct
9 Correct 13 ms 31572 KB Output is correct
10 Correct 13 ms 31572 KB Output is correct
11 Correct 13 ms 31560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31572 KB Output is correct
2 Correct 13 ms 31572 KB Output is correct
3 Correct 12 ms 31572 KB Output is correct
4 Correct 12 ms 31556 KB Output is correct
5 Correct 12 ms 31628 KB Output is correct
6 Correct 13 ms 31568 KB Output is correct
7 Correct 12 ms 31572 KB Output is correct
8 Correct 13 ms 31580 KB Output is correct
9 Correct 13 ms 31572 KB Output is correct
10 Correct 13 ms 31572 KB Output is correct
11 Correct 13 ms 31560 KB Output is correct
12 Correct 12 ms 31572 KB Output is correct
13 Correct 13 ms 31568 KB Output is correct
14 Correct 13 ms 31572 KB Output is correct
15 Correct 13 ms 31652 KB Output is correct
16 Correct 13 ms 31636 KB Output is correct
17 Correct 13 ms 31572 KB Output is correct
18 Correct 12 ms 31572 KB Output is correct
19 Correct 13 ms 31536 KB Output is correct
20 Correct 13 ms 31572 KB Output is correct
21 Correct 13 ms 31560 KB Output is correct
22 Correct 13 ms 31644 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 13 ms 31536 KB Output is correct
25 Correct 14 ms 31572 KB Output is correct
26 Correct 13 ms 31572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31572 KB Output is correct
2 Correct 13 ms 31572 KB Output is correct
3 Correct 12 ms 31572 KB Output is correct
4 Correct 12 ms 31556 KB Output is correct
5 Correct 12 ms 31628 KB Output is correct
6 Correct 13 ms 31568 KB Output is correct
7 Correct 12 ms 31572 KB Output is correct
8 Correct 13 ms 31580 KB Output is correct
9 Correct 13 ms 31572 KB Output is correct
10 Correct 13 ms 31572 KB Output is correct
11 Correct 13 ms 31560 KB Output is correct
12 Correct 12 ms 31572 KB Output is correct
13 Correct 13 ms 31568 KB Output is correct
14 Correct 13 ms 31572 KB Output is correct
15 Correct 13 ms 31652 KB Output is correct
16 Correct 13 ms 31636 KB Output is correct
17 Correct 13 ms 31572 KB Output is correct
18 Correct 12 ms 31572 KB Output is correct
19 Correct 13 ms 31536 KB Output is correct
20 Correct 13 ms 31572 KB Output is correct
21 Correct 13 ms 31560 KB Output is correct
22 Correct 13 ms 31644 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 13 ms 31536 KB Output is correct
25 Correct 14 ms 31572 KB Output is correct
26 Correct 13 ms 31572 KB Output is correct
27 Correct 34 ms 31716 KB Output is correct
28 Correct 29 ms 31736 KB Output is correct
29 Correct 27 ms 31736 KB Output is correct
30 Correct 27 ms 31736 KB Output is correct
31 Correct 26 ms 31700 KB Output is correct
32 Correct 27 ms 31732 KB Output is correct
33 Correct 29 ms 31676 KB Output is correct
34 Correct 27 ms 31616 KB Output is correct
35 Correct 28 ms 31740 KB Output is correct
36 Correct 27 ms 31700 KB Output is correct
37 Correct 26 ms 31700 KB Output is correct
38 Correct 26 ms 31632 KB Output is correct
39 Correct 27 ms 31740 KB Output is correct
40 Correct 27 ms 31708 KB Output is correct
41 Correct 27 ms 31700 KB Output is correct
42 Correct 27 ms 31700 KB Output is correct
43 Correct 27 ms 31700 KB Output is correct
44 Correct 26 ms 31740 KB Output is correct
45 Correct 27 ms 31708 KB Output is correct
46 Correct 27 ms 31740 KB Output is correct
47 Correct 27 ms 31700 KB Output is correct
48 Correct 28 ms 31736 KB Output is correct
49 Correct 27 ms 31696 KB Output is correct
50 Correct 27 ms 31696 KB Output is correct
51 Correct 27 ms 31668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31572 KB Output is correct
2 Correct 13 ms 31572 KB Output is correct
3 Correct 12 ms 31572 KB Output is correct
4 Correct 12 ms 31556 KB Output is correct
5 Correct 12 ms 31628 KB Output is correct
6 Correct 13 ms 31568 KB Output is correct
7 Correct 12 ms 31572 KB Output is correct
8 Correct 13 ms 31580 KB Output is correct
9 Correct 13 ms 31572 KB Output is correct
10 Correct 13 ms 31572 KB Output is correct
11 Correct 13 ms 31560 KB Output is correct
12 Correct 12 ms 31572 KB Output is correct
13 Correct 13 ms 31568 KB Output is correct
14 Correct 13 ms 31572 KB Output is correct
15 Correct 13 ms 31652 KB Output is correct
16 Correct 13 ms 31636 KB Output is correct
17 Correct 13 ms 31572 KB Output is correct
18 Correct 12 ms 31572 KB Output is correct
19 Correct 13 ms 31536 KB Output is correct
20 Correct 13 ms 31572 KB Output is correct
21 Correct 13 ms 31560 KB Output is correct
22 Correct 13 ms 31644 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 13 ms 31536 KB Output is correct
25 Correct 14 ms 31572 KB Output is correct
26 Correct 13 ms 31572 KB Output is correct
27 Correct 34 ms 31716 KB Output is correct
28 Correct 29 ms 31736 KB Output is correct
29 Correct 27 ms 31736 KB Output is correct
30 Correct 27 ms 31736 KB Output is correct
31 Correct 26 ms 31700 KB Output is correct
32 Correct 27 ms 31732 KB Output is correct
33 Correct 29 ms 31676 KB Output is correct
34 Correct 27 ms 31616 KB Output is correct
35 Correct 28 ms 31740 KB Output is correct
36 Correct 27 ms 31700 KB Output is correct
37 Correct 26 ms 31700 KB Output is correct
38 Correct 26 ms 31632 KB Output is correct
39 Correct 27 ms 31740 KB Output is correct
40 Correct 27 ms 31708 KB Output is correct
41 Correct 27 ms 31700 KB Output is correct
42 Correct 27 ms 31700 KB Output is correct
43 Correct 27 ms 31700 KB Output is correct
44 Correct 26 ms 31740 KB Output is correct
45 Correct 27 ms 31708 KB Output is correct
46 Correct 27 ms 31740 KB Output is correct
47 Correct 27 ms 31700 KB Output is correct
48 Correct 28 ms 31736 KB Output is correct
49 Correct 27 ms 31696 KB Output is correct
50 Correct 27 ms 31696 KB Output is correct
51 Correct 27 ms 31668 KB Output is correct
52 Correct 296 ms 43340 KB Output is correct
53 Correct 307 ms 43468 KB Output is correct
54 Correct 291 ms 43336 KB Output is correct
55 Correct 300 ms 43360 KB Output is correct
56 Correct 306 ms 43300 KB Output is correct
57 Correct 297 ms 43296 KB Output is correct
58 Correct 302 ms 43364 KB Output is correct
59 Correct 294 ms 43364 KB Output is correct
60 Correct 309 ms 43412 KB Output is correct
61 Correct 293 ms 43296 KB Output is correct
62 Correct 289 ms 43360 KB Output is correct
63 Correct 297 ms 43400 KB Output is correct
64 Correct 294 ms 43340 KB Output is correct
65 Correct 299 ms 43384 KB Output is correct
66 Correct 290 ms 43360 KB Output is correct
67 Correct 292 ms 43340 KB Output is correct
68 Correct 296 ms 43364 KB Output is correct