Submission #915422

# Submission time Handle Problem Language Result Execution time Memory
915422 2024-01-23T22:14:26 Z n3rm1n Divide and conquer (IZhO14_divide) C++17
17 / 100
2 ms 2396 KB
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 1e5+10;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int n;
int x[MAXN], gold[MAXN], energy[MAXN];
long long a[MAXN], pref[MAXN];
void read()
{
    cin >> n;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> x[i] >> gold[i] >> energy[i];
        a[i] = a[i-1];
        pref[i] = pref[i-1] + gold[i];
        if(i > 1)a[i] -= x[i] - x[i-1];
        a[i] += energy[i];
    }
}
int t[MAXN * 4];
int lazy[MAXN * 4];
int inf = -1;
void make_tree(int i, int l, int r)
{
    if(l == r)
    {
        t[i] = a[l];
      //  cout << l << " " << r << " " << a[l] << endl;
        return;
    }
    int mid = (l + r)/2;
    make_tree(2*i, l, mid);
    make_tree(2*i+1, mid+1, r);
    t[i] = max(t[2*i], t[2*i+1]);
}
void push_lazy(int i, int l, int r)
{
    t[i] += lazy[i];
    if(l != r)
    {
        lazy[2*i] += lazy[i];
        lazy[2*i+1] += lazy[i];
    }
    lazy[i] = 0;
}
int ql, qr, val;
void update(int i, int l, int r)
{
    push_lazy(i, l, r);
    if(qr < l || ql > r)return;
    if(ql <= l && r <= qr)
    {
        lazy[i] = val;
        push_lazy(i, l, r);
        return;
    }
    int mid = (l + r)/2;
    update(2*i, l, mid);
    update(2*i+1, mid+1, r);
    t[i] = max(t[2*i], t[2*i+1]);
}
int xx;
void clear_update(int i, int l, int r)
{
    if(l == r)
    {
        t[i] = -1;
        return;
    }
    int mid = (l + r)/2;
    if(xx <= mid)clear_update(2*i, l, mid);
    else clear_update(2*i+1, mid+1, r);
    t[i] = max(t[2*i], t[2*i+1]);
}

int query(int i, int l, int r)
{
    push_lazy(i, l, r);
    if(l == r)
    {
        if(t[i] >= 0)return l;
        else return -1;
    }
    int mid = (l + r)/2;
    push_lazy(2*i, l, mid);
    push_lazy(2*i+1, mid + 1, r);
    int tleft = t[2*i], tright = t[2*i+1];
    if(tright >= 0)return query(2*i+1, mid+1, r);
    else return query(2*i, l, mid);
}
int main()
{
    speed();

    read();
    make_tree(1, 1, n);

    long long ans = 0;
    for (int starter = 1; starter <= n; ++ starter)
    {

        long long index = query(1, 1, n);
       // cout << index << endl;
        if(index == -1)continue;
        long long sol = pref[index] - pref[starter-1];
        ans = max(ans, sol);
        long long change =  -energy[starter];
        if(starter < n)change += x[starter+1] - x[starter];
        ql = starter + 1;
        qr = n;
        val = change;
        if(ql <= qr)
            update(1, 1, n);
        xx = starter;
        clear_update(1, 1, n);
    }
    cout << ans << endl;
    return 0;
}

Compilation message

divide.cpp: In function 'int query(int, int, int)':
divide.cpp:93:9: warning: unused variable 'tleft' [-Wunused-variable]
   93 |     int tleft = t[2*i], tright = t[2*i+1];
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Incorrect 2 ms 2396 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Incorrect 2 ms 2396 KB Output isn't correct
19 Halted 0 ms 0 KB -