记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 5ms 4.25 MiB
#2 Accepted 12ms 4.336 MiB
#3 Accepted 15ms 6.375 MiB
#4 Accepted 31ms 6.324 MiB
#5 Accepted 759ms 9.434 MiB
#6 Accepted 780ms 9.383 MiB
#7 Accepted 761ms 9.375 MiB
#8 Accepted 833ms 9.395 MiB
#9 Accepted 794ms 9.473 MiB
#10 Accepted 800ms 9.477 MiB

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
void read(T &x){
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
	if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
	x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x){
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
const int N = 500005, M = 100005;
int n, m, cur, ans[M], tr[N];
struct query {
    int id, l, r, k;
} q[M], tmp[M];
struct element {
    int pos, val;
    bool operator < (const element &obj) const{
	return val < obj.val;
    }
} b[N];
void add(int p, int x){
    while(p <= n) tr[p] += x, p += p & -p;
}
int ask(int p){
    int ret = 0;
    while(p) ret += tr[p], p -= p & -p;
    return ret;
}
void solve(int l, int r, int ql, int qr){
    if(ql > qr) return;
    if(l == r){
	for(int i = ql; i <= qr; i++)
	    ans[q[i].id] = b[l].val;
	return;
    }
    int mid = (l + r) >> 1, cnt = 0;
    for(int i = l; i <= mid; i++)
	add(b[i].pos, 1);
    for(int i = ql; i <= qr; i++)
	if(ask(q[i].r) - ask(q[i].l - 1) >= q[i].k) cnt++;
    for(int i = ql, p1 = ql, p2 = ql + cnt; i <= qr; i++){
	int sum = ask(q[i].r) - ask(q[i].l - 1);
	if(ask(q[i].r) - ask(q[i].l - 1) >= q[i].k) tmp[p1++] = q[i];
	else q[i].k -= sum, tmp[p2++] = q[i];
    }
    for(int i = ql; i <= qr; i++)
	q[i] = tmp[i];
    for(int i = l; i <= mid; i++)
	add(b[i].pos, -1);
    solve(l, mid, ql, ql + cnt - 1);
    solve(mid + 1, r, ql + cnt, qr);
}
int main(){
    read(n), read(m);
    for(int i = 1; i <= n; i++)
	read(b[i].val), b[i].pos = i;
    sort(b + 1, b + n + 1);
    for(int i = 1; i <= m; i++)
	q[i].id = i, read(q[i].l), read(q[i].r), read(q[i].k);
    solve(1, n, 1, m);
    for(int i = 1; i <= m; i++)
	write(ans[i]), enter;
    return 0;
}

信息

递交者
类型
递交
比赛
24OI Poinsonous Round (Test)
语言
C++
递交时间
2017-12-05 16:06:47
评测时间
2017-12-05 16:06:47
评测机
分数
100
总耗时
4794ms
峰值内存
9.477 MiB