From f584924bafc62b8d6cd2f1d11201208c5cd2152d Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sun, 18 Aug 2024 16:39:13 +0800 Subject: [PATCH] update --- day14/P3372/1.ans | 3 + day14/P3372/1.in | 7 +++ day14/P3372/P3372.cpp | 122 +++++++++++++++++++++++++++++++++++++++++ day14/P3372/P3372.json | 7 +++ 4 files changed, 139 insertions(+) create mode 100644 day14/P3372/1.ans create mode 100644 day14/P3372/1.in create mode 100644 day14/P3372/P3372.cpp create mode 100644 day14/P3372/P3372.json diff --git a/day14/P3372/1.ans b/day14/P3372/1.ans new file mode 100644 index 0000000..76be83c --- /dev/null +++ b/day14/P3372/1.ans @@ -0,0 +1,3 @@ +11 +8 +20 \ No newline at end of file diff --git a/day14/P3372/1.in b/day14/P3372/1.in new file mode 100644 index 0000000..4f8c701 --- /dev/null +++ b/day14/P3372/1.in @@ -0,0 +1,7 @@ +5 5 +1 5 4 2 3 +2 2 4 +1 2 3 2 +2 3 4 +1 1 5 1 +2 1 4 \ No newline at end of file diff --git a/day14/P3372/P3372.cpp b/day14/P3372/P3372.cpp new file mode 100644 index 0000000..da24f62 --- /dev/null +++ b/day14/P3372/P3372.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +using namespace std; + +template +class SegTree{ +public: + SegTree(int arr[],int n); + void add(int l,int r,int c); + int search(int l,int r); +private: + int n; + int t[MAX*4+1]; + int l[MAX*4+1]; +private: + void build(int s,int e,int now,int arr[]); + void update(int s,int e,int now,int l,int r,int c); + int getSum(int s,int e,int now,int l,int r); +}; +int readInt(); + +const int MAX_N=1e5+5; +int arr[MAX_N]; + +int main(){ + const int n=readInt(),m=readInt(); + for(int i=1;i<=n;i++){ + arr[i]=readInt(); + } + SegTree s(arr,n); + for(int i=1;i<=m;i++){ + const int z=readInt(),x=readInt(),y=readInt(); + if(z==1){ + const int k=readInt(); + s.add(x, y, k); + }else{ + cout< +int SegTree::getSum(int s,int e,int now,int l,int r){ + if(l<=s&&e<=r){ + return t[now]; + } + int m=(e-s)/2+s; + if(this->l[now]){ + t[now*2]+=this->l[now]*(m-s+1); + t[now*2+1]+=this->l[now]*(e-(m+1)+1); + this->l[now*2]+=this->l[now]; + this->l[now*2+1]+=this->l[now]; + this->l[now]=0; + } + int sum=0; + if(l<=m)sum=getSum(s, m, now, l, r); + if(m+1<=r)sum+=getSum(m+1, e, now, l, r); + return sum; +} + +template +int SegTree::search(int l,int r){ + return getSum(1, n, 1, l, r); +} + +template +void SegTree::update(int s,int e,int now,int l,int r,int c){ + if(l<=s&&e<=r){ + t[now]+=(e-s+1)*c; + this->l[now]+=c; + return; + } + int m=(e-s)/2+s; + if(this->l[now]&&s!=e){ + t[now*2]+=this->l[now]*(m-s+1); + t[now*2+1]+=this->l[now]*(e-(m+1)+1); + this->l[now*2]+=this->l[now]; + this->l[now*2+1]+=this->l[now]; + this->l[now]=0; + } + if(l<=m)update(s, m, now, l, r, c); + if(m+1<=r)update(m+1, e, now, l, r, c); + t[now]=t[now*2]+t[now*2+1]; +} + +template +void SegTree::add(int l,int r,int c){ + update(1, n, 1, l, r, c); +} + +template +void SegTree::build(int s,int e,int now,int arr[]){ + if(s==e){ + t[now]=arr[s]; + return; + } + int m=s+(e-s)/2; + build(s, m, now*2, arr); + build(m+1, e, now*2+1, arr); + t[now]=t[now*2]+t[now*2+1]; +} + +template +SegTree::SegTree(int arr[],int n):n(n){ + build(1, n, 1, arr); +} \ No newline at end of file diff --git a/day14/P3372/P3372.json b/day14/P3372/P3372.json new file mode 100644 index 0000000..06c0c80 --- /dev/null +++ b/day14/P3372/P3372.json @@ -0,0 +1,7 @@ +{ + "files":["P3372.cpp"], + "args":["-Wall","-g"], + "tests":[ + {"in":"1.in","ans":"1.ans"} + ] +} \ No newline at end of file