线段树(区间更新+区间最大)

https://nanti.jisuanke.com/t/42387

题意:n(1 <= n <= 1e5)个数,初始为1,有q(1 <= q < =1e5)次询问,两种操作。1、mul  l ,r , x 区间[l , r]乘以x(1 <= x <= 10)。2、 query l , r 询问区间[l , r]每个数分解质因数最大指数的最大值。

解法:理解题意后知道,就是在统计区间每个数的质数(2 ,3, 5, 7)个数,每个数质数的指数最大值的区间最大值。

维护四颗线段树(2,3,5,7)分别统计质数个数,查询每一棵树区间最大值,四棵树中的最大值即为答案。

#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
typedef long long ll ;
#define int ll
#define mod 1000000007
#define gcd __gcd
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//ll lcm(ll a , ll b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define cin(x) scanf("%lld" , &x);
using namespace std;
const int N = 1e7+9;
const int maxn = 1e5+9;
const double esp = 1e-2;
int q , n;
char s[20];
struct node{
    int l , r , ma, lazy;
};
struct Segment_Tree{
    node tree[maxn<<2];
    void pushup(int root){
        tree[root].val = tree[root>>1].val + tree[root>>1|1].val;
        tree[root].ma = max(tree[root<<1].ma , tree[root<<1|1].ma);
    }
    void pushdown(int root){
        tree[root<<1].ma += tree[root].lazy;
        tree[root<<1|1].ma += tree[root].lazy;
        tree[root<<1].lazy += tree[root].lazy;
        tree[root<<1|1].lazy += tree[root].lazy;
        tree[root].lazy = 0;
    }
    void build(int l , int r , int root){
        tree[root].l = l , tree[root].r = r , tree[root].val = 0, tree[root].lazy = 0 , tree[root].ma = 0;
        if(l == r){
            return ;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
    }
    void update(int l , int r , int root , int va){
        if(tree[root].l >= l && tree[root].r <= r){
            tree[root].lazy += va ;
            tree[root].ma += va;
            return ;
        }
        if(tree[root].lazy) pushdown(root);
        int mid = (tree[root].l + tree[root].r) >> 1;
        if(l <= mid)
            update(l , r , root<<1 , va);
        if(r > mid)
            update(l , r , root<<1|1 , va);
        pushup(root);
    }
    int query(int l , int r , int root){
        int ma = -INF;
        if(tree[root].l >= l && tree[root].r <= r){
            return tree[root].ma;
        }
        if(tree[root].lazy) pushdown(root);
        int mid = (tree[root].l + tree[root].r) >> 1;
        if(mid >= l)
            ma = max(ma , query(l , r , root<<1));
        if(r > mid)
            ma = max(ma , query(l , r , root<<1|1));
        return ma ;

    }
}pr2 , pr3 , pr5 , pr7;

void solve(){
    scanf("%lld%lld" , &n , &q);
    pr2.build(1 , n , 1);
    pr3.build(1 , n , 1);
    pr5.build(1 , n , 1);
    pr7.build(1 , n , 1);
    rep(i , 1 , q){
        scanf("%s" , s);
        if(s[0] == 'M' && s[1] == 'U'){
            int l , r , x;
            scanf("%lld%lld%lld" , &l , &r , &x);
            if(x == 2){
                pr2.update(l , r , 1,1);
            }else if(x == 3){
                pr3.update(l , r , 1,1);
            }else if(x == 4){
                pr2.update(l , r , 1,2);
            }else if(x == 5){
                pr5.update(l , r , 1,1);
            }else if(x == 6){
                pr2.update(l , r , 1,1);
                pr3.update(l , r , 1,1);
            }else if(x == 7){
                pr7.update(l , r , 1,1);
            }else if(x == 8){
                pr2.update(l , r , 1,3);
            }else if(x == 9){
                pr3.update(l , r , 1,2);
            }else if(x == 10){
                pr2.update(l , r , 1,1);
                pr5.update(l , r , 1,1);
            }
        }else{
            int l , r ;
            int ans = -1 ;
            scanf("%lld%lld" , &l , &r);
            ans = max(ans , pr2.query(l , r , 1));
            ans = max(ans , pr3.query(l , r , 1));
            ans = max(ans , pr5.query(l , r , 1));
            ans = max(ans , pr7.query(l , r , 1));
            printf("ANSWER %lld\n" , ans);
        }
    }
}

signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0); cout.tie(0);
    //int t ;
    //cin(t);
    //while(t--){
    //while(~scanf("%lld%lf" , &n , &a))
        solve();
    //}
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/12463010.html