1. 程式人生 > >LOJ#6277. 數列分塊入門 1

LOJ#6277. 數列分塊入門 1

pre mar max others def 簡單 統計 _id fast

內存限制:256 MiB時間限制:100 ms標準輸入輸出 題目類型:傳統評測方式:文本比較 上傳者: hzwer 提交提交記錄統計討論 1 測試數據

題目描述

給出一個長為 nnn 的數列,以及 nnn 個操作,操作涉及區間加法,單點查值。

輸入格式

第一行輸入一個數字 nnn。

第二行輸入 nnn 個數字,第 iii 個數字為 aia_ia?i??,以空格隔開。

接下來輸入 nnn 行詢問,每行輸入四個數字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔開。

opt=0\mathrm{opt} = 0opt=0,表示將位於 [l,r][l, r][l,r] 的之間的數字都加 ccc。

opt=1\mathrm{opt} = 1opt=1,表示詢問 ara_ra?r?? 的值(lll 和 ccc 忽略)。

輸出格式

對於每次詢問,輸出一行一個數字表示答案。

樣例

樣例輸入

4
1 2 2 3
0 1 3 1
1 0 1 0
0 1 2 2
1 0 2 0

樣例輸出

2
5

數據範圍與提示

對於 100% 100\%100% 的數據,1≤n≤50000,−231≤others 1 \leq n \leq 50000, -2^{31} \leq \mathrm{others}1n50000,2?31??others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans2?31??1。

感覺自己好菜啊,,

這種難度的題都寫不出來QWQ。。

思路比較簡單,對序列的下標進行分塊,維護每個塊加上的元素,

不在整塊內的暴力加

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=1e5+10;
const int INF=1e8+10;
inline char nc()
{
    static char buf[MAXN],*p1=buf,*p2=buf;
    
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++; } inline int read() { char c=nc();int x=0,f=1; while(c<0||c>9){if(c==-)f=-1;c=nc();} while(c>=0&&c<=9){x=x*10+c-0;c=nc();} return x*f; } int N,a[MAXN],tag[MAXN],belong[MAXN],L[MAXN],R[MAXN]; int block; void IntervalAdd(int l,int r,int val) { for(int i=l;i<=min(r,R[l]);i++) a[i]+=val; if(belong[l]!=belong[r]) for(int i=L[r];i<=r;i++) a[i]+=val; for(int i=belong[l]+1;i<=belong[r]-1;i++) tag[i]+=val; } int main() { #ifdef WIN32 freopen("a.in","r",stdin); #else #endif int N=read();block=sqrt(N); for(int i=1;i<=N;i++) a[i]=read(); for(int i=1;i<=N;i++) belong[i]=(i-1)/block+1; for(int i=1;i<=N;i++) L[i]=(belong[i]-1)*block+1; for(int i=1;i<=N;i++) R[i]=belong[i]*block; for(int i=1;i<=N;i++) { int opt=read(),l=read(),r=read(),c=read(); if(opt==0) IntervalAdd(l,r,c); else printf("%d\n",a[r]+tag[belong[r]]); } return 0; }

LOJ#6277. 數列分塊入門 1