上次比赛最后时间紧慌了,没好好审题,有一个重要条件没好好利用,还瞎排序。
也就是询问和操作的时间都是按顺序给出的,只需要按照他给的顺序依次操作就行了。
还有一个条件就是
这个保证同样非常重要,再一次验证只需要按照给出的顺序操作,因为同时课的询问一定在更新之后。
然后具体思路就是这样:
首先维护一个存储传播更新操作的队列
struct op
{
int id;
int tim;
vector<int>q;
};
vector<op>op;
(这个队列的自然顺序就是按时间顺序的,因为我们添加op的时候本身已经是按时间先后选项入队了)
当需要更新的时候,我们
首先执行一个update(time)操作,update(time)用来处理在time时间之前所有的传播更新操作,也就是把队列中time小于当前time的都执行然后出队。
然后在更新a这个节点
最后把a的传播操作存入队列op
就大功告成了。
然后是查询:
查询首先也是upate(time)
然后直接输出a的链就ok了。
大致流程就是如下:
if(type==2)
{
int id=input[0], tim=input[1];
update(tim);
L=block[id].size();
printf("%d", L);
for(int i=0; i<L; i++)printf(" %d", block[id][i]);printf("\n");
}
else
{
int id=input[0], tim=input[1], val=input[2];
update(tim);
block[id].ps(val);
add_op(id, tim+t, block[id]);
}
完整code:
#include <bits/stdc++.h>
#define ps push_back
using namespace std;
const int maxo=2e6+10;
struct op
{
int id;
int tim;
vector<int>q;
};
vector<op>op;
int head, tail;
vector<int>block[505];
vector<int>edg[505];
int n, m, t, k;
char buf[105];
int input[5];
int read(char a[])
{
int cnt=0, sum=0;
for(int i=0; a[i]; i++)
{
if(isdigit(a[i]))
sum=sum*10+(a[i]-'0');
else{
input[cnt]=sum;
sum=0;
cnt++;
}
}
input[cnt]=sum;
cnt++;
return cnt;
}
void add_op(int id, int tim, vector<int> q)
{
struct op tmp;
tmp.id=id;
tmp.tim=tim;
tmp.q=q;
op.ps(tmp);
tail++;
}
void update(int tim)
{
int id, to;
vector<int>q;
while(head<tail)
{
if(op[head].tim>tim)break;
q=op[head].q;
id=op[head].id;
for(int i=0; i<edg[id].size(); i++)
{
to=edg[id][i];
if(q.size()>block[to].size())
{
block[to]=q;
add_op(to, op[head].tim+t, block[to]);
}
else if(q.size()==block[to].size())
{
if(q[q.size()-1]<block[to][block[to].size()-1]){
block[to]=q;
add_op(to, op[head].tim+t, block[to]);
}
}
}
head++;
}
}
int main()
{
int x, y;
cin>>n>>m;
while(m--)
{
scanf("%d%d", &x, &y);
edg[x].ps(y);
edg[y].ps(x);
}
for(int i=1; i<=n; i++)block[i].ps(0);
scanf("%d%d\n", &t, &k);
// cout<<t<<k<<endl;
int L;
while(k--)
{
gets(buf);
// printf("%s\n", buf);
int type=read(buf);
// printf("%d %d %d %d\n", type, input[0], input[1], input[2]);
if(type==2)
{
int id=input[0], tim=input[1];
update(tim);
L=block[id].size();
printf("%d", L);
for(int i=0; i<L; i++)printf(" %d", block[id][i]);printf("\n");
}
else
{
int id=input[0], tim=input[1], val=input[2];
update(tim);
block[id].ps(val);
add_op(id, tim+t, block[id]);
}
}
return 0;
}
/*
5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
1 27
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
1 10 10
2 11 9
1 11
2 11
3 11
4 11
5 11
1 12
2 12
3 12
4 12
5 12
*/