_Kyrie_ @ 2021-12-24 21:57:47
#include <bits/stdc++.h>
#define re register
#define pb push_back
#define ll long long
#define int long long
using namespace std;
inline void IN(int &x) {
int s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
x = s * w;
}
#define lep(i, l, r) for(re int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(re int i = (l); i >= (r); --i)
const int N = 2e5 + 100;
int n, m, x, top, tag1[N], tag[N], an[N], siz[N], c[N][2], a[N], fa[N], zh[N], rt, ans[N];
const int inf = 0x7fffffff;
struct plpl {
int x, w, dj;
};
vector<plpl>lk[N];
struct node {
int c, w;
} q[N];
bool cmp(node a, node b) {
return (a.w == b.w ? a.c<b.c : a.w>b.w) ;
}
void up(int x) {
siz[x] = siz[c[x][0]] + siz[c[x][1]] + 1;
}
void down(int x) {
if (tag[x]) {
tag[c[x][0]] += tag[x];
a[c[x][0]] += tag[x];
tag[c[x][1]] += tag[x];
a[c[x][1]] += tag[x];
tag[x] = 0;
}
if (tag1[x]) {
an[c[x][0]] += tag1[x];
tag1[c[x][0]] += tag1[x];
an[c[x][1]] += tag1[x];
tag1[c[x][1]] += tag1[x];
tag1[x] = 0;
}
}
bool Get(int x) {
return c[fa[x]][1] == x;
}
void Con(int x, int y, int z) {
if (y) c[y][z] = x;
if (x) fa[x] = y;
}
void xz(int x) {
int y = fa[x], z = fa[y], m = Get(x), n = Get(y);
Con(c[x][m ^ 1], y, m), Con(y, x, m ^ 1), Con(x, z, n);
up(y);
up(x);
}
void splay(int x, int goal) {
int y = x, z = 0;
if (!x)
return ;
zh[++z]=y;
while(fa[y]) zh[++z]=fa[y],y=fa[y];
while(z) down(zh[z--]);
//cout<<"ok "<<x<<endl;
down(y);
while (fa[x] != goal) {
y = fa[x], z = fa[y];
if (z != goal)
((c[y][1] == x) ^ (c[z][1] == y) ? xz(x) : xz(y));
xz(x);
}
if (goal == 0)
rt = x;
}
void ins(int x, int k, int w) {
//cout<<"start\n";
int y = rt, pre = 0;
//cout<<"klkl "<<endl;
while (y && a[y] != x)
//{ cout<<"ppp "<<y<<" "<<a[y]<<" "<<c[y][0]<<" "<<c[y][1]<<endl;
down(y), pre = y, y = c[y][x > a[y]];
//}
if (y) {
lk[y].pb((plpl) {
k, w, an[y]
});
return ;
}
y = k;
an[y] = w;
fa[y] = pre;
if (pre)
c[pre][x > a[pre]] = y;
a[y] = x;
siz[y] = 1;
tag[y] = tag1[y] = c[y][0] = c[y][1] = 0;
//cout<<"wiefyhiorwe "<<y<<endl;
splay(y, 0);
//cout<<"finish\n";
}
int pre(int x) {
int u = rt, res = 0;
while (u) {
down(u);
if (a[u] < x)
res = u, u = c[u][1];
else
u = c[u][0];
}
return res;
}
int nxt(int x) {
int u = rt, res = 0;
while (u) {
down(u);
if (a[u] > x)
res = u, u = c[u][0];
else
u = c[u][1];
}
return res;
}
struct lsj {
int x, w, k;
} stk[N];
void dfs(int x) {
if (!x)
return ;
down(x);
if (c[x][0])
dfs(c[x][0]);
stk[++top] = {a[x], an[x], x};
if (!lk[x].empty())
for (re int i = 0, jj = lk[x].size() - 1; i <= jj; i++) {
int v = lk[x][i].x, w = lk[x][i].w, dj = lk[x][i].dj;
stk[++top] = {a[x], an[x] + w - dj, v};
}
lk[x].clear();
if (c[x][1])
dfs(c[x][1]);
c[x][0] = c[x][1] = 0;
a[x] = an[x] = fa[x] = 0;
}
void dfs1(int x) {
down(x);
// cout<<"lsj "<<x<<" "<<an[x]<<endl;
if (c[x][0])
dfs1(c[x][0]);
ans[x - 1] = an[x];
if (!lk[x].empty())
for (re int i = 0, jj = lk[x].size() - 1; i <= jj; i++) {
int v = lk[x][i].x, w = lk[x][i].w, dj = lk[x][i].dj;
ans[v - 1] = w + an[x] - dj;
}
if (c[x][1])
dfs1(c[x][1]);
}
signed main() {
IN(n);
lep(i, 1, n) IN(q[i].c), IN(q[i].w);
sort(q + 1, q + 1 + n, cmp);
IN(m);
ins(-inf, 1, 0);
ins(inf, m + 2, 0);
lep(i, 1, m) IN(x), ins(x, i + 1, 0);
lep(i, 1, n) {
int x = q[i].c;
int aa = pre(x), bb = nxt((2 * x));
splay(aa, 0);
splay(bb, aa);
//cout<<"ok "<<i<<" "<<x<<" "<<aa<<" "<<bb<<" "<<c[bb][1]<<" "<<c[bb][0]<<endl;
//lep(j,1,m+2) cout<<j<<" "<<a[j]<<" "<<an[j]<<" "<<c[j][0]<<" "<<c[j][1]<<" "<<fa[j]<<endl;
down(bb);
tag[c[bb][1]] -= x;
a[bb] -= x;
a[c[bb][1]] -= x;
an[bb]++;
an[c[bb][1]]++;
tag1[c[bb][1]]++;
dfs(c[bb][0]);
c[bb][0] = 0; //cout<<"babamama "<<top<<endl;
lep(j, 1, top) {
ins(stk[j].x - x, stk[j].k, stk[j].w + 1);
//cout<<stk[j].x-x<<" "<<stk[j].k<<" "<<stk[j].w+1<<endl;
}
top = 0;
}
dfs1(rt);
lep(i, 1, m) printf("%lld ", ans[i]);
return 0;
}
/*
*/
by zuytong @ 2021-12-24 22:06:50
建议用指针
by Ckger @ 2021-12-24 22:17:33
放弃吧!耀哥!