Water Problem

water problem

发布时间: 2015年10月10日 15:34   时间限制: 1000ms   内存限制: 256M

描述

题意很简单

给你N个数, Q个查询

每次查询给你一个区间[L, R]

你要找出 [L, R] 这个区间里面取模M后的最大值。

输入

第一行一个T,表示测试数据组数。
第二行两个整数N, M (1<=N<=10^5, 1<=M<=10^9)。
第三行给你N个整数 整数范围在1到10^9之间。
第四行给你一个整数Q. ( 1<=Q<=10^5)
下面Q行, 每一行对应两个整数L, R (1<=L<=R<10^9)

输出

每一行对应一个询问的答案。

样例输入1
复制

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

样例输出1

2
2

这是一道线段树模板题。

注意到每组测试数据中的模数只有一个,所以可以每次先读入所有数字,并对M取模,之后再建立线段树回答区间最大值即可。

事实上,M的存在大概就是唬人的。

 1 #include <cstdio>
 2
 3 inline int max(const int &a, const int &b) {
 4     return a > b ? a : b;
 5 }
 6
 7 inline int min(const int &a, const int &b) {
 8     return a < b ? a : b;
 9 }
10
11 inline int nextChar() {
12     static const int siz = 1 << 22;
13     static char buf[siz];
14     static char *head = buf + siz;
15     static char *tail = buf + siz;
16
17     if (head == tail)
18         fread(head = buf, 1, siz, stdin);
19
20     return int(*head++);
21 }
22
23 inline int nextInt() {
24     register int ret = 0;
25     register int chr = nextChar();
26
27     for (; chr < 48; chr = nextChar());
28     for (; chr > 47; chr = nextChar())
29         ret = ret * 10 + chr - 48;
30
31     return ret;
32 }
33
34 const int mxn = 100005;
35 const int mxm = 2000005;
36
37 int n, m, p, seq[mxn], nod[mxm];
38
39 void build(int t, int l, int r) {
40     if (l == r)
41         nod[t] = seq[l];
42     else {
43         int a = t << 1;
44         int b = t << 1 | 1;
45         int c = (l + r) >> 1;
46
47         build(a, l, c);
48         build(b, c + 1, r);
49
50         nod[t] = max(nod[a], nod[b]);
51     }
52 }
53
54 int query(int t, int l, int r, int x, int y) {
55     if (l == x && y == r)
56         return nod[t];
57     else {
58         int a = t << 1;
59         int b = t << 1 | 1;
60         int c = (l + r) >> 1;
61
62         if (y <= c)
63             return query(a, l, c, x, y);
64         else if (x > c)
65             return query(b, c + 1, r, x, y);
66         else
67             return max(
68                 query(a, l, c, x, c),
69                 query(b, c + 1, r, c + 1, y)
70             );
71     }
72 }
73
74 signed main() {
75     for (register int cas = nextInt(); cas--; ) {
76         n = nextInt();
77         p = nextInt();
78
79         for (register int i = 1; i <= n; ++i)
80             seq[i] = nextInt() % p;
81
82         m = nextInt();
83
84         build(1, 1, n);
85
86         for (register int i = 1; i <= m; ++i) {
87             int l = nextInt();
88             int r = nextInt();
89             printf("%d\n", query(1, 1, n, l, r));
90         }
91     }
92 }
时间: 2024-10-03 10:48:41

Water Problem的相关文章

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

dutacm.club Water Problem(矩阵快速幂)

Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:1228   Accepted:121 [Submit][Status][Discuss] Description 函数 f:Z+→Z .已知 f(1),f(2) 的值,且对于任意 x>1,有 f(x+1)=f(x)+f(x?1)+sin(πx2) . 求 f(n) 的

HDU 4974 A simple water problem(贪心)

HDU 4974 A simple water problem 题目链接 签到题,很容易贪心得到答案是(sum + 1) / 2和ai最大值的最大值 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; typedef long long ll; int t, n; ll a, Max, sum; int main(

HDU - 4974 A simple water problem

Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or 1 for each competitor and add it to the total score

HDOJ 4974 A simple water problem

A simple water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 173    Accepted Submission(s): 112 Problem Description Dragon is watching competitions on TV. Every competition is held be

2014多校第十场1004 || HDU 4974 A simple water problem

题目链接 题意 : n支队伍,每场两个队伍表演,有可能两个队伍都得一分,也可能其中一个队伍一分,也可能都是0分,每个队伍将参加的场次得到的分数加起来,给你每个队伍最终得分,让你计算至少表演了几场. 思路 : ans = max(maxx,(sum+1)/2) :其实想想就可以,如果所有得分中最大值没有和的一半大,那就是队伍中一半一半对打,否则的话最大的那个就都包了. 1 #include <cstdio> 2 #include <cstring> 3 #include <st

ACDream 1734 Can you make a water problem?(贪心)

Can you make a water problem? Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Losanto want to make a water problem. But he have no idea….Then he thought a problem: A b

HDU 5443 The Water Problem

The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 153    Accepted Submission(s): 123 Problem Description In Land waterless, water is a very limited resource. People always figh

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is