BZOJ1938: [CROATIAN2010] ALADIN

题解  线段树+类欧模板题   我们考虑到取模可以转化成 a*(pos-l+1)-b*(a*(pos-l+1)/b)这样  那么可以知道前面直接接公式计算 后面用线段树覆盖 然后类欧统计答案即可

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=5e4+10;
const double eps=1e-8;
#define ll long long
using namespace std;
struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
ll atm(ll a,ll b,ll c,ll n){
    if(c<=0||n<0)return 0;
    ll m=(a*n+b)/c;
    if(a>=c||b>=c)return (a/c)*(n*(n+1)/2)+(b/c)*(n+1)+atm(a%c,b%c,c,n);
    else return n*m-atm(c,c-b-1,a,m-1);
}
typedef struct node{
    bool flag;
    int l,r;ll A,B,C,ans; 
}node;
node d[MAXN*21];
vector<int>vec;
ll A,B,C;
ll calc(ll l,ll r,ll a,ll b,ll c){
    ll ans=(r-c+2)*(r-c+1)/2;ans-=(l-c+1)*(l-c)/2;ans*=a;
    ans-=b*(atm(a,(1-c)*a,b,r)-atm(a,(1-c)*a,b,l-1));
    return ans;
}
void push(int rt,int l,int r){
    if(!d[rt].flag)return ;
    int mid=(l+r)>>1;
    d[rt<<1].flag=d[rt<<1|1].flag=1;d[rt].flag=0;
    d[rt<<1].A=d[rt<<1|1].A=d[rt].A;d[rt<<1].B=d[rt<<1|1].B=d[rt].B;d[rt<<1].C=d[rt<<1|1].C=d[rt].C;
    d[rt<<1].ans=calc(vec[l-2]+1,vec[mid-1],d[rt].A,d[rt].B,d[rt].C);
    d[rt<<1|1].ans=calc(vec[mid-1]+1,vec[r-1],d[rt].A,d[rt].B,d[rt].C);
}
void up(int rt){d[rt].ans=d[rt<<1].ans+d[rt<<1|1].ans;}
void update(int rt,int l,int r,int ql,int qr){
    //cout<<l<<"===="<<r<<endl;
    if(ql<=l&&r<=qr){d[rt].flag=1;d[rt].A=A;d[rt].B=B;d[rt].C=C;d[rt].ans=calc(vec[l-2]+1,vec[r-1],A,B,C);return ;}
    int mid=(l+r)>>1;
    push(rt,l,r);
    if(ql<=mid)update(rt<<1,l,mid,ql,qr);
    if(qr>mid)update(rt<<1|1,mid+1,r,ql,qr);
    up(rt);
    //cout<<l<<" "<<r<<" "<<d[rt].ans<<endl;
}
ll ans;
void querty(int rt,int l,int r,int ql,int qr){
    if(ql<=l&&r<=qr){ans+=d[rt].ans;return ;}
    int mid=(l+r)>>1;
    push(rt,l,r);
    if(ql<=mid)querty(rt<<1,l,mid,ql,qr);
    if(qr>mid)querty(rt<<1|1,mid+1,r,ql,qr);
    up(rt);
}
typedef struct Q{
    int op,l,r,A,B;
}Q;
Q que[MAXN];
int main(){
    int n,q;n=read();q=read();
    vec.pb(0);
    inc(i,1,q){
        que[i].op=read();que[i].l=read();que[i].r=read();vec.pb(que[i].l),vec.pb(que[i].l-1),vec.pb(que[i].l+1),vec.pb(que[i].r),vec.pb(que[i].r-1),vec.pb(que[i].r+1);
        if(que[i].op==1)que[i].A=read(),que[i].B=read();
    }
    sort(vec.begin(),vec.end());
    int sz=unique(vec.begin(),vec.end())-vec.begin();
    inc(i,1,q){
        int l=lower_bound(vec.begin(),vec.begin()+sz,que[i].l)-vec.begin()+1;
        int r=lower_bound(vec.begin(),vec.begin()+sz,que[i].r)-vec.begin()+1;
        if(que[i].op==1)A=que[i].A,B=que[i].B,C=que[i].l,update(1,1,sz,l,r);
        else {ans=0;querty(1,1,sz,l,r);printf("%lld\n",ans);}
    }
}

 

1938: [CROATIAN2010] ALADIN

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 95  Solved: 27
[Submit][Status][Discuss]

Description

有一个长度为 N 的序列P 和两种操作,共 Q个: 1. 给定 L, R, A, B,将第 L 到第 R 个之间的每个元素 Px 变成((X-L+1)×A)mod B 。 2. 给定 L, R,询问第 L 到第 R 个元素的和。 数据规模:N≤10^9 ,Q≤50000 , A, B≤106

Input

The first line contains two integers N i Q (1 ≤ N ≤ 1 000 000 000) (1 ≤ Q ≤ 50 000), number of boxes and number of queries. The next Q lines contain information about the simulation. If the line starts with 1, than it follows the format "1 L R A B" (1 ≤ L ≤ R ≤ N) (1 ≤ A, B ≤ 1 000 000), meaning that Aladin keyed in numbers L, R, A and B in the device and allowed the device to do its job. If the line starts with 2, than it follows the format "2 L R" (1 ≤ L ≤ R ≤ N). Meaning that Aladin wonders how many stones in total are ther stones are in boxes labeled L to R (inclusive).

Output

For each query beginning with 2 output the answer to that particular query. Queries should be processed in the order they are given in the input.

Sample Input

6 3
2 1 6
1 1 5 1 2
2 1 6

Sample Output

0
3

猜你喜欢

转载自www.cnblogs.com/wang9897/p/9805977.html
今日推荐