Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8. Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <bits/stdc++.h>
#define booost ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define fi first
#define se second
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;


struct SegmentTree /// (+, sum)
{
    const static int MAX = 1 << 20;
    LL INF = LL(1e18) + 7;
    vector < LL > tree;
    SegmentTree()
    {
        tree.resize(2 * MAX, 0);
    }
    void update(int a, LL val)
    {
        a += MAX;
        tree[a]+=val;
        a/=2;
        while(a) tree[a] = tree[2*a]+tree[2*a+1], a /= 2;
    }
    LL query(int a, int b)
    {
        a += MAX, b += MAX;
        if(a==b) return tree[a];
        LL res = tree[a]+ tree[b];
        while(a / 2 != b / 2)
        {
            if(a % 2 == 0) res += tree[a + 1];
            if(b % 2 == 1) res +=  tree[b - 1];
            a /= 2, b /= 2;
        }
        return res;
    }
};


const LL MOD=1e9+7;
const LL LLINF=1e18+7;

SegmentTree tree,treecheck;

vector<LL> rybki;
map<LL,int> mpszpr;///tu gdzie wleci nast�pna --- -1, aby zna� ostatni� kt�ra jest
vector<pair<int,PLL>> queries;
deque<PII> segments;

multiset<LL> szprotki;

int binsearch(PII interval, LL goal)/// left idx
{
    //cout<<"bsearch : "<<interval.fi<<" "<<interval.second<<" "<<goal<<'\n';
    int l=interval.fi, r=interval.se;
    int ans=l;
    while(l<=r)
    {
        int mid=(l+r)/2;
        LL val=tree.query(mid,interval.se);
        //cout<<val<<'\n';
        if(val>=goal)
        {
            ans=mid;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    //cout<<"ans: "<<ans<<'\n';
    return ans;
}

int solve(LL s,LL goal)
{
    if(s>=goal) return 0;
    if( szprotki.empty()) return -1;
    segments.clear();

    auto it=szprotki.lower_bound(s);
    if( it==szprotki.begin()) return -1;
    LL greatest=*(--it);
    LL prev=0;
    segments.push_back({0,mpszpr[greatest]-1});

    int res=0;
    LL curval=s;
    while(curval<goal)
    {
        if(segments.empty()) return -1;
        auto itt=szprotki.upper_bound(greatest);
        LL mingoal=itt==szprotki.end()?goal-1:*itt;
        int left=binsearch(segments.back(),min(mingoal-curval+1,goal-curval));

        //cout<<"res,curval,goal: "<<res<<" "<<curval<<" "<<goal<<'\n';

        res+=treecheck.query(left,segments.back().se);
        curval+=tree.query(left,segments.back().se);

        prev=greatest;
        greatest=*(--szprotki.lower_bound(curval));

        if(curval>=goal)    return res;

        if(left==segments.back().fi)///nastepny
        {
            segments.pop_back();
            if(greatest==prev)  continue;
        }
        else
        {
            int lleft=segments.back().fi;
            segments.pop_back();
            segments.push_back({lleft,left-1});
        }

        if(greatest==prev)
        {
            //cout<<"lel\n";
            return -1;
        }
        else
        {
            segments.push_back({mpszpr[prev],mpszpr[greatest]-1});
        }
    }

    return res;
}
int main()
{
    //booost;
    //freopen("C:/Users/kucha/Documents/ISIM 5. semestr/PA/tests/MS5.in","r",stdin);
    int n,q;
    LL w;
    //cin>>n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        //cin>>w;
        scanf("%lld",&w);
        rybki.push_back(w);
        queries.push_back({2,{w,w}});
    }
    //cin>>q;
    scanf("%d",&q);
    int id;
    LL f,s;
    for(int i=0;i<q;i++)
    {
        //cin>>id;
        scanf("%d",&id);
        if(id==1)
        {
            //cin>>f>>s;
            scanf("%lld %lld",&f,&s);
            queries.push_back({id,{f,s}});
        }
        else
        {
            //cin>>f;
            scanf("%lld",&f);
            queries.push_back({id,{f,f}});
            if(id==2)
            {
                rybki.push_back(f);
            }
        }
    }
    sort(rybki.begin(),rybki.end());
    for(int i=0;i<rybki.size();i++)
    {
        if(mpszpr[rybki[i]]==0)
        {
            mpszpr[rybki[i]]=i;
        }
    }
    mpszpr[rybki[0]]=0;

    for(auto query:queries)
    {
        if(query.fi==1)
        {
            int ans=solve(query.se.fi,query.se.se);
            printf("%d\n",ans);
        }
        else if(query.fi==2)
        {
            tree.update(mpszpr[query.se.fi],query.se.fi);
            treecheck.update(mpszpr[query.se.fi],1);
            mpszpr[query.se.fi]++;
            szprotki.insert(query.se.fi);
        }
        else
        {
            mpszpr[query.se.fi]--;
            tree.update(mpszpr[query.se.fi],-query.se.fi);
            treecheck.update(mpszpr[query.se.fi],-1);
            szprotki.erase(szprotki.find(query.se.fi));
        }
    }

}