Submission #100960

# Submission time Handle Problem Language Result Execution time Memory
100960 2019-03-15T12:08:40 Z shafinalam Growing Trees (BOI11_grow) C++14
100 / 100
812 ms 4660 KB
#include <bits/stdc++.h>

using namespace std;

const int mx = 1e5+5;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<int,pii>piii;

#define  sf scanf
#define  pf printf

#define  input freopen("input.txt","r",stdin)
#define  output freopen("output.txt","w",stdout)

#define  inf 1e16
#define  ff first
#define  ss second
#define  MP make_pair
#define  pb push_back
#define  all(v) v.begin(), v.end()
#define  printcase(cases) printf("Case %d:", cases);
#define  Unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define  FAST  ios_base::sync_with_stdio(0);cout.tie(0)
#define  endl printf("\n")
#define  __lcm(a, b) ((a*b)/__gcd(a, b))


int tree[mx<<2];
int lazy[mx<<2];
int arr[mx];
int n, m;

void push(int node, int b,int e)
{
    if(lazy[node])
    {
        tree[node]+=((e-b+1)*lazy[node]);
        if(b!=e)
        {
            int left = node<<1;
            int right = left|1;
            lazy[left]+=lazy[node];
            lazy[right]+=lazy[node];
        }
        lazy[node] = 0;
    }
    return;
}
void update(int node, int b, int e, int l, int r)
{
    push(node, b, e);
    if(b>r || e<l) return;
    if(b>=l && e<=r)
    {
        lazy[node]++;
        push(node, b, e);
        return;
    }
    int mid = (b+e)>>1;
    int left = node<<1;
    int right = left|1;
    update(left, b, mid, l, r);
    update(right, mid+1, e, l, r);
    tree[node] = tree[left]+tree[right];
}
int query(int node, int b, int e, int pos)
{
    push(node, b, e);
    if(b>pos || e<pos)
        return 0;
    if(b==e && pos==b)
        return tree[node];
    int mid = (b+e)>>1;
    int left = node<<1;
    int right = left|1;
    if(pos<=mid) return query(left, b, mid, pos);
    return query(right, mid+1, e, pos);
}
int get(int pos)
{
    return query(1, 1, n, pos);
}
int main()
{
    sf("%d%d", &n, &m);

    for(int i = 1; i <= n; i++) sf("%d", &arr[i]);
    sort(arr+1, arr+n+1);

    while(m--)
    {
        char type;
        cin >> type;

        if(type=='F')
        {
            int h, c;
            sf("%d%d", &c, &h);

            int lo = 1, hi = n, pos = n+1;
            while(lo<=hi)
            {
                int mid = (lo+hi)>>1;
                if(arr[mid]+get(mid)>=h)
                {
                    pos = mid;
                    hi = mid-1;
                }
                else
                    lo = mid+1;
            }
            if(pos==n+1) continue;
            int last = pos+c-1;
            last = min(last, n);
            if(last==n || (arr[last]+get(last)) < (arr[last+1]<get(last+1))) update(1, 1, n, pos, last);
            else
            {
                int l = 0, r = n+1, val = arr[last]+get(last);
                lo = 1, hi = n;
                while(lo<=hi)
                {
                    int mid = (lo+hi)>>1;
                    if(arr[mid]+get(mid)<val)
                    {
                        lo = mid+1;
                        l = mid;
                    }
                    else hi = mid-1;
                }
                lo = 1, hi = n;
                while(lo<=hi)
                {
                    int mid = (lo+hi)>>1;
                    if(arr[mid]+get(mid)>val)
                    {
                        r = mid;
                        hi = mid-1;
                    }
                    else lo = mid+1;
                }
                l++;
                r--;
                int len = last-l+1;
                update(1, 1, n, pos, l-1);
                update(1, 1, n, r-len+1, r);

            }
            //for(int i = 1; i <= n; i++) pf("%lld ", arr[i]+get(i));
            //cout << '\n';
        }
        else
        {
            int a, b;
            sf("%d%d", &a, &b);

            int l = n+1, r = 0, lo = 1, hi = n;

            while(lo<=hi)
            {
                int mid = (lo+hi)>>1;
                if(arr[mid]+get(mid)>=a)
                {
                    l = mid;
                    hi = mid-1;
                }
                else lo = mid+1;
            }
            lo = 1, hi = n;
            while(lo<=hi)
            {
                int mid = (lo+hi)>>1;
                if(arr[mid]+get(mid)<=b)
                {
                    r = mid;
                    lo = mid+1;
                }
                else hi = mid-1;
            }
            if(l>r)
            {
                pf("0\n");
                continue;
            }
            pf("%d\n", r-l+1);
        }
    }
    return 0;
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     sf("%d%d", &n, &m);
       ^
grow.cpp:90:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= n; i++) sf("%d", &arr[i]);
                                   ^
grow.cpp:101:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             sf("%d%d", &c, &h);
               ^
grow.cpp:157:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             sf("%d%d", &a, &b);
               ^
# Verdict Execution time Memory Grader output
1 Correct 477 ms 2968 KB Output is correct
2 Correct 663 ms 4608 KB Output is correct
3 Correct 202 ms 4404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 384 KB Output is correct
2 Correct 13 ms 384 KB Output is correct
3 Correct 17 ms 512 KB Output is correct
4 Correct 14 ms 384 KB Output is correct
5 Correct 337 ms 768 KB Output is correct
6 Correct 440 ms 924 KB Output is correct
7 Correct 28 ms 512 KB Output is correct
8 Correct 328 ms 800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 335 ms 848 KB Output is correct
2 Correct 406 ms 892 KB Output is correct
3 Correct 8 ms 512 KB Output is correct
4 Correct 313 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 916 KB Output is correct
2 Correct 484 ms 976 KB Output is correct
3 Correct 50 ms 512 KB Output is correct
4 Correct 412 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 1936 KB Output is correct
2 Correct 567 ms 4216 KB Output is correct
3 Correct 88 ms 1400 KB Output is correct
4 Correct 159 ms 4144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 546 ms 2948 KB Output is correct
2 Correct 651 ms 2976 KB Output is correct
3 Correct 200 ms 4516 KB Output is correct
4 Correct 94 ms 1444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 444 ms 3036 KB Output is correct
2 Correct 476 ms 4156 KB Output is correct
3 Correct 241 ms 4472 KB Output is correct
4 Correct 122 ms 1456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 719 ms 2936 KB Output is correct
2 Correct 719 ms 3148 KB Output is correct
3 Correct 131 ms 3576 KB Output is correct
4 Correct 446 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 643 ms 3068 KB Output is correct
2 Correct 752 ms 4636 KB Output is correct
3 Correct 812 ms 4660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 756 ms 3400 KB Output is correct