Guest User

Untitled

a guest
Apr 30th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. //Tanuj Khattar
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef pair<int,int>   II;
  7. typedef vector< II >      VII;
  8. typedef vector<int>     VI;
  9. typedef vector< VI >    VVI;
  10. typedef long long int   LL;
  11.  
  12. #define PB push_back
  13. #define MP make_pair
  14. #define F first
  15. #define S second
  16. #define SZ(a) (int)(a.size())
  17. #define ALL(a) a.begin(),a.end()
  18. #define SET(a,b) memset(a,b,sizeof(a))
  19.  
  20. #define si(n) scanf("%d",&n)
  21. #define dout(n) printf("%d\n",n)
  22. #define sll(n) scanf("%lld",&n)
  23. #define lldout(n) printf("%lld\n",n)
  24. #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
  25.  
  26. #define TRACE
  27.  
  28. #ifdef TRACE
  29. #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
  30. template <typename Arg1>
  31. void __f(const char* name, Arg1&& arg1){
  32.   cerr << name << " : " << arg1 << std::endl;
  33. }
  34. template <typename Arg1, typename... Args>
  35. void __f(const char* names, Arg1&& arg1, Args&&... args){
  36.   const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
  37. }
  38. #else
  39. #define trace(...)
  40. #endif
  41.  
  42. //FILE *fin = freopen("in","r",stdin);
  43. //FILE *fout = freopen("out","w",stdout);
  44.  
  45. const int N = int(1.35*1e5)+10;
  46. const int SQRT = 400;
  47. LL A[N],dp[N],dp2[N],PS[N],ans[N],dp3[N];
  48. int st[N],top,arr[N],len,st2[N],top2;
  49. VI B[N];
  50. struct query{
  51.   int l,r;
  52. }Q[N];
  53. bool cmp(int i,int j){
  54.   return Q[i].r < Q[j].r;
  55. }
  56. LL get2(int ll,int rr,int n,int L,int len){
  57.   LL ret = 0;assert(len>0);LL mx = -int(1e9);
  58.   for(int i=rr;i>=ll;i--){
  59.     mx = max(mx,A[i]);
  60.     if(mx >= A[arr[len]]){ret += mx*(n + 1 - L);continue;}
  61.     int l = 1, h = len, ans = 0;
  62.     while(l<=h){
  63.       int m = (l+h)/2;
  64.       if(A[arr[m]] >= mx){ans = m;h = m-1;}
  65.       else l = m+1;
  66.     }
  67.     ret += (mx*(arr[ans]-L));
  68.     ret += PS[len]-PS[ans-1];
  69.   }
  70.   return ret;
  71. }
  72. LL get(int l,int r){
  73.   LL ret = 0;top2=0;
  74.   int R = l;
  75.   while(R <= r){
  76.     while(top2 && A[st2[top2]] <= A[R])top2--;
  77.     int lpos = (top2?st2[top2]:l-1);
  78.     dp3[R] = (R -lpos)*A[R] + (top2?dp3[lpos]:0);
  79.     st2[++top2]=R;
  80.     ret+=dp3[R];
  81.     R++;
  82.   }
  83.   return ret;
  84. }
  85. int main()
  86. {
  87.   int n,q;
  88.   si(n);si(q);
  89.   for(int i=1;i<=n;i++)sll(A[i]);
  90.   for(int i=1;i<=q;i++){
  91.     si(Q[i].l);si(Q[i].r);
  92.     B[Q[i].l/SQRT].PB(i);
  93.   }
  94.   for(int i=0;i<N;i++){
  95.     sort(ALL(B[i]),cmp);
  96.     int L = (i+1)*SQRT, R = L;top = len = 0;
  97.     LL add = 0;
  98.     for(int j=0;j<SZ(B[i]);j++){
  99.       int idx = B[i][j];
  100.       int l = Q[idx].l,r = Q[idx].r;
  101.       ans[idx] = get(l,min(r,L-1));
  102.       if(r<L)continue;
  103.       while(R <= r){
  104.         while(top && A[st[top]] <= A[R])top--;
  105.         int lpos = (top?st[top]:L-1);
  106.         dp2[R] = (R -lpos)*A[R] + (top?dp2[lpos]:0);
  107.         st[++top] = R;
  108.         if(!len || A[R]>A[arr[len]]){
  109.           if(len){
  110.             dp[arr[len]] = (R - arr[len])*A[arr[len]];
  111.             PS[len] = PS[len-1] + dp[arr[len]];
  112.           }
  113.           arr[++len]=R;
  114.           dp[R] = (r - R + 1)*A[R];
  115.           PS[len] = PS[len-1] + dp[R];
  116.         }
  117.         add += dp2[R];R++;
  118.       }
  119.       dp[arr[len]] = (r - arr[len] + 1)* A[arr[len]];
  120.       PS[len] = PS[len-1] + dp[arr[len]];
  121.       ans[idx] += add;
  122.       ans[idx] += get2(l,L-1,r,L,len);
  123.     }
  124.   }
  125.   for(int i=1;i<=q;i++)
  126.     lldout(ans[i]);
  127.   return 0;
  128. }
Add Comment
Please, Sign In to add comment