#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll sz = 100000000;
char in_buf[sz];
char out_buf[sz];
inline char read_next()
{
static ll i = sz;
if (i == sz)
{
fread(in_buf, 1, sz, stdin);
i = 0;
}
return in_buf[i++];
}
inline ll read_int()
{
ll r = read_next() - '0';
char ch;
while ((ch = read_next()) >= '0' && ch <= '9')
r = r * 10 + ch - '0';
return r;
}
ll w_idx;
inline void write_next(char ch)
{
if (w_idx == sz)
{
fwrite(out_buf, 1, sz, stdout);
w_idx = 0;
}
out_buf[w_idx++] = ch;
}
inline void write_int(ll x)
{
char s[10];
ll i = 0;
do {
s[i++] = x % 10;
x /= 10;
} while (x > 0);
i--;
while (i >= 0)
write_next(s[i--] + '0');
}
#define mid ((l+r)>>1)
struct query{
int l, r, id;
bool operator < (const query &o) const{
return make_pair(l>>9, r) < make_pair(o.l >> 9, o.r);
}
}q[100100];
int n, m, cnt;
ll x[100100], tree[400400], ans[100100];
map<ll, int> mp;
void update(int id, ll val, int node = 1, int l = 1, int r = cnt){
if(id < l || r < id) return;
if(l == r){
tree[node] += val;
return;
}
update(id, val, node*2, l, mid), update(id, val, node*2+1, mid+1, r);
tree[node] = max(tree[node*2], tree[node*2+1]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> x[i], mp[x[i]] = 0;
for(auto &x : mp) x.second = ++cnt;
for(int i=1;i<=m;i++) cin >> q[i].l >> q[i].r, q[i].id = i;
sort(q+1, q+1+m);
int l = 1, r = 0;
for(int i=1;i<=m;i++){
while(l > q[i].l) l--, update(mp[x[l]], x[l]);
while(r < q[i].r) r++, update(mp[x[r]], x[r]);
while(l < q[i].l) update(mp[x[l]], -x[l]), l++;
while(r > q[i].r) update(mp[x[r]], -x[r]), r--;
ans[q[i].id] = tree[1];
}
for(int i=1;i<=m;i++) cout << ans[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
8 ms |
384 KB |
Output is correct |
3 |
Correct |
16 ms |
384 KB |
Output is correct |
4 |
Correct |
31 ms |
504 KB |
Output is correct |
5 |
Correct |
69 ms |
608 KB |
Output is correct |
6 |
Correct |
96 ms |
512 KB |
Output is correct |
7 |
Correct |
114 ms |
512 KB |
Output is correct |
8 |
Correct |
73 ms |
512 KB |
Output is correct |
9 |
Correct |
75 ms |
512 KB |
Output is correct |
10 |
Correct |
167 ms |
640 KB |
Output is correct |
11 |
Correct |
162 ms |
640 KB |
Output is correct |
12 |
Correct |
155 ms |
760 KB |
Output is correct |
13 |
Correct |
156 ms |
760 KB |
Output is correct |
14 |
Correct |
148 ms |
640 KB |
Output is correct |
15 |
Correct |
149 ms |
640 KB |
Output is correct |
16 |
Correct |
88 ms |
608 KB |
Output is correct |
17 |
Correct |
28 ms |
512 KB |
Output is correct |
18 |
Correct |
146 ms |
640 KB |
Output is correct |
19 |
Correct |
172 ms |
760 KB |
Output is correct |
20 |
Correct |
170 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
8 ms |
512 KB |
Output is correct |
8 |
Correct |
13 ms |
768 KB |
Output is correct |
9 |
Correct |
38 ms |
1272 KB |
Output is correct |
10 |
Correct |
13 ms |
768 KB |
Output is correct |
11 |
Correct |
61 ms |
3448 KB |
Output is correct |
12 |
Correct |
51 ms |
1152 KB |
Output is correct |
13 |
Correct |
91 ms |
2296 KB |
Output is correct |
14 |
Correct |
144 ms |
6136 KB |
Output is correct |
15 |
Correct |
172 ms |
8696 KB |
Output is correct |
16 |
Correct |
210 ms |
11000 KB |
Output is correct |
17 |
Correct |
40 ms |
1912 KB |
Output is correct |
18 |
Correct |
65 ms |
3320 KB |
Output is correct |
19 |
Correct |
124 ms |
11768 KB |
Output is correct |
20 |
Correct |
94 ms |
10080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
760 KB |
Output is correct |
2 |
Correct |
651 ms |
1308 KB |
Output is correct |
3 |
Correct |
1578 ms |
1924 KB |
Output is correct |
4 |
Correct |
2777 ms |
2804 KB |
Output is correct |
5 |
Correct |
3987 ms |
3448 KB |
Output is correct |
6 |
Execution timed out |
4072 ms |
2424 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |