题意:给定给你一叠DV,编号1到n,1在最上面,n在最下面。然后现在给你m个操作,每次都指定一张CD,问要拿走这个CD需要挪走上面多少张CD,并且这个要拿走的CD放在这个叠CD的顶端。
析:这个题要倒着来做,我就是正着做的,太复杂了,因为每次操作后,都要再重新处理后面的数,时间复杂度太高。
如果是倒着做,每次只要更新后面的就好,不用管前面的数,因为前面的是不会影响到的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 300000 + 10;
const int mod = 100000 + 50;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r > 0 && r <= n && c > 0 && c <= m;
}
int sum[maxn];
int lowbit(int x){ return -x&x; }
void add(int x, int val){
while(x < maxn){
sum[x] += val;
x += lowbit(x);
}
}
int query(int x){
int ans = 0;
while(x){
ans += sum[x];
x -= lowbit(x);
}
return ans;
}
int pos[maxn];
int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
memset(sum, 0, sizeof sum);
for(int i = 1; i <= n; ++i){
pos[i] = n - i + 1;
add(pos[i], 1);
}
int cnt = n;
for(int i = 0; i < m; ++i){
if(i) putchar(' ');
int x;
scanf("%d", &x);
printf("%d", n - query(pos[x]));
add(pos[x], -1);
pos[x] = ++cnt;
add(pos[x], 1);
}
printf("
");
}
return 0;
}