Tranvick

RSQ 2

Feb 3rd, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. #define N 111111
  7. #define INF 1000000000
  8. struct cell{
  9.     int l,r,max,add,count;
  10. } a[4*N];
  11.  
  12. void build(int x,int l,int r){
  13.     a[x].l=l;a[x].r=r;a[x].max=a[x].add=0;
  14.     a[x].count = r - l + 1;
  15.     if (a[x].l<a[x].r){
  16.         build(x+x,l,(l+r)>>1);
  17.         build(x+x+1,1+((l+r)>>1),r);
  18.     }
  19. }
  20.  
  21. int get_max(int x,int sumadd=0){
  22.     return a[x].max+a[x].add+sumadd;
  23. }
  24.  
  25. void modify(int x,int l,int r,int val){
  26.     if (!x || a[x].r < l || a[x].l > r) return;
  27.     if (l<=a[x].l && r>=a[x].r){
  28.         a[x].add+=val;
  29.         return;
  30.     }
  31.     modify(x+x,l,r,val);
  32.     modify(x+x+1,l,r,val);
  33.     if (get_max(x + x) == get_max(x + x + 1)) {
  34.         a[x].max = get_max(x + x);
  35.         a[x].count = a[x + x].count + a[x + x + 1].count;
  36.     }
  37.     if (get_max(x + x) < get_max(x + x + 1)) {
  38.         a[x].max = get_max(x + x + 1);
  39.         a[x].count = a[x + x + 1].count;
  40.     }
  41.     if (get_max(x + x) > get_max(x + x + 1)) {
  42.         a[x].max = get_max(x + x);
  43.         a[x].count = a[x + x].count;
  44.     }
  45. }
  46.  
  47. pair<int, int> findmax(int x,int l,int r,int sumadd){
  48.     if (!x || a[x].r < l || a[x].l > r) return make_pair(-INF, 0);
  49.     if (l<=a[x].l && r>=a[x].r) return make_pair(get_max(x,sumadd), a[x].count);
  50.     if (findmax(x + x, l, r, sumadd + a[x].add) == findmax(x + x + 1, l, r, sumadd + a[x].add))
  51.         return make_pair(findmax(x + x, l, r, sumadd + a[x].add).first,
  52.                  findmax(x + x, l, r, sumadd + a[x].add).second + findmax(x + x + 1, l, r, sumadd + a[x].add).second);
  53.     else return max(findmax(x + x, l, r, sumadd + a[x].add), findmax(x + x + 1, l, r, sumadd + a[x].add));
  54. }
  55.  
  56. int main(){
  57. //  freopen("in","r",stdin);
  58. //  freopen("out","w",stdout);
  59.    
  60.     int n,q;
  61.     cin>>n>>q;
  62.    
  63.     build(1,1,n);
  64.     while (q--){
  65.         int t,l,r,val; cin >> t;
  66.         if (t==1){
  67.             cin>>l>>r>>val;
  68.             modify(1,l,r,val);
  69.         }else{
  70.             cin>>l>>r;
  71.             cout<<findmax(1,l,r,0).first << " " << findmax(1, l, r, 0).second <<endl;
  72.         }
  73.     }
  74.    
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment