Codeforces 438D The Child and Sequence (线段树取模操作)
题目链接:http://codeforces.com/problemset/problem/438/D
#include <cmath> #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <ctype.h> #include <map> #include <vector> #include <set> #include <bitset> #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r using namespace std; typedef long long ll; static const int MAX_N = 1e5 + 5; int n; struct Node{ ll maxl, suml; }T[MAX_N << 2]; void pushUp(int rt){ T[rt].maxl = max(T[rt << 1].maxl, T[rt << 1 | 1].maxl); T[rt].suml = T[rt << 1].suml + T[rt << 1 | 1].suml; } void build(int rt, int l, int r){ if(l == r){ scanf("%lld", &T[rt].maxl); T[rt].suml = T[rt].maxl; return ; } int mid = l + r >> 1; build(lson); build(rson); pushUp(rt); } void updateMod(int rt, int l, int r, int ql, int qr, int v){ if(T[rt].maxl < v) return ; if(l == r){ T[rt].suml %= v; T[rt].maxl = T[rt].suml; return ; } int mid = l + r >> 1; if(ql <= mid) updateMod(lson, ql, qr ,v); if(qr > mid) updateMod(rson, ql, qr, v); pushUp(rt); } void updatePos(int rt, int l, int r, int pos, int v){ if(l == r){ T[rt].suml = T[rt].maxl = v; return ; } int mid = l + r >> 1; if(pos <= mid) updatePos(lson, pos, v); else updatePos(rson, pos, v); pushUp(rt); } ll query(int rt, int l, int r, int ql, int qr){ if(l >= ql && r <= qr) return T[rt].suml; int mid = l + r >> 1; ll ans = 0; if(ql <= mid) ans += query(lson, ql, qr); if(qr > mid) ans += query(rson, ql, qr); return ans; } int main(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int m; scanf("%d%d", &n, &m); build(1, 1, n); while(m--){ int opt, ql, qr, x; scanf("%d", &opt); if(opt == 1){ scanf("%d%d", &ql, &qr); printf("%lld\n", query(1, 1, n, ql, qr)); } else if(opt == 2){ scanf("%d%d%d", &ql, &qr, &x); updateMod(1, 1, n, ql, qr, x); } else{ scanf("%d%d", &ql, &x); updatePos(1, 1, n, ql, x); } } return 0; }