zoukankan      html  css  js  c++  java
  • 【洛谷P1483】序列变换

    题目大意:给定一个长度为 N 的序列,有 M 个操作,支持将下标为 x 的倍数的数都加上 y,查询下标为 i 的元素的值。

    题解:由于查询操作很少,相对的,修改操作很多。若直接模拟修改操作,即:枚举倍数,容易超时。现考虑记录下每次 x 位置的修改值,每次查询一个位置时,只需枚举这个位置的约数,将这个位置所有约数处的修改加入答案贡献即可。

    代码如下

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pb push_back
    #define mp make_pair
    #define all(x) x.begin(),x.end()
    #define cls(a,b) memset(a,b,sizeof(a))
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> P;
    const int dx[]={0,1,0,-1};
    const int dy[]={1,0,-1,0};
    const int mod=1e9+7;
    const int inf=0x3f3f3f3f;
    const int maxn=1e6+10;
    const double eps=1e-6;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll sqr(ll x){return x*x;}
    inline ll fpow(ll a,ll b,ll c){ll ret=1%c;for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;return ret;}
    inline ll read(){
        ll x=0,f=1;char ch;
        do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
        do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
        return f*x;
    }
    /*------------------------------------------------------------*/
    
    int n,q,a[maxn];
    ll d[maxn];
    
    void read_and_parse(){
        n=read(),q=read();
        for(int i=1;i<=n;i++)a[i]=read();
    }
    inline ll get(int idx){
        ll ret=0;
        for(int i=1;i<=sqrt(idx);i++){
            if(idx%i!=0)continue;
            ret+=d[i];
            if(i!=idx/i)ret+=d[idx/i];
        }
        return ret;
    }
    void solve(){
        while(q--){
            int opt=read();
            if(opt==1){
                int idx=read(),val=read();
                d[idx]+=val;
            }else{
                int idx=read();
                printf("%lld
    ",get(idx)+a[idx]);
            }
        }
    }
    int main(){
        read_and_parse();
        solve();
        return 0;
    }
    
  • 相关阅读:
    hdu1242 Rescue BFS广搜 + 优先队列
    hdu 1430 魔板
    康托展开
    hdu 4394 Digital Square(bfs)
    hdu 1969 Pie
    KMP模板
    hdu 1846 Brave Game
    循环赛日程表
    hdu 1022 Train Problem I
    整数划分问题
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10677013.html
Copyright © 2011-2022 走看看