#P12558. [UOI 2024] Heroes and Monsters

[UOI 2024] Heroes and Monsters

题目描述

There are nn heroes and nn monsters. The heroes and monsters are numbered with integers from 11 to nn. The strength of the ii-th hero is equal to aia_i, and the strength of the ii-th monster is equal to bib_i. It is guaranteed that all values a1,a2,,an,b1,b2,,bna_1, a_2, \ldots, a_n, b_1, b_2, \ldots, b_n are pairwise distinct.

There will be a total of nn battles. In each battle, exactly one hero and exactly one monster will participate, with each hero and each monster participating in exactly one battle. Let the battle involve the hero with number ii and the monster with number jj. If ai>bja_i>b_j, then the hero with number ii will be happy, otherwise, he will be sad.

Let anskans_k be the number of different sets of heroes SS of size kk, such that there is a distribution of battles where all heroes in SS will be happy and all other heroes will be sad.

Given qq queries of the form ll, rr. For each query, find (i=lransi)mod998244353(\sum\limits_{i=l}^{r} ans_i) \mod 998244353.

输入格式

The first line contains a single integer nn (1n51031 \leq n \leq 5 \cdot 10^3) --- the number of battles that will take place.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai2n1 \leq a_i \leq 2 \cdot n) --- the strengths of the heroes.

The third line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n (1bi2n)(1 \leq b_i \leq 2 \cdot n) --- the strengths of the monsters.

It is guaranteed that all values a1,a2,,an,b1,b2,,bna_1, a_2, \ldots, a_n, b_1, b_2, \ldots, b_n are pairwise distinct.

The fourth line contains a single integer qq (1qn+1)(1 \leq q \leq n+1) --- the number of queries.

The next qq lines contain two integers each, ll and rr (0lrn)(0 \leq l \leq r \leq n) --- the parameters of the corresponding query.

It is guaranteed that all values a1,a2,,an,b1,b2,,bna_1, a_2, \ldots, a_n, b_1, b_2, \ldots, b_n are pairwise distinct.

输出格式

For each query, output a single integer --- the required value (i=lransi)mod998244353(\sum\limits_{i=l}^{r} ans_i) \mod 998244353.

3
3 4 6
1 2 5
3
1 2
2 3
3 3
2
3
1

提示

The image below shows the heroes and monsters of the first example. The heroes are at the top, and the monsters are at the bottom. The number inside the square represents the strength of the corresponding hero or monster.

In the example, there are three possible sets of happy heroes: {1,2,3}\{1,2,3\}, {2,3}\{2,3\}, and {1,3}\{1,3\}. Below are three possible distributions of battles in which the corresponding sets of heroes will be happy. Note that there may be multiple distributions of battles where the same set of heroes will be happy.

Scoring

  • (33 points): ai<bja_i < b_j for 1i,jn1 \leq i,j \leq n;
  • (99 points): q=1q = 1, l=1l = 1, r=1r = 1;
  • (66 points): ai=2i1a_i = 2 \cdot i - 1, bi=2ib_i = 2 \cdot i for 1in1 \leq i \leq n;
  • (1616 points): n500n \leq 500, q=1q = 1, l=0l = 0, r=nr = n;
  • (1414 points): q=1q = 1, l=0l = 0, r=nr = n;
  • (1515 points): q=1q = 1, l=rl=r;
  • (1717 points): n500n \leq 500;
  • (2020 points): without additional restrictions.