Submission #226577

# Submission time Handle Problem Language Result Execution time Memory
226577 2020-04-24T11:42:24 Z MKopchev Growing Trees (BOI11_grow) C++14
100 / 100
149 ms 3832 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;
int n,q;

int inp[nmax];
long long pref[nmax];

void update(int pos,int val)
{
    //cout<<"update "<<pos<<" "<<val<<endl;
    while(pos<=n)
    {
        pref[pos]+=val;
        pos=pos+(pos&(-pos));
    }
}
void update_interval(int l,int r,int val)
{
    //cout<<"update_interval "<<l<<" "<<r<<" "<<val<<endl;

    update(l,val);
    update(r+1,-val);
}

int query(int pos)
{
    //cout<<"query "<<pos<<endl;
    long long ret=0;
    while(pos)
    {
        ret=ret+pref[pos];
        pos=pos-(pos&(-pos));
    }
    return ret;
}
int where_begins(int val)
{
    int not_ok=0,ok=n+1;
    while(ok-not_ok>1)
    {
        int av=(ok+not_ok)/2;

        if(query(av)>val)not_ok=av;
        else ok=av;
    }

    //cout<<"where_begins "<<val<<" -> "<<ok<<endl;

    return ok;
}
int main()
{
    scanf("%i%i",&n,&q);

    for(int i=1;i<=n;i++)scanf("%i",&inp[i]);

    sort(inp+1,inp+n+1);

    reverse(inp+1,inp+n+1);

    for(int i=1;i<=n;i++)
        update_interval(i,i,inp[i]);

    for(int i=1;i<=q;i++)
    {
        char c=getchar();
        while(c!='F'&&c!='C')c=getchar();

        if(c=='C')
        {
            int mini,maxi;
            scanf("%i%i",&mini,&maxi);
            printf("%i\n",where_begins(mini-1)-where_begins(maxi));
        }
        else
        {
            int height,cnt;
            scanf("%i%i",&cnt,&height);

            int pos=where_begins(height-1);
            pos--;

            if(pos<=cnt)
            {
                update_interval(1,pos,1);
                continue;
            }

            int max_applied=query(pos-cnt+1);

            int start=where_begins(max_applied);

            int pos_other=where_begins(max_applied-1);

            //cout<<pos<<" "<<start<<" "<<max_applied<<" "<<pos_other<<endl;

            update_interval(pos_other,pos,1);

            cnt=cnt-(pos-pos_other+1);

            update_interval(start,start+cnt-1,1);
        }
        /*
        for(int j=1;j<=n;j++)
            cout<<query(j)<<" ";cout<<endl;
        */
    }
    return 0;
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i%i",&n,&q);
     ~~~~~^~~~~~~~~~~~~~
grow.cpp:56:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;i++)scanf("%i",&inp[i]);
                          ~~~~~^~~~~~~~~~~~~~
grow.cpp:73:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%i%i",&mini,&maxi);
             ~~~~~^~~~~~~~~~~~~~~~~~~~
grow.cpp:79:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%i%i",&cnt,&height);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 97 ms 2680 KB Output is correct
2 Correct 135 ms 3192 KB Output is correct
3 Correct 71 ms 2976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 7 ms 384 KB Output is correct
3 Correct 8 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 63 ms 1528 KB Output is correct
6 Correct 76 ms 1784 KB Output is correct
7 Correct 9 ms 512 KB Output is correct
8 Correct 51 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 1656 KB Output is correct
2 Correct 76 ms 1912 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 64 ms 1436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 1912 KB Output is correct
2 Correct 81 ms 1784 KB Output is correct
3 Correct 12 ms 512 KB Output is correct
4 Correct 81 ms 1784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 2296 KB Output is correct
2 Correct 118 ms 2552 KB Output is correct
3 Correct 26 ms 1064 KB Output is correct
4 Correct 63 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 2296 KB Output is correct
2 Correct 119 ms 2680 KB Output is correct
3 Correct 71 ms 2808 KB Output is correct
4 Correct 28 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 2424 KB Output is correct
2 Correct 95 ms 2680 KB Output is correct
3 Correct 76 ms 3064 KB Output is correct
4 Correct 25 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 3064 KB Output is correct
2 Correct 121 ms 2680 KB Output is correct
3 Correct 43 ms 2284 KB Output is correct
4 Correct 116 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 2936 KB Output is correct
2 Correct 134 ms 3028 KB Output is correct
3 Correct 149 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 3832 KB Output is correct