【AHOI2009】【BZOJ1798】Seq 维护序列seq

来源:互联网 发布:手机滚动屏软件 编辑:程序博客网 时间:2024/05/21 19:22

Description

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。
Input

第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。第三行有一个整数M,表示操作总数。从第四行开始每行描述一个操作,输入的操作有以下三种形式: 操作1:“1 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含双引号)。询问所有满足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。
Output

对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。
Sample Input

7 43

1 2 3 4 5 6 7

5

1 2 5 5

3 2 4

2 3 7 9

3 1 3

3 4 7

Sample Output

2

35

8

HINT

【样例说明】

初始时数列为(1,2,3,4,5,6,7)。
经过第1次操作后,数列为(1,10,15,20,25,6,7)。
对第2次操作,和为10+15+20=45,模43的结果是2。
经过第3次操作后,数列为(1,10,24,29,34,15,16}
对第4次操作,和为1+10+24=35,模43的结果是35。
对第5次操作,和为29+34+15+16=94,模43的结果是8。

测试数据规模如下表所示

数据编号 1 2 3 4 5 6 7 8 9 10
N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
Source

Day1

听说你们都用的线段树..
直接把2631的LCT拿了下来改几个longlong就过了..

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 100010#define LL long longusing namespace std;char ch[3];int opt,u,v,w;int n,m,P;int sta[MAXN],top;struct splay{    int fa,ch[2],size;    bool rev;    LL sum,w,time,plus;}tree[MAXN];void in(int &x){    char ch=getchar();int flag=1;x=0;    while (!(ch>='0'&&ch<='9')) flag=ch=='-'?-1:1,ch=getchar();    while (ch>='0'&&ch<='9')    x=x*10+ch-'0',ch=getchar();x*=flag;}bool is_root(int x) {    return tree[tree[x].fa].ch[0]!=x&&tree[tree[x].fa].ch[1]!=x;   }void push_up(int x){    tree[x].sum=(tree[tree[x].ch[0]].sum+tree[tree[x].ch[1]].sum+tree[x].w)%P;    tree[x].size=(tree[tree[x].ch[0]].size+tree[tree[x].ch[1]].size+1)%P;}void calc(int x,LL tim,LL plu){    if (!x) return;    tree[x].w=(tree[x].w*tim+plu)%P;    tree[x].sum=(tree[x].sum*tim+plu*tree[x].size)%P;    tree[x].plus=(tree[x].plus*tim+plu)%P;    tree[x].time=(tree[x].time*tim)%P;}void push_down(int x){    if (tree[x].rev)    {        tree[x].rev^=1;tree[tree[x].ch[0]].rev^=1;tree[tree[x].ch[1]].rev^=1;        swap(tree[x].ch[0],tree[x].ch[1]);    }    if (tree[x].time!=1||tree[x].plus!=0)   calc(tree[x].ch[0],tree[x].time,tree[x].plus),calc(tree[x].ch[1],tree[x].time,tree[x].plus);    tree[x].time=1;tree[x].plus=0;}void rot(int x){    int y=tree[x].fa,z=tree[y].fa,l,r;    l=(tree[y].ch[1]==x);r=l^1;    if (!is_root(y))    tree[z].ch[tree[z].ch[1]==y]=x;    tree[tree[x].ch[r]].fa=y;tree[y].fa=x;tree[x].fa=z;    tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y;    push_up(y);push_up(x);}void Splay(int x){    top=0;sta[++top]=x;    for (int i=x;!is_root(i);i=tree[i].fa)  sta[++top]=tree[i].fa;    while (top) push_down(sta[top--]);    while (!is_root(x))    {        int y=tree[x].fa,z=tree[y].fa;        if (!is_root(y))        {            if ((tree[y].ch[0]==x)^(tree[z].ch[0]==y))  rot(x);            else    rot(y);        }        rot(x);    }}void access(int x)  {    for (int i=0;x;i=x,x=tree[x].fa)    Splay(x),tree[x].ch[1]=i,push_up(x);   }void make_root(int x)   {    access(x);Splay(x);tree[x].rev^=1; }void link(int x,int y)  {   make_root(x);tree[x].fa=y;  }void split(int x,int y) {    make_root(x);access(y);Splay(y);   }int main(){    in(n);in(P);    for (int i=1;i<=n;i++)  in(w),tree[i].w=tree[i].sum=w%P,tree[i].size=tree[i].time=1;    for (int i=1;i<n;i++)   link(i,i+1),access(i+1);    for (in(m);m;m--)    {        in(opt);in(u);in(v);        if (opt==2) in(w),split(u,v),calc(v,1,w);        if (opt==1) in(w),split(u,v),calc(v,w,0);        if (opt==3) split(u,v),printf("%lld\n",tree[v].sum%P);    }}
1 0
原创粉丝点击