結果

問題 No.3507 RangeSum RangeUpdate RangeSqrt
コンテスト
ユーザー wsrtrt
提出日時 2026-04-18 05:27:34
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 16,196 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,783 ms
コンパイル使用メモリ 392,004 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2026-04-18 05:27:49
合計ジャッジ時間 10,030 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define ll long long
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define pii pair<int, int>
#define pll pair<long long,long long>
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<long long>
#define vc vector<char>
#define vvi vector<vector<int>> 
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
template <class T> void print(const T &a) { cout << a; }
void OUT() { cout << endl; }
template <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {
    print(head);
    if(sizeof...(tail)) cout << ' ';
    OUT(tail...);
}
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> pair<T, T> operator-(const pair<T, T> &x, const pair<T, T> &y) { return pair<T, T>(x.ff - y.ff, x.ss - y.ss); }
template <class T> pair<T, T> operator+(const pair<T, T> &x, const pair<T, T> &y) { return pair<T, T>(x.ff + y.ff, x.ss + y.ss); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.ff, r.ff), min(l.ss, r.ss)); }
template <class T> vector<T> &operator--(vector<T> &v) {
    fore(e, v) e--;
    return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e--;
    return res;
}
template <class T> vector<T> &operator++(vector<T> &v) {
    fore(e, v) e++;
    return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e++;
    return res;
}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
//座標圧縮
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    UNIQUE(y);
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x + y - 1) / y);
}
long long POW(long long x, int n) {
    long long res = 1LL;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
//0^n=0
long long modpow(long long a, long long n, long long mod) {
    a%=mod;
    assert(a!=0||n!=0);
    if(a==0)return 0;
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//return 0<=a&&a<h&&0<=b&&b<w;
inline bool ingrid(ll a,ll b,ll h,ll w){return 0<=a&&a<h&&0<=b&&b<w;}
//return 0<=a&&a<n;
inline bool inl(int a,int n){return 0<=a&&a<n;}
// bit 演算系
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
ll allbit(ll n) { return (1LL << n) - 1; }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}
long long sqrtll(long long x) {
    assert(x >= 0);
    long long rev = sqrt(x);
    while(rev * rev > x) --rev;
    while((rev+1) * (rev+1)<=x) ++rev;
    return rev;
}
int logN(long long n){
    int ret=1;
    while((1LL<<ret)<n)ret++;
    return ret;
}
const double PI=3.1415926535897932384626433832795028841971;
const ll MOD998 = 998244353;
const int INFI = numeric_limits<int>::max() / 2; const long long INFL = numeric_limits<long long>::max() / 2;
#define inf INFINITY

template<class T>
void debug(vector<T> a){
    rep(i,0,(int)a.size()){
        cout<<a[i]<<' ';
    }
    cout<<endl;
    return;
}

bool palindrome(const string& s){
    return equal(all(s),s.rbegin());
}

template <std::uint_fast64_t Modulus> class modint {
    using u64 = std::uint_fast64_t;
public:
    u64 a;
    constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
    constexpr u64 &val() noexcept { return a; }
    constexpr const u64 &val() const noexcept { return a; }
    constexpr modint operator+(const modint rhs) const noexcept {
        return modint(*this) += rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept {
        return modint(*this) -= rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept {
        return modint(*this) *= rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept {
        return modint(*this) /= rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
            a -= Modulus;
        }
        return *this;
    }
    constexpr modint &operator-=(const modint rhs) noexcept {
        if (a < rhs.a) {
            a += Modulus;
        }
        a -= rhs.a;
        return *this;
    }
    constexpr modint &operator*=(const modint rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr modint &operator/=(modint rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
        if (exp % 2) {
          *this *= rhs;
        }
        rhs *= rhs;
        exp /= 2;
        }
        return *this;
    }
    friend bool operator==(const modint& a,const modint& b) { return a.val()==b.val(); }
    friend bool operator!=(const modint& a,const modint& b) { return a.val()!=b.val(); }
};
using mint9=modint<998244353>;
using mint1=modint<1000000007>;

//costを指定しないと重みなし辺になります
struct Edge{
    int from,to;
    ll cost;
    Edge()=default;
    Edge(int from,int to,ll cost=1):from(from),to(to),cost(cost){}
    operator int() const {return to;}
};

constexpr pii dx4[4] = {pii{-1, 0},pii{0, -1}, pii{0, 1}, pii{1, 0} };
constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
constexpr pii dx[100]={{0,0},{-1,0},{0,-1},{-1,-1}};
struct node{
    int l,r;ll val;
    ll query_sum(int a,int b){
        chmax(a,l);chmin(b,r);
        if(b<=a)return 0;
        return val*(b-a);
    }
    ll sum(){return val*(r-l);}
};
const int B=350;
struct block{
private:
    vector<node> v;
    ll cnt_1{};
    vector<node> v1;
public:
    int l,r;ll sum{};
    void init(int x,ll val){
        if(val>1){
            v.push_back({x,x+1,val});
            sum+=val;
        }
        else {
            if(val==0)return;
            if(val==1){
                cnt_1++;
                v1.push_back({x,x+1,val});
            }
        }
    }
    void update(int a,int b,ll val){
        chmax(a,l);chmin(b,r);
        if(b<=a)return;
        if(a==l&&b==r){
            v.clear();
            v.push_back({l,r,val});
            sum=val*(r-l);
            return;
        }else{
            {
                vector<node> nxt;
            rep(i,0,v1.size()){
                //まったく関係がない
                if(b<=v1[i].l||v1[i].r<=a){
                    nxt.push_back(v1[i]);
                    continue;
                }
                //完全に包含されている
                if(a<=v1[i].l&&v1[i].r<=b){
                    continue;
                }
                //左端を含む
                if(v1[i].l<=a&&a<=v1[i].r){
                    if(v1[i].l<a){
                        nxt.push_back({v1[i].l,a,v1[i].val});
                    }
                }
                //右端を含む
                if(v1[i].l<=b&&b<=v1[i].r){
                    if(b<v1[i].r){
                        nxt.push_back({b,v1[i].r,v1[i].val});
                    }
                }
            }
                swap(v1,nxt);
                cnt_1=0;
                rep(i,0,v1.size()){
                    cnt_1+=v1[i].sum();
                }
            }
            vector<node> nxt;
            rep(i,0,v.size()){
                //まったく関係がない
                if(b<=v[i].l||v[i].r<=a){
                    nxt.push_back(v[i]);
                    continue;
                }
                //完全に包含されている
                if(a<=v[i].l&&v[i].r<=b){
                    continue;
                }
                //左端を含む
                if(v[i].l<=a&&a<=v[i].r){
                    if(v[i].l<a){
                        nxt.push_back({v[i].l,a,v[i].val});
                    }
                }
                //右端を含む
                if(v[i].l<=b&&b<=v[i].r){
                    if(b<v[i].r){
                        nxt.push_back({b,v[i].r,v[i].val});
                    }
                }
            }
            if(val!=1)nxt.push_back({a,b,val});
            else {
                v1.push_back({a,b,val});
                cnt_1+=b-a;
            }
            swap(v,nxt);
            sum=0;
            rep(i,0,v.size()){
                sum+=v[i].sum();
            }
        }
    }
    ll query_sum(int a,int b){
        //[l,r),[a,b)
        chmax(a,l);chmin(b,r);
        //まったく含まれない
        if(b<=a)return 0;
        //完全に包含さてている
        if(a==l&&b==r)return sum+cnt_1;
        //部分的に包含
        ll res{};
        rep(i,0,v.size()){
            res+=v[i].query_sum(a,b);
        }
        rep(i,0,v1.size()){
            res+=v1[i].query_sum(a,b);
        }
        return res;
    }
    void isqrt(int a,int b){
        chmax(a,l);chmin(b,r);
        if(b<=a)return;
        vector<node> nxt;
        rep(i,0,v.size()){
            ll is=sqrt(v[i].val);
            //まったく関係がない
            if(b<=v[i].l||v[i].r<=a){
                nxt.push_back(v[i]);
                continue;
            }
            //完全に包含されている
            if(a<=v[i].l&&v[i].r<=b){
                if(is!=1)nxt.push_back({v[i].l,v[i].r,is});
                else{ 
                    cnt_1+=(v[i].r-v[i].l);
                    v1.push_back({v[i].l,v[i].r,1});
                }
                continue;
            }
            //左端を含む
            if(v[i].l<=a&&a<=v[i].r){
                if(v[i].l<a){
                    if(is!=1)nxt.push_back({a,v[i].r,is});
                    else {
                        cnt_1+=(v[i].r-a);
                        v1.push_back({a,v[i].r,1});
                    }
                    chmin(v[i].r,a);
                }
            }
            //右端を含む
            if(v[i].l<=b&&b<=v[i].r){
                if(b<v[i].r){
                    if(is!=1)nxt.push_back({v[i].l,b,is});
                    else {
                        cnt_1+=(b-v[i].l);
                        v1.push_back({v[i].l,b,1});
                    }
                    chmax(v[i].l,b);
                }
            }
            if(v[i].l<v[i].r)nxt.push_back(v[i]);
        }
        swap(v,nxt);
        sum=0;
        rep(i,0,v.size()){
            sum+=v[i].sum();
        }
    }
};
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n,q;cin>>n>>q;
    vector<ll> a(n);
    block blocks[B];
    rep(i,0,B){
        blocks[i].sum=0;
        blocks[i].l=i*B;
        blocks[i].r=(i+1)*B;
    }
    rep(i,0,n){
        cin>>a[i];
        blocks[i/B].init(i,a[i]);
    }
    while(q--){
        int t;cin>>t;
        if(t==0){
            int l,r;cin>>l>>r;
            ll ans{};
            rep(i,0,B){
                ans+=blocks[i].query_sum(l,r);
            }
            cout<<ans<<endl;
        }else if(t==1){
            int l,r;ll x;cin>>l>>r>>x;
            rep(i,0,B){
                blocks[i].update(l,r,x);
            }
        }else{
            int l,r;cin>>l>>r;
            rep(i,0,B){
                blocks[i].isqrt(l,r);
            }
            //cout<<blocks[0].query_sum(0,1)<<endl;
        }
    }

	return 0;
}
/*
5回ぐらい

6 4
1 2 3 4 5 6
1 0 3 10
1 2 4 9
2 2 4
0 0 3
*/
0