Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define N 111111
- #define INF 1000000000
- struct cell{
- int l,r,max,add,count;
- } a[4*N];
- void build(int x,int l,int r){
- a[x].l=l;a[x].r=r;a[x].max=a[x].add=0;
- a[x].count = r - l + 1;
- if (a[x].l<a[x].r){
- build(x+x,l,(l+r)>>1);
- build(x+x+1,1+((l+r)>>1),r);
- }
- }
- int get_max(int x,int sumadd=0){
- return a[x].max+a[x].add+sumadd;
- }
- void modify(int x,int l,int r,int val){
- if (!x || a[x].r < l || a[x].l > r) return;
- if (l<=a[x].l && r>=a[x].r){
- a[x].add+=val;
- return;
- }
- modify(x+x,l,r,val);
- modify(x+x+1,l,r,val);
- if (get_max(x + x) == get_max(x + x + 1)) {
- a[x].max = get_max(x + x);
- a[x].count = a[x + x].count + a[x + x + 1].count;
- }
- if (get_max(x + x) < get_max(x + x + 1)) {
- a[x].max = get_max(x + x + 1);
- a[x].count = a[x + x + 1].count;
- }
- if (get_max(x + x) > get_max(x + x + 1)) {
- a[x].max = get_max(x + x);
- a[x].count = a[x + x].count;
- }
- }
- pair<int, int> findmax(int x,int l,int r,int sumadd){
- if (!x || a[x].r < l || a[x].l > r) return make_pair(-INF, 0);
- if (l<=a[x].l && r>=a[x].r) return make_pair(get_max(x,sumadd), a[x].count);
- if (findmax(x + x, l, r, sumadd + a[x].add) == findmax(x + x + 1, l, r, sumadd + a[x].add))
- return make_pair(findmax(x + x, l, r, sumadd + a[x].add).first,
- findmax(x + x, l, r, sumadd + a[x].add).second + findmax(x + x + 1, l, r, sumadd + a[x].add).second);
- else return max(findmax(x + x, l, r, sumadd + a[x].add), findmax(x + x + 1, l, r, sumadd + a[x].add));
- }
- int main(){
- // freopen("in","r",stdin);
- // freopen("out","w",stdout);
- int n,q;
- cin>>n>>q;
- build(1,1,n);
- while (q--){
- int t,l,r,val; cin >> t;
- if (t==1){
- cin>>l>>r>>val;
- modify(1,l,r,val);
- }else{
- cin>>l>>r;
- cout<<findmax(1,l,r,0).first << " " << findmax(1, l, r, 0).second <<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment