cdjz 数据结构 Challenge 1
http://cdqz.openjudge.cn/ds/1001/
不明原因 CE,汇编出错
具体思路是对每个位置建立链表,修改时向该位置插入新节点并保存时间戳,查询时沿着链表找到第一个时间戳小于k的数字。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
class Trane {
public :
int value[maxn], time[maxn], prev[maxn], next[maxn];
int head, tail, tot;
void init() {
tot = 2, head = 1, tail = 2;
next[head] = tail;
prev[tail] = head;
}
void insert(int val, int t) {
int q = ++tot;
value[q] = val;
time[q] = t;
next[q] = next[q - 1];
next[q - 1] = q;
prev[q] = q - 1;
}
int ask(int t) {
int now = 2;
while(t <= time[now]) {
now ++;
t = time[now];
}
return value[now];
}
} a[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) a[i].init();
for(int i = 1, x; i <= n; i ++) {
scanf("%d", &x);
a[i].insert(x, 0);
}
for(int i = 1; i <= m; i ++) {
char cmd;
cin >> cmd;
if(cmd == 'Q') {
int x, t;
scanf("%d%d", &x, &t);
printf("%d\n", a[x].ask(t));
} else {
int x, d;
cin >> x >> d;
a[x].insert(d, i);
}
}
}