Submission #881794

# Submission time Handle Problem Language Result Execution time Memory
881794 2023-12-02T00:30:42 Z cpptowin Growing Trees (BOI11_grow) C++17
100 / 100
144 ms 10556 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf 1000000000
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1 << j))
#define onbit(i, j) (i | (1 << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
int n, q;
int a[maxn];
int st[maxn], lazy[maxn];
void down(int id)
{
    st[id << 1] += lazy[id];
    st[id << 1 | 1] += lazy[id];
    lazy[id << 1] += lazy[id];
    lazy[id << 1 | 1] += lazy[id];
    lazy[id] = 0;
}
void up(int id, int l, int r, int u, int v, int val)
{
    if (l > v or r < u)
        return;
    if (u <= l and r <= v)
    {
        st[id] += val;
        lazy[id] += val;
        return;
    }
    down(id);
    int mid = (l + r) >> 1;
    up(id << 1, l, mid, u, v, val);
    up(id << 1 | 1, mid + 1, r, u, v, val);
    st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
    if (l > v or r < u)
        return 0;
    if (u <= l and r <= v)
        return st[id];
    down(id);
    int mid = (l + r) >> 1;
    return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}

int get(int idx, int l, int r, int val) {
	if(st[idx] < val) return -1;
	if(l == r) return l;
	down(idx);
 
	int mid = l + r >> 1;
	if(st[idx << 1] >= val) return get(idx << 1, l, mid, val);
	return get(idx << 1 | 1, mid + 1, r, val);
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q;
    fo(i, 1, n) cin >> a[i];
    sort(a + 1, a + n + 1);
    fo(i, 1, n) up(1, 1, n, i, i, a[i]);
    while (q--)
    {
        char t;
        int l, r;
        cin >> t >> r >> l;
        if (t == 'F')
        {
            int lb = get(1, 1, n, l);
            if(lb == -1) continue;
            int maxx = get(1, 1, n, lb, lb + r - 1);
            int ub = get(1, 1, n, maxx);
            if(ub == -1) ub = n + 1;
            ub--;
            int ub1 = get(1, 1, n, maxx + 1);
            if(ub1 == -1) ub1 = n + 1;
            ub1--;
            if (ub >= lb)
                up(1, 1, n, lb, ub, 1);
            int len;
            if (ub < lb)
                len = r;
            else
                len = r - (ub - lb + 1);
            up(1, 1, n, max(ub + 1,ub1 - len + 1), ub1, 1);
            // cout << lb << ' ' << ub << ' ' << ub1 << "\n";
            // fo(i,1,n) cout << get(1,1,n,i,i) << ' ';en;
        }
        else
        {
            swap(l,r);
            int lb = get(1, 1, n, l);
            int ub = get(1, 1, n, r + 1);
            if (ub == -1)
                ub = n + 1;
            if (lb != -1)
                cout << ub - lb << "\n";
            else
                cout << 0 << "\n";
        }
    }
}

Compilation message

grow.cpp: In function 'long long int get(long long int, long long int, long long int, long long int)':
grow.cpp:83:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   83 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: At global scope:
grow.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   87 | main()
      | ^~~~
grow.cpp: In function 'int main()':
grow.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 66 ms 8856 KB Output is correct
2 Correct 109 ms 10500 KB Output is correct
3 Correct 83 ms 10320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4732 KB Output is correct
5 Correct 33 ms 5608 KB Output is correct
6 Correct 38 ms 5888 KB Output is correct
7 Correct 5 ms 4696 KB Output is correct
8 Correct 19 ms 5400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 5188 KB Output is correct
2 Correct 39 ms 5980 KB Output is correct
3 Correct 3 ms 4696 KB Output is correct
4 Correct 24 ms 5464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 4956 KB Output is correct
2 Correct 41 ms 5992 KB Output is correct
3 Correct 9 ms 4572 KB Output is correct
4 Correct 39 ms 6072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 8792 KB Output is correct
2 Correct 96 ms 10064 KB Output is correct
3 Correct 14 ms 7000 KB Output is correct
4 Correct 64 ms 10096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 8876 KB Output is correct
2 Correct 104 ms 10320 KB Output is correct
3 Correct 75 ms 10472 KB Output is correct
4 Correct 14 ms 7168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 9044 KB Output is correct
2 Correct 67 ms 10184 KB Output is correct
3 Correct 78 ms 10332 KB Output is correct
4 Correct 13 ms 7004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 8896 KB Output is correct
2 Correct 97 ms 10180 KB Output is correct
3 Correct 38 ms 9544 KB Output is correct
4 Correct 65 ms 10260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 9044 KB Output is correct
2 Correct 97 ms 10320 KB Output is correct
3 Correct 144 ms 10556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 9296 KB Output is correct