제출 #1328959

#제출 시각아이디문제언어결과실행 시간메모리
1328959sitingfakeSterilizing Spray (JOI15_sterilizing)C++20
20 / 100
195 ms1860 KiB
#include<bits/stdc++.h>
using namespace std;

// define
//bool M1;
//#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
//#define memory cerr << abs(&M2 - &M1)/1024.0/1024 << " MB" << "\n"


#define ll long long
#define ii pair <int , int>
#define iii pair <int , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define prev __prev
#define next __next
#define sitingfake 1
#define orz 1

//constant

const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};

template<typename T> bool maximize(T &a, const T &b){
    if(a < b) {a = b; return 1;}
    return 0;
}

template<typename T> bool minimize(T &a, const T &b){
    if(a > b) {a = b; return 1;}
    return 0;
}

void Plus(ll & a ,ll b){
    b %= mod;
    a += b;
    if(a < 0) a += mod;
    a %= mod;
    return;
}

void Mul(ll & a, ll b){
    (a *= (b % mod)) %= mod;
    return;
}

//code
const int maxn = 1e5 + 7;
int n , k , q;
int a[maxn];
ll val[40];
ll SumMod[400][60] , Sum[400];

int L[450] , R[450] , id[maxn];
int Flag[450];
int lim = 0;
int BlockSize;

void Build(){
    BlockSize = sqrt(n);
    val[0] = 1;
    for(int i = 1; i <= 35; i++){
        val[i] = val[i - 1] * 1ll * k;
        if(val[i] > 1e9){
            lim = i - 1;
            break;
        }
    }
    if(k == 1) lim = 0;
    for(int i = 1; i <= n; i++){
        id[i] = (i + BlockSize - 1) / BlockSize;
        R[id[i]] = i;
        if(L[id[i]] == 0) L[id[i]] = i;
        Sum[id[i]] += a[i];
        for(int j = 0; j <= lim; j++){
            SumMod[id[i]][j] += (a[i] % val[j]);
        }
        Flag[id[i]] = 0;
    }
}

void Apply(int pos){
    if(Flag[pos] == 0) return;
    Sum[pos] = 0;
    for(int i = 0; i <= lim; i++) SumMod[pos][i] = 0;
    for(int i = L[pos]; i <= R[pos]; i++){
        a[i] /= val[Flag[pos]];
        Sum[pos] += a[i];
        if(a[i] != 0 && k != 1){
            for(int j = 0; j <= lim; j++){
                SumMod[pos][j] += (a[i] % val[j]);
            }
        }
    }
    Flag[pos] = 0;
}

void UpdatePos(int pos , int x){
    Apply(id[pos]);
    Sum[id[pos]] -= a[pos];
    Sum[id[pos]] += x;
    for(int i = 0; i <= lim; i++){
        SumMod[id[pos]][i] -= (a[pos] % val[i]);
        SumMod[id[pos]][i] += (x % val[i]);
    }
    a[pos] = x;
}
void UpdateRange(int l , int r){
    if(id[l] == id[r]){
        Apply(id[l]);
        for(int i = l; i <= r; i++){
            if(a[i] == 0) continue;
            Sum[id[l]] -= a[i];
            Sum[id[l]] += (a[i] / k);
            for(int j = 0; j <= lim; j++){
                SumMod[id[l]][j] -= (a[i] % val[j]);
                SumMod[id[l]][j] += ((a[i] / k) % val[j]);
            }
            a[i] /= k;
        }
        return;
    }
    for(int i = id[l] + 1; i < id[r]; i++){
        Flag[i]++;
        if(Flag[i] > lim) Flag[i] = lim;
    }
    Apply(id[l]);
    Apply(id[r]);

    for(int i = l; i <= R[id[l]]; i++){
        if(a[i] == 0) continue;
        Sum[id[l]] -= a[i];
        Sum[id[l]] += (a[i] / k);
        for(int j = 0; j <= lim; j++){
            SumMod[id[l]][j] -= (a[i] % val[j]);
            SumMod[id[l]][j] += ((a[i] / k) % val[j]);
        }
        a[i] /= k;
    }

    for(int i = r; i >= L[id[r]]; i--){
        if(a[i] == 0) continue;
        Sum[id[r]] -= a[i];
        Sum[id[r]] += (a[i] / k);
        for(int j = 0; j <= lim; j++){
            SumMod[id[r]][j] -= (a[i] % val[j]);
            SumMod[id[r]][j] += ((a[i] / k) % val[j]);
        }
        a[i] /= k;
    }
}

void QueryRange(int l , int r){
    ll ans = 0;
    if(id[l] == id[r]){
        Apply(id[l]);
        for(int i = l ; i <= r; i++) ans += a[i];
        cout << ans << "\n";
        return;
    }
    for(int i = id[l] + 1; i < id[r]; i++){
        ans += ((Sum[i] / val[Flag[i]]) - (SumMod[i][Flag[i]] / val[Flag[i]]));
        //cout << Sum[i] << " " << Flag[i]<< " " << L[i] << " " << R[i] << " 111 " << endl;
    }
    Apply(id[l]);
    Apply(id[r]);
    for(int i = l; i <= R[id[l]]; i++) ans += a[i];
    for(int i = r ; i >= L[id[r]]; i--) ans += a[i];
    cout << ans << "\n";
}

void debug(){
    for(int i = id[1]; i <= id[n]; i++){
        cout << Sum[i] << endl;
        for(int j = 0; j <= lim; j++){
            cout << SumMod[i][j] << " ";
        }
        cout << endl;
    }
}
void solve(void){
    cin >> n >> q >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    Build();
    while(q--){
        int t , l , r;
        cin >> t >> l >> r;
        if(t == 1){
            UpdatePos(l , r);
        }
        else if(t == 2){
            UpdateRange(l , r);
        }
        else{
            QueryRange(l , r);
        }
    }
}
/**
5 6 3
1 2 8 1 3
1 2 5
2 3 5
3 2 5
2 1 4
1 3 2
3 3 5

**/
//bool M2;
signed main(){
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);

   #define task "gray"

   if(fopen(task".inp","r")){
       freopen(task".inp","r",stdin);
       freopen(task".out","w",stdout);
   }

   int tc = 1;
//   cin >> tc;
   while(tc--) solve();

//   execute;
//   memory;
}

컴파일 시 표준 에러 (stderr) 메시지

sterilizing.cpp: In function 'int main()':
sterilizing.cpp:235:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  235 |        freopen(task".inp","r",stdin);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sterilizing.cpp:236:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  236 |        freopen(task".out","w",stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...