HDU 4267 A Simple Problem with Integers(라인 트리)
N<=5×104, 매번 구간 [a, b]내 만족(i-3-a)%k=0(i∈[a, b])의 수Ai를 c로 증가시키고 마지막 단점 조회 값
분석:
//
// 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
", 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
", ans);
}
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
백준 알고리즘 14427번 : 수열과 쿼리 15길이가 N인 수열 A1, A2, ..., AN이 주어진다. 이때, 다음 쿼리를 수행하는 프로그램을 작성하시오. 1 i v : Ai를 v로 바꾼다. (1 ≤ i ≤ N, 1 ≤ v ≤ 109) 2 : 수열에서 크기가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.