Major cities have been destroyed by Jovian spacecrafts and humanity is fighting back. Nlogonia is spearheading the counter-offensive, by hacking into the spacecrafts' control system.
Unlike Earthling computers, in which usually a byte has 28 possible values, Jovian computers use bytes with B possible values, {0, 1,...,B - 1}. Nlogonian software engineers have reverse-engineered the firmware for the Jovian spacecrafts, and plan to sabotage it so that the ships eventually selfdestruct.
As a security measure, however, the Jovian spacecrafts run a supervisory program that periodically checks the integrity of the firmware, by hashing portions of it and comparing the result against known good values. To hash the portion of the firmware from the byte at position i to the byte at positionj, the supervisor uses the hash function
H(f2,...,f5) | = | B0f5 +B1f4 +B2f3 +B3f2 ( mod P) | (1) |
= | 200x 4 + 201x 2 + 202x 2 + 203x 14 ( mod 139) | (2) | |
= | 4 + 40 + 800 + 112000 ( mod 139) | (3) | |
= | 112844 ( mod 139) | (4) | |
= | 115 | (5) |
The Nlogonian cryptologists need to find a way to sabotage the firmware without tripping the supervisor. As a first step, you have been assigned to write a program to simulate the interleaving of two types of commands: editing bytes of the firmware by the Nlogonian software engineers, and computing hashes of portions of the firmware by the Jovian supervisory program. At the beginning of the simulation the value of every byte in the firmware is zero.
Each test case is described using several lines. The first line contains four integersB,P,L andN, whereB is the number of possible values of a Jovian byte,P is the modulus of the Jovian hash (2B <P109 andP prime),L is the length (number of Jovian bytes) of the spacecrafts' firmware, andN is the number of commands to simulate (1L,N105). At the beginning of the simulation the value of every byte in the firmware isfi = 0 for1iL. Each of the next N lines describes a command to simulate. Each command description starts with an uppercase letter that is either `E' or `H', with the following meanings.
`E' The line describes an edit command. The letter is followed by two integersI and V indicating that the byte at positionI of the firmware (that is,fI) must receive the valueV (1IL and 0VB - 1).
`H' The line describes a hash command. The letter is followed by two integersI and J indicating thatH(fI,...,fJ) must be computed (1IJL).
The last test case is followed by a line containing four zeros.
For each test case output the results of the hash commands in the input. In the i-th line write an integer representing the result of the i-th hash command. Print a line containing a single character `-' (hyphen) after each test case.
20 139 5 7 E 1 12 E 2 14 E 3 2 E 4 2 E 5 4 H 2 5 E 2 14 10 1000003 6 11 E 1 3 E 2 4 E 3 5 E 4 6 E 5 7 E 6 8 H 1 6 E 3 0 E 3 9 H 1 3 H 4 6 999999935 999999937 100000 7 E 100000 6 E 1 7 H 1 100000 E 50000 8 H 1 100000 H 25000 75000 H 23987 23987 0 0 0 0
115 - 345678 349 678 - 824973478 236724326 450867806 0 -
/*
这道题就是一道的线段树的题
用num存储树内所有的H(t[u].l,t[u].r),更新时根据更新点和区间右端点乘以个(B ^ (t[u].r - pos)),求和时根据求和区间右端点和线段树区间的右端点乘以个(B ^( r - t[u].r))。
//单点修改,区间求值
多组数据,要清空。
还有就是涉及到 进制转换 并且预处理出来B的幂次方
(这个没什么好说的,上代码吧)
#include
#include
#include
#define maxn 1000000
using namespace std;
/*
一道线段树的题
涉及到进制的转换
*/
int b,p,l,n;
long long B[maxn],ans;
int a[maxn];
struct tree
{
int l,r;
long long num;
}t[maxn*4];
void pre()
{
memset(a,0,sizeof(a));
B[0]=1;
for(int i=1;i<=l;i++)
B[i]=(B[i-1]*b)%p;
}
void Build(int l,int r,int u)
{
t[u].l=l;t[u].r=r;
t[u].num=0;//清空
if(t[u].l==t[u].r) return;
int mid=(l+r)>>1;
Build(l,mid,u<<1) ;
Build(mid+1,r,u<<1|1);
}
void update(int l,int u,int num)
{
t[u].num=(t[u].num+B[t[u].r-l]*num%p)%p;///注意向上更新num
if(t[u].num<0) t[u].num+=p;
if(t[u].l==t[u].r) return;
int mid=(t[u].l+t[u].r)>>1;
if(l>mid) update(l,u<<1|1,num);
else update(l,u<<1,num);
}
void query(int l,int r,int u)
{
if(l<=t[u].l&&r>=t[u].r)
{
ans=(ans+B[r-t[u].r]*t[u].num%p)%p;
if(ans<0) ans+=p;///!!!
return;
}
int mid=(t[u].l+t[u].r)>>1;
if(l<=mid) query(l,r,u<<1);
if(r>mid) query(l,r,u<<1|1);
}
int main()
{
char op;
int x,y;
while(scanf("%d%d%d%d",&b,&p,&l,&n))
{
if(b==0&&p==0&&l==0&&n==0) break;
pre();
Build(1,l,1);
for(int i=1;i<=n;i++)
{
getchar();
scanf("%c%d%d",&op,&x,&y);
if(op=='E')
{
update(x,1,y-a[x]);
a[x]=y;
}
else
{
ans=0;
query(x,y,1);
printf("%lld\n",ans);
}
}
printf("-\n");
}
return 0;
}