数列分块入门 1

mac2024-10-15  26

分块九题


#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <limits> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #define lowbit(x) ( x&(-x) ) #define pi 3.141592653589793 #define e 2.718281828459045 #define INF 0x3f3f3f3f #define eps 1e-6 #define HalF (l + r)>>1 #define lsn rt<<1 #define rsn rt<<1|1 #define Lson lsn, l, mid #define Rson rsn, mid+1, r #define QL Lson, ql, qr #define QR Rson, ql, qr #define myself rt, l, r using namespace std; typedef unsigned long long ull; typedef unsigned int uit; typedef long long ll; const int maxN = 5e4 + 7; int N, Q, len, s, val[maxN]; struct K { int l, r, sum; }t[maxN]; void update(int ql, int qr, int w) { int st = (ql - 1) / len + 1, ed = (qr - 1) / len + 1; if(st == ed) { for(int i=ql; i<=qr; i++) val[i] += w; return; } for(int i=t[st].l; i<=t[st].r; i++) { if(i >= ql && i <= qr) val[i] += w; } for(int i=t[ed].l; i<=t[ed].r; i++) { if(i >= ql && i <= qr) val[i] += w; } for(int i=st + 1; i <= ed - 1; i++) t[i].sum += w; } int main() { scanf("%d", &N); Q = N; len = sqrt(N); s = N / len + (N % len == 0 ? 0 : 1); for(int i=1; i<=s; i++) { t[i].l = (i - 1) * len + 1; t[i].r = i * len; t[i].sum = 0; } for(int i=1; i<=N; i++) scanf("%d", &val[i]); int op, l, r, c, belong; while(Q--) { scanf("%d%d%d%d", &op, &l, &r, &c); if(op) { belong = (r - 1) / len + 1; printf("%d\n", t[belong].sum + val[r]); } else update(l, r, c); } return 0; }

 

最新回复(0)