链接:http://codeforces.com/problemset/problem/311/C
题目意思就不说了。。。(懒。。)
本题唯一的难点就是判断宝藏是否可达。其他的用线段树水过就行了。。。
观察题目数据范围,
除了第一个method K的范围是10^4 ,以后新加的method X的范围都是10^18, 不利于求解。
于是对于每一个X ,我们让它对K取余来求解。
比如你有一个宝藏在cell 23,
最开始的K为10 ,后来新加一个X为13,
于是你把dis[ 13%10==3 ]设为13,对于任意一个 s >=13 且 s%10==3 的位置 s ,
你都能取到,只要在13后面加任意个10就好了。
但是,对于3这个位置,
虽然3%10==3,但是却不能取到,因为3<( dis[ 3 ] = 13 ),
由此我们要在已经有的method里用SPFA 求出对于每个 s<K 最小的 dis[ s ],
然后对于每个位置a[ i ] , 判断 a[ i ] 是否大于等于 dis[ a[ i ] % K ]。
由此就判断出宝藏是否可达。剩下的水水的~
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <utility>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define DWN(i,r,l) for(int i=(r);i>=(l);--i)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define MID ((l+r)>>1)
#define L pos<<1,l,MID
#define R pos<<1|1,MID+1,r
#define LL pos<<1
#define RR pos<<1|1
#define N 100100
#define INF (1ll<<62)
vector<int>f;
long long dis[N];
bool flag[N];
long long a[N];
int c[N];
long long h;
int n,m,K;
struct Node
{
int max1,max2;
}node[N<<2];
void pushdown(int pos)
{
node[pos].max1=max(node[LL].max1,node[RR].max1);
node[pos].max2=max(node[LL].max2,node[RR].max2);
}
void build(int pos,int l,int r)
{
node[pos].max1=-1;
if(l==r) node[pos].max2=c[l];
else
{
build(L);
build(R);
pushdown(pos);
}
}
void update(int pos,int l,int r,int p,int add)
{
if(l==r)
{
node[pos].max1+=add;
node[pos].max2+=add;
}
else
{
if(p<=MID) update(L,p,add);
else update(R,p,add);
pushdown(pos);
}
}
void setnode(int pos,int l,int r)
{
if(l==r) node[pos].max1=-1;
else
{
if(node[LL].max1>=node[RR].max1) setnode(L);
else setnode(R);
pushdown(pos);
}
}
void trans(int pos,int l,int r,int p)
{
if(l==r)
{
node[pos].max1=node[pos].max2;
node[pos].max2=-1;
}
else
{
if(p<=MID) trans(L,p);
else trans(R,p);
pushdown(pos);
}
}
//**********************************************************//
//从这里开始看就好了
bool visit[N];
void spfa() //每加入一个method后更新dis
{
memset(visit,0,sizeof(visit));
queue<int>q;
REP(i,K) if(dis[i]<INF) q.push(i),visit[i]=1;
while(!q.empty())
{
int top=q.front();q.pop();
visit[top]=0;
REP(i,(int)f.size())
{
long long temp=dis[top]+dis[ f[i] ];
int to=(top+f[i])%K;
if(temp<dis[to])
{
dis[to]=temp;
if(!visit[to])
{
visit[to]=1;
q.push(to);
}
}
}
}
}
void init()
{
memset(flag,0,sizeof(flag));//判断这个宝藏之前是否取到了
REP(i,K) dis[i]=INF;
f.clear();
dis[0]=0;
}
int main()//线段树部分没必要看的,从SPFA开始看就好了
{
//freopen("in","r",stdin);
int x,y;
while(cin>>h>>n>>m>>K)
{
init();
FOR(i,1,n)
{
scanf("%I64d%d",&a[i],&c[i]);
a[i]--;
}
build(1,1,n);
FOR(i,1,n)
{
if(!flag[i] && a[i]>=dis[ a[i]%K ] )
{
flag[i]=1;
trans(1,1,n,i);//对最开始能到达的宝藏进行更新
}
}
long long temp;
while(m--)
{
scanf("%d",&x);
if(x==1)
{
scanf("%I64d",&temp);
f.pb(temp%K);//加入method
dis[ temp%K ]=min( dis[ temp%K ] , temp );
spfa();
FOR(i,1,n)
{
if(!flag[i] && a[i]>=dis[ a[i]%K ] )
{
flag[i]=1;
trans(1,1,n,i);//对新能到达的宝藏进行更新
}
}
}
else if(x==2)
{
scanf("%d%d",&x,&y);
update(1,1,n,x,-y);
}
else if(x==3)
{
if(node[1].max1<=-1)
{
puts("0");
}
else
{
printf("%d\n",node[1].max1);
setnode(1,1,n);//取走宝藏
}
}
}
}
return 0;
}