比赛 |
EYOI暨SBOI暑假快乐赛6th |
评测结果 |
WWWWWWWWWW |
题目名称 |
千风的诗篇 |
最终得分 |
0 |
用户昵称 |
yrtiop |
运行时间 |
2.191 s |
代码语言 |
C++ |
内存使用 |
6.80 MiB |
提交时间 |
2022-06-30 10:21:32 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n,m;
int a[maxn],pos[maxn];
int c[maxn];
int lowbit(int x) {
return x & -x;
}
void add(int x,int y) {
for(;x <= n;x += lowbit(x))c[x] += y;
return ;
}
int query(int x) {
int ans = 0;
for(;x;x -= lowbit(x))ans += c[x];
return ans;
}
struct queryy {
int x,y,z;
queryy() {
x = y = z = 0;
}
queryy(int x,int y,int z):x(x),y(y),z(z){}
}Q[maxn];
int ANS[maxn];
bool vis[maxn];
void solve(int l,int r) {
if(l >= r)return ;
int mid = l + r >> 1;
solve(l , mid);
solve(mid + 1 , r);
int i,j;
for(i = mid,j = r;i >= l;-- i) {
while(j > mid&&Q[i].y < Q[j].y)add(Q[j].z , 1),-- j;
ANS[Q[i].x] += query(Q[i].z - 1);
}
for(i = r;i > j;-- i)add(Q[i].z , -1);
for(i = l,j = mid + 1;i <= mid;++ i) {
while(j < r&&Q[j].y < Q[i].y)add(Q[j].z , 1),++ j;
ANS[Q[i].x] += query(n) - query(Q[i].z);
}
for(i = mid + 1;i < j;++ i)add(Q[i].z , -1);
sort(Q + l , Q + r + 1 , [](queryy x,queryy y){return x.y < y.y;});
return ;
}
int main() {
freopen("windy.in","r",stdin);
freopen("windy.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;++ i)scanf("%d",&a[i]),pos[a[i]] = i;
int ans = 0;
for(int i = n;i >= 1;-- i) {
ans += query(a[i] - 1);
add(a[i] , 1);
}
printf("%d\n",ans);
memset(c , 0 , sizeof(c));
int cnt = 0;
for(int i = 1;i <= m;++ i) {
int x;
scanf("%d",&x);
Q[++ cnt] = queryy(i , pos[x] , x);
vis[pos[x]] = true;
}
for(int i = 1;i <= n;++ i) {
if(!vis[i])Q[++ cnt] = queryy(m + 1 , i , a[i]);
}
solve(1 , cnt);
for(int i = 1;i < m;++ i)ans -= ANS[i],printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}