HDU 4267 A Simple Problem with Integers (线段树)

题意:

N<=5×104,[a,b](ia)%k=0(i[a,b])Aic,

分析:

  • ,i%k=a%k , 然后单独维护一下
  • ,,BIT,线,,
  • MLE,,线MLE
    add[k][mod]add[55]
  • BIT,

代码:
线段树:

//
//  Created by TaoSama on 2015-09-30
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 5e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, q, A[N];
int id[11][10];

#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

int add[N << 2][55];
bool lazy[N << 2];

int cur;

void push_down(int rt) {
    if(lazy[rt]) {
        for(int i = 0; i < 55; ++i) {
            add[rt << 1][i] += add[rt][i];
            add[rt << 1 | 1][i] += add[rt][i];
            add[rt][i] = 0;
        }
        lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt];
        lazy[rt] = 0;
    }
}
void build(int l, int r, int rt) {
    lazy[rt] = 0;
    for(int i = 0; i < 55; ++i) add[rt][i] = 0;
    if(l == r) return;
    int m = l + r >> 1;
    build(lson);
    build(rson);
}
void update(int L, int R, int v, int l, int r, int rt) {
    if(L <= l && r <= R) {
        lazy[rt] = 1;
        add[rt][cur] += v;
        return;
    }
    int m = l + r >> 1;
    push_down(rt);
    if(L <= m) update(L, R, v, lson);
    if(R > m) update(L, R, v, rson);
}
int query(int o, int l, int r, int rt) {
    if(l == r) {
        int ret = A[l];
        for(int k = 1; k <= 10; ++k) ret += add[rt][id[k][o % k]];
        return ret;
    }
    int m = l + r >> 1;
    push_down(rt);
    if(o <= m) return query(o, lson);
    else return query(o, rson);
}


void gao() {
    int cnt = 0;
    for(int i = 1; i <= 10; ++i) for(int j = 0; j < i; ++j) id[i][j] = cnt++;
    //cnt = 55;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    gao();
    while(scanf("%d", &n) == 1) {
        for(int i = 1; i <= n; ++i) scanf("%d", A + i);
        scanf("%d", &q);
        build(root);
        while(q--) {
            int op, a; scanf("%d%d", &op, &a);
            if(op == 1) {
                int b, k, c; scanf("%d%d%d", &b, &k, &c);
                cur = id[k][a % k];
                update(a, b, c, root);
            } else printf("%d\n", query(a, root));
        }
    }
    return 0;
}

BIT:

//
//  Created by TaoSama on 2015-09-30
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 5e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, q, A[N];
int b[11][10][N];

void add(int k, int mod, int i, int v) {
    for(; i <= n; i += i & -i) b[k][mod][i] += v;
}

int sum(int k, int mod, int i) {
    int ret = 0;
    for(; i; i -= i & -i) ret += b[k][mod][i];
    return ret;
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(scanf("%d", &n) == 1) {
        for(int i = 1; i <= n; ++i) scanf("%d", A + i);
        scanf("%d", &q);
        memset(b, 0, sizeof b);
        while(q--) {
            int op, a; scanf("%d%d", &op, &a);
            if(op == 1) {
                int b, k, c; scanf("%d%d%d", &b, &k, &c);
                add(k, a % k, a, c);
                add(k, a % k, b + 1, -c);
            } else {
                int ans = A[a];
                for(int k = 1; k <= 10; ++k) ans += sum(k, a % k, a);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值