QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#248877 | #6656. 先人类的人类选别 | zzpcd | RE | 0ms | 0kb | C++17 | 2.7kb | 2023-11-11 22:24:15 | 2023-11-11 22:24:15 |
answer
#include<bits/stdc++.h>
using namespace std;
const int MN = 5e5 + 5;
using ll = long long;
int n, m, a[MN];
int num[MN << 1], N;
struct que{
int x, l, r;
}q[MN];
namespace zkw{
struct Node{
ll v;
int num;
int ls, rs;
}T[MN * 20];
int N, rt[MN];
void add(int& x, int y, int l, int r, int pos) {
if(!x) x = ++N;
if(l == r) {
T[x] = T[y];
T[x].num++;
T[x].v += num[pos];
return;
}
int mid = l + r >> 1;
if(pos <= mid)
add(T[x].ls, T[y].ls, l, mid, pos),
T[x].rs = T[y].rs;
else
add(T[x].rs, T[y].rs, mid + 1, r, pos),
T[x].ls = T[y].ls;
T[x].num = T[T[x].ls].num + T[T[x].rs].num;
T[x].v = T[T[x].ls].v + T[T[x].rs].v;
}
void add2(int& x, int l, int r, int pos) {
if(!x) x = ++N;
if(l == r) {
T[x].num++;
T[x].v += num[pos];
return;
}
int mid = l + r >> 1;
if(pos <= mid)
add2(T[x].ls, l, mid, pos);
else
add2(T[x].rs, mid + 1, r, pos);
T[x].num = T[T[x].ls].num + T[T[x].rs].num;
T[x].v = T[T[x].ls].v + T[T[x].rs].v;
}
ll query(int x, int y, int l, int r, int k) {
if(l == r) {
return (ll)k * num[l];
}
int mid = l + r >> 1;
if(k <= T[T[x].rs].num + T[T[y].rs].num)
return query(T[x].rs, T[y].rs, mid + 1, r, k);
return T[T[x].rs].v + T[T[y].rs].v + query(T[x].ls, T[y].ls, l, mid, k - (T[T[x].rs].num + T[T[y].rs].num));
}
}
using zkw::rt;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i];
num[i] = a[i];
}
N = n + m;
for(int i = 1; i <= m; i++) {
cin >> q[i].x >> q[i].l >> q[i].r;
num[i + n] = q[i].x;
}
sort(num + 1, num + 1 + N);
N = unique(num + 1, num + 1 + N) - num - 1;
for(int i = 1; i <= n; i++)
zkw::add(rt[i], rt[i - 1], 1, N, lower_bound(num + 1, num + 1 + N, a[i]) - num);
for(int i = 1; i <= m; i++) {
auto [x, l, r] = q[i];
zkw::add2(rt[n + 1], 1, N, lower_bound(num + 1, num + 1 + N, x) - num);
// cerr << zkw::query(rt[1], rt[n + 1], 1, N, 1) << "\n";
// cerr << zkw::T[rt[1]].v << "\n";
cout << zkw::query(rt[r], rt[n + 1], 1, N, r) - zkw::query(rt[l - 1], rt[n + 1], 1, N, l - 1) << "\n";
}
}
/*
6 8
1 6 1 3 5 4
2 3 6
3 3 4
2 4 4
6 3 5
4 1 1
4 2 3
2 4 6
1 3 3
6 1
1 6 1 3 5 4
2 1 6
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
499998 499999 225823 463012 151183 217174 254116 87516 430524 123887 1361 369166 194997 386501 152639 163995 82309 151761 31404 226965 394185 101304 79621 99315 150936 156155 31208 399526 358496 220267 192773 347327 92807 441843 423834 173327 131913 369822 399380 107225 354942 357415 472293 22050 24...
output:
30690660976 47414891120 105487455953 22358372158 20975402746 6257771014 31201467488 115331181834 9124930293 7824102632 2146273518 51745314301 53146730142 23687230091 120607771274 1050423809 72818807434 38847698680 5378878564 27350934969 7121225405 11453494649 23342470451 58986381247 8361770541 35351...