【codevs4367】控油肛和序列 线段树+暴力

题目描述 Description

在儿童节,一个熊孩子来到了哲学的圣城,企图膜拜王♂の哲学。由于熊孩子本性大发,将哲学圣城弄得十分脏乱。控油肛对他非常生气。这也不能说控油肛的脾气差,因为他弄丢了很多重要的东西。尤其是dc最喜欢的哲学序列。幸运的是控油肛记得如何修复。于是乎他必须快一点,在dc敢来之前修复好.

最初,控油肛需要创建一个整数的序列a1,a2,…………an。然后控油肛就可以执行以下操作:

1.打印操作:给定两个整数l,k 打印区间[l,k]内所有数的和

2.取模操作:给定三个整数l,k,x对于[l,k]区间内的每一个数a[i],l<=i<=k,a[i]=a[i]mod x;

3.设置操作:给定两个整数k,x使a[k]=x;

话说如果是平时控油肛可以一边上舰一边修复数列,可是dc快要回来了,dc若是看见了又要大发雷霆,所以控油肛找到了你,希望你能帮他解决。

输入描述 Input Description

输入的第一行包含了两个整数:n,m。第二行包含了n个整数,分别表示a1,a2………an的初始值。

一下m行分别对应着m条指令。每条指令的第一个整数对应着指令的类型

如果某行第一个整数是1,对应着打印操作,后面跟着两个整数l,k;

如果某行第一个整数是2,对应着取模操作,后面跟着三个整数l,k,x

如果某行第一个整数是3,对应着设置操作,后面跟着两个整数k,x

输出描述 Output Description

对于每个打印操作,输出一行结果。

注意,输出数据可能超过32位整数范围

样例输入 Sample Input

3 1

1 2 3

1 1 3

样例输出 Sample Output

6

数据范围及提示 Data Size & Hint

对于30%数据,m,n<=50000;

对于50%的数据,m,n<=100000;

对于75%的数据,m,n<=500000;

对于100%的数据,m,n<=700000;a[i]<=2^30-1并且所有输出均longlong以内.

备注:数据非常水放心做


历史遗留问题,我在填坑

又是个线段树+暴力的题

我只想写一下证明:

a%p=r ,设 a=kp+r ,则 a%p=r=(2r)/2<(p+r)/2<=(kp+r)/2=x/2 ,然后复杂度就是 O(nlognloga) 了…

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;
const int SZ = 1000010;
const int INF = 1000000010;
const double eps = 1e-6;

struct segment{
    int l,r;
    LL sum,maxn;
}tree[SZ];

LL num[SZ];

void update(int p)
{
    tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
    tree[p].maxn = max(tree[p << 1].maxn,tree[p << 1 | 1].maxn);
}

void build(int p,int l,int r)
{
    tree[p].l = l;
    tree[p].r = r;
    if(l == r)
    {
        tree[p].sum = tree[p].maxn = num[l];
        return ;
    }
    int mid = l + r >> 1;
    build(p << 1,l,mid);
    build(p << 1 | 1,mid + 1,r);
    update(p);
}

void modu(int p,int l,int r,LL mod)
{
    if(tree[p].l == tree[p].r)
    {
        tree[p].sum %= mod;
        tree[p].maxn %= mod;
        return ;
    }
    int mid = tree[p].l + tree[p].r >> 1;
    if(l <= mid && tree[p << 1].maxn >= mod) 
        modu(p << 1,l,r,mod);
    if(mid < r && tree[p << 1 | 1].maxn >= mod) 
        modu(p << 1 | 1,l,r,mod);
    update(p);
}

void change(int p,int pos,LL x)
{
    if(tree[p].l == tree[p].r)
    {
        tree[p].sum = x;
        tree[p].maxn = x;
        return ;
    }
    int mid = tree[p].l + tree[p].r >> 1;
    if(pos <= mid) change(p << 1,pos,x);
    else change(p << 1 | 1,pos,x);
    update(p);
}

LL ask(int p,int l,int r)
{
    if(l <= tree[p].l && tree[p].r <= r)
        return tree[p].sum;
    int mid = tree[p].l + tree[p].r >> 1;
    LL ans = 0;
    if(l <= mid) ans += ask(p << 1,l,r);
    if(mid < r) ans += ask(p << 1 | 1,l,r);
    return ans;
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++)
        scanf("%lld",&num[i]);
    build(1,1,n);
    while(m --)
    {
        int opt,l,r;
        LL x;
        scanf("%d",&opt);
        if(opt == 1)
            scanf("%d%d",&l,&r),printf("%lld\n",ask(1,l,r));
        else if(opt == 2)
            scanf("%d%d%lld",&l,&r,&x),modu(1,l,r,x);
        else
            scanf("%d%lld",&l,&x),change(1,l,x);            
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值