Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1791 Accepted Submission(s): 772
Problem Description
In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for $a_l,a_{l+1}...a_r$
2. query l r: query $\sum_{i=l}^r \lfloor a_i / b_i \rfloor$
Input
There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form ‘add l r‘ or ‘query l r‘, representing an operation.
$1 \leq n,q \leq 100000$, $1 \leq l \leq r \leq n$, there‘re no more than 5 test cases.
Output
Output the answer for each ‘query‘, each one line.
Sample Input
5 12
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
Sample Output
1
1
2
4
4
6
分析:线段树模板改一改,维护最大值最小值就好了。
#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <map> #define range(i,a,b) for(auto i=a;i<=b;++i) #define LL long long #define itrange(i,a,b) for(auto i=a;i!=b;++i) #define rerange(i,a,b) for(auto i=a;i>=b;--i) #define fill(arr,tmp) memset(arr,tmp,sizeof(arr)) using namespace std; int b[int(1e5+5)],n,q; template <class T> class segtree{ private: T *add,*cnt,*minb,*maxa; void pushup(int rt){ minb[rt]=min(minb[rt<<1],minb[rt<<1|1]); cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1]; maxa[rt]=max(maxa[rt<<1],maxa[rt<<1|1]); } void pushdown(int rt){ if(add[rt]){ int v=add[rt]; add[rt]=0; maxa[rt<<1]+=v; maxa[rt<<1|1]+=v; add[rt<<1]+=v; add[rt<<1|1]+=v; } } public: explicit segtree(int len=int(1e5+5)){ add=new T[len<<2];fill(add,0); cnt=new T[len<<2];fill(cnt,0); minb=new T[len<<2];fill(minb,0); maxa=new T[len<<2];fill(maxa,0); } void build(int l,int r,int rt){ add[rt]=0; if(l==r){ cnt[rt]=maxa[rt]=0; minb[rt]=b[l]; return; } int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1); pushup(rt); } void update(int L,int R,T c,int l,int r,int rt){ if(L<=l&&r<=R){ maxa[rt]++; if(maxa[rt]<minb[rt]){ ++add[rt]; return; } if(l==r&&maxa[rt]>=minb[rt]){ ++cnt[rt]; minb[rt]+=b[l]; return; } } pushdown(rt); int m=(l+r)>>1; if(L<=m)update(L,R,0,l,m,rt<<1); if(m<R)update(L,R,0,m+1,r,rt<<1|1); pushup(rt); } T query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R)return cnt[rt]; int m=(l+r)>>1; pushdown(rt); T ret=0; if(L<=m)ret+=query(L,R,l,m,rt<<1); if(m<R)ret+=query(L,R,m+1,r,rt<<1|1); return ret; } }; segtree<int>tree; void init(){} void solve(){ while(cin>>n>>q){ range(i,1,n)scanf("%d",b+i); tree.build(1,n,1); char op[6];int l,r; while(q--){ scanf("%s%d%d",op,&l,&r); if(op[0]==‘a‘)tree.update(l,r,0,1,n,1); else printf("%d\n",tree.query(l,r,1,n,1)); } } } int main() { init(); solve(); return 0; }
原文地址:https://www.cnblogs.com/Rhythm-/p/9375066.html