#include<cstdio>
#include<list>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define FOREACH(it,c) for(auto it=(c).begin();it!=(c).end();++it)
typedef long long LL;
using namespace std;
struct tri
{
LL p;
LL l;
};
struct cmp{
bool operator()(const pair<LL,list<tri>::iterator>& a,const pair<LL,list<tri>::iterator>& b)
{
if(a.ST!=b.ST)
return a.ST<b.ST;
else
return &*(a.ND)<&*(b.ND);
}
};
int main()
{
int i,n,m,w;
LL sut=0,p0,sul=0;
list<tri> t;
pair<int,int> d[200002];
LL wyn[200002];
set<pair<LL,list<tri>::iterator>,cmp > kol;
tri bt;
scanf("%d%d",&n,&m);
bt.l=1;
bt.p=0;
t.PB(bt);
p0=0;
for(i=1;i<=n;++i)
{
scanf("%lld",&bt.p);
kol.insert(MP(bt.p-p0,--t.end()));
// printf("kol ins %lld\n",bt.p-p0);
t.PB(bt);
// printf("t ins %lld %d\n",bt.p,bt.l);
p0=bt.p;
}
for(i=0;i<m;++i)
{
d[i].ND=i;
scanf("%d",&d[i].ST);
}
sort(d,d+m);
w=0;
while(w<m)
{
while(kol.begin()->ST<d[w].ST)
{
auto a=kol.begin()->ND;
auto b=a;
b++;
auto c=b;
c++;
sul+=a->l*b->l;
sut+=b->l*(b->p-a->p);
// printf("kol era %lld %lld %d\n",kol.begin()->ST,a->p,a->l);
kol.erase(kol.begin());
if(c!=t.end())
{
// printf("kol era %lld %lld %d\n",(c->p-b->p)/b->l,b->p,b->l);
kol.erase(MP((c->p-b->p)/b->l,b));
a->l+=b->l;
// printf("kol ins %lld %lld %d\n",(c->p-b->p)/a->l,a->p,a->l);
kol.insert(MP((c->p-a->p)/a->l,a));
}
else
{
a->l+=b->l;
}
// printf("t era %lld %d\n",b->p,b->l);
t.erase(b);
}
wyn[d[w].ND]=sul*d[w].ST-sut;
++w;
}
for(i=0;i<m;++i)
printf("%lld\n",wyn[i]);
}
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 | #include<cstdio> #include<list> #include<queue> #include<map> #include<set> #include<algorithm> #define PB push_back #define ST first #define ND second #define MP make_pair #define FOREACH(it,c) for(auto it=(c).begin();it!=(c).end();++it) typedef long long LL; using namespace std; struct tri { LL p; LL l; }; struct cmp{ bool operator()(const pair<LL,list<tri>::iterator>& a,const pair<LL,list<tri>::iterator>& b) { if(a.ST!=b.ST) return a.ST<b.ST; else return &*(a.ND)<&*(b.ND); } }; int main() { int i,n,m,w; LL sut=0,p0,sul=0; list<tri> t; pair<int,int> d[200002]; LL wyn[200002]; set<pair<LL,list<tri>::iterator>,cmp > kol; tri bt; scanf("%d%d",&n,&m); bt.l=1; bt.p=0; t.PB(bt); p0=0; for(i=1;i<=n;++i) { scanf("%lld",&bt.p); kol.insert(MP(bt.p-p0,--t.end())); // printf("kol ins %lld\n",bt.p-p0); t.PB(bt); // printf("t ins %lld %d\n",bt.p,bt.l); p0=bt.p; } for(i=0;i<m;++i) { d[i].ND=i; scanf("%d",&d[i].ST); } sort(d,d+m); w=0; while(w<m) { while(kol.begin()->ST<d[w].ST) { auto a=kol.begin()->ND; auto b=a; b++; auto c=b; c++; sul+=a->l*b->l; sut+=b->l*(b->p-a->p); // printf("kol era %lld %lld %d\n",kol.begin()->ST,a->p,a->l); kol.erase(kol.begin()); if(c!=t.end()) { // printf("kol era %lld %lld %d\n",(c->p-b->p)/b->l,b->p,b->l); kol.erase(MP((c->p-b->p)/b->l,b)); a->l+=b->l; // printf("kol ins %lld %lld %d\n",(c->p-b->p)/a->l,a->p,a->l); kol.insert(MP((c->p-a->p)/a->l,a)); } else { a->l+=b->l; } // printf("t era %lld %d\n",b->p,b->l); t.erase(b); } wyn[d[w].ND]=sul*d[w].ST-sut; ++w; } for(i=0;i<m;++i) printf("%lld\n",wyn[i]); } |
English