#include <bits/stdc++.h>
#define lld long long int
using namespace std;
struct node
{
node *right, *left;
lld co;
};
lld N,Q,a,b,c;
lld tab[300030];
node *root=new node();
node *End=new node();
lld rozw(lld wag,lld ile)
{
if(wag>=ile)return 0;
node *gdzie=new node(), *kopia=new node(), *akt=new node();
gdzie=root->right;
kopia->co=-1;
akt=kopia;
while(gdzie!=End)
{
node *pom=new node();
pom->co=gdzie->co;
pom->left=akt;
akt->right=pom;
akt=pom;
gdzie=gdzie->right;
}
node *pom=new node();
pom->co=1000000000000000000;
pom->left=akt;
akt->right=pom;
gdzie=kopia;
lld licz=0;
while(wag<ile)
{
while(gdzie->co<wag)
{
gdzie=gdzie->right;
}
gdzie=gdzie->left;
if(gdzie->co==-1)return -1;
if(gdzie->co==1000000000000000000)return -1;
if(gdzie->co>=wag)continue;
++licz;
wag+=gdzie->co;
gdzie->right->left=gdzie->left;
gdzie->left->right=gdzie->right;
gdzie=gdzie->right;
}
return licz;
}
void add(lld ile)
{
node *pom=root;
while(pom->co<ile)
{
pom=pom->right;
}
node *pom2=new node();
pom2->co=ile;
pom2->left=pom->left;
pom2->right=pom;
pom->left->right=pom2;
pom->left=pom2;
}
void del(lld ile)
{
node *pom=root;
while(pom->co<ile)
{
pom=pom->right;
}
pom->left->right=pom->right;
pom->right->left=pom->left;
}
int main()
{
root->co=-1;
node *akt=root;
scanf("%lld",&N);
for(lld i = 0;i<N;++i)
{
scanf("%lld",&tab[i]);;
}
sort(tab,tab+N);
for(lld i = 0;i<N;++i)
{
node *pom=new node();
pom->co=tab[i];
pom->left=akt;
akt->right=pom;
akt=pom;
}
End->co=1000000000000000000;
End->left=akt;
akt->right=End;
scanf("%lld",&Q);
for(lld i = 0;i<Q;++i)
{
scanf("%lld",&a);
if(a==1)
{
scanf("%lld%lld",&b,&c);
printf("%lld\n",rozw(b,c));
}
if(a==2)
{
scanf("%lld",&b);
add(b);
}
if(a==3)
{
scanf("%lld",&b);
del(b);
}
}
}
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 | #include <bits/stdc++.h> #define lld long long int using namespace std; struct node { node *right, *left; lld co; }; lld N,Q,a,b,c; lld tab[300030]; node *root=new node(); node *End=new node(); lld rozw(lld wag,lld ile) { if(wag>=ile)return 0; node *gdzie=new node(), *kopia=new node(), *akt=new node(); gdzie=root->right; kopia->co=-1; akt=kopia; while(gdzie!=End) { node *pom=new node(); pom->co=gdzie->co; pom->left=akt; akt->right=pom; akt=pom; gdzie=gdzie->right; } node *pom=new node(); pom->co=1000000000000000000; pom->left=akt; akt->right=pom; gdzie=kopia; lld licz=0; while(wag<ile) { while(gdzie->co<wag) { gdzie=gdzie->right; } gdzie=gdzie->left; if(gdzie->co==-1)return -1; if(gdzie->co==1000000000000000000)return -1; if(gdzie->co>=wag)continue; ++licz; wag+=gdzie->co; gdzie->right->left=gdzie->left; gdzie->left->right=gdzie->right; gdzie=gdzie->right; } return licz; } void add(lld ile) { node *pom=root; while(pom->co<ile) { pom=pom->right; } node *pom2=new node(); pom2->co=ile; pom2->left=pom->left; pom2->right=pom; pom->left->right=pom2; pom->left=pom2; } void del(lld ile) { node *pom=root; while(pom->co<ile) { pom=pom->right; } pom->left->right=pom->right; pom->right->left=pom->left; } int main() { root->co=-1; node *akt=root; scanf("%lld",&N); for(lld i = 0;i<N;++i) { scanf("%lld",&tab[i]);; } sort(tab,tab+N); for(lld i = 0;i<N;++i) { node *pom=new node(); pom->co=tab[i]; pom->left=akt; akt->right=pom; akt=pom; } End->co=1000000000000000000; End->left=akt; akt->right=End; scanf("%lld",&Q); for(lld i = 0;i<Q;++i) { scanf("%lld",&a); if(a==1) { scanf("%lld%lld",&b,&c); printf("%lld\n",rozw(b,c)); } if(a==2) { scanf("%lld",&b); add(b); } if(a==3) { scanf("%lld",&b); del(b); } } } |
English