zoukankan      html  css  js  c++  java
  • GYM 100741A Queries

    传送门

    题目大意:

    一个长度为n的序列,q次三种操作

    +p  r:下标为p的数+r

    -p r:下标为p的数-r

    s l r mod [L,R]中有多少数%m=mod,m已经给出

    题解:

    开十个树状数组

    代码

    我的

    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    int n,m,q,l,r,od,p,mod,a[100];
    struct Tree{
        int tre[1001];
    }s[10];
    
    int lowbit(int x){
        return x&(-x);
    }
    
    void add(int k,int p){
        while(p<=n){
            s[k].tre[p]++;
            p+=lowbit(p);
        }
    }
    
    void modify(int k,int p,int x){
        while(p<=n){
            s[k].tre[p]+=x;
            p+=lowbit(x);
        }
    }
    
    int sum(int l,int r,int mod){
        int cntl=0,cntr=0;l--;
        while(l){
            cntl+=s[mod].tre[l];
            l-=lowbit(l);
        }
        while(r){
            cntr+=s[mod].tre[r];
            r-=lowbit(r);
        }
        return cntr-cntl;
    } 
    
    int main(){
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            add(a[i]%m,i);
        }
        for(int i=1;i<=q;i++){
            scanf("%d",&od);
            if(od==1){
                scanf("%d%d",&p,&r);
                modify(a[p]%m,p,-1);
                a[p]+=r;
                modify(a[p]%m,p,1);
            }
            if(od==2){
                scanf("%d%d",&p,&r);
                modify(a[p]%m,p,-1);
                a[p]-=r;
                modify(a[p]%m,p,1);
            }
            if(od==3){
                scanf("%d%d%d",&l,&r,&mod);
                printf("%d
    ",sum(l,r,mod));
            }
        }
        return 0;
    }

    丁神的

    #include<bits/stdc++.h>
    #define N 10010
    #define ll long long
    using namespace std;
    
    struct BIT {
        ll c[N];
        int n;
    
        int lowbit(int x) {
            return x&(-x);
        }
        void modify(int x,ll y) {
            for(; x<=n; x+=lowbit(x)) c[x]+=y;
        }
        ll query(int x) {
            ll ret=0;
            for(; x; x-=lowbit(x)) ret+=c[x];
            return ret;
        }
    
        ll query(int l,int r) {
            return query(r)-query(l-1);
        }
    } bit[20];
    
    int n,m,T;
    ll a[N];
    
    int main() {
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++) bit[i].n=n;
        for(int i=1; i<=n; i++) {
            scanf("%lld",&a[i]);
            bit[a[i]%m].modify(i,a[i]);
        }
        scanf("%d",&T);
        while(T--) {
            int x,y,z;
            char opt[10];
            scanf("%s%d%d",opt,&x,&y);
            if(opt[0]=='+') {
                bit[a[x]%m].modify(x,-a[x]);
                a[x]+=y;
                bit[a[x]%m].modify(x,a[x]);
                printf("%lld
    ",a[x]);
            }
            if(opt[0]=='-') {
                if(a[x]<y) {
                    printf("%lld
    ",a[x]);
                } else {
                    bit[a[x]%m].modify(x,-a[x]);
                    a[x]-=y;
                    bit[a[x]%m].modify(x,a[x]);
                    printf("%lld
    ",a[x]);
                }
            }
            if(opt[0]=='s') {
                scanf("%d",&z);
                printf("%lld
    ",bit[z].query(x,y));
            }
        }
        return 0;
    }
  • 相关阅读:
    完整的UED流程
    curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
    部署方案模板
    商城项目合作
    架构师分类
    k8s视图
    虚拟化通信技术
    AxureRP8 实现时间功能
    在AxureRP8中实现广告文字滚动效果
    获取当前网络中的电脑数目及MAC-通过MAC查找IP-通过IP查询机器名
  • 原文地址:https://www.cnblogs.com/zzyh/p/7304919.html
Copyright © 2011-2022 走看看