f(l, r) = max(al,al+1...ar), g(l, r) = min(al, al+1...ar)

How can i calc ∑∑f(l,r) * g(l,r) mod 1e9 + 7?

# | User | Rating |
---|---|---|

1 | tourist | 3619 |

2 | Um_nik | 3493 |

3 | ecnerwala | 3446 |

4 | Radewoosh | 3383 |

5 | ksun48 | 3357 |

6 | yosupo | 3324 |

7 | Benq | 3299 |

8 | maroonrk | 3243 |

9 | apiadu | 3238 |

10 | Petr | 3217 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 207 |

2 | Monogon | 196 |

3 | SecondThread | 195 |

4 | vovuh | 188 |

5 | pikmike | 186 |

5 | Um_nik | 186 |

7 | antontrygubO_o | 185 |

8 | Ashishgup | 182 |

9 | pashka | 169 |

10 | Radewoosh | 167 |

f(l, r) = max(al,al+1...ar), g(l, r) = min(al, al+1...ar)

How can i calc ∑∑f(l,r) * g(l,r) mod 1e9 + 7?

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/30/2020 13:25:04 (g2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Use a stack to find the l & r for each 'i' such that l <= i <= r and f(l, r) = a[i], and store these in an array 'big'. Do the same thing for each 'i' such that g(l, r) = a[i], and store these in an array 'small'. Both of these tasks can be done in O(N).

Let's try to find ∑ g(l, r). This can be done in O(N) by iterating through small and finding the sum of ∑ (a[i] * (small[i].r — small[i].l + 1)). It should be easy to understand why this is the case. Let's call this sum 'sum_g'.

Our answer turns out to be ∑ (a[i] * sum_g * (large[i].r — large[i].l + 1)). Of course, you should be careful with mods when calculating each quantity.