Submission #20584

#TimeUsernameProblemLanguageResultExecution timeMemory
20584kdh9949역사적 조사 (JOI14_historical)C++14
100 / 100
2966 ms6804 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int sz = 131072, b = 320;
int n, q, a[100010], xx[100010];
ll ans[100010];

struct Query{
	int s, e, idx;
	bool operator<(const Query &oth) const {
		return (s / b == oth.s / b) ? e < oth.e : s < oth.s;
	}
} qa[100010];

struct Seg{
	ll dat[2 * sz];
	void init(){
		for(int i = 1; i < 2 * sz; i++) dat[i] = 0;
	}
	void upd(int x, int v){
		x += sz - 1; dat[x] += v * xx[x - sz + 1];
		for(x /= 2; x; x /= 2) dat[x] = max(dat[2 * x], dat[2 * x + 1]);
	}
	ll get(){ return dat[1]; }
} S;

int main(){
	scanf("%d%d", &n, &q);
	for(int i = 1; i <= n; i++){ scanf("%d", a + i); xx[i] = a[i]; }
	sort(xx + 1, xx + n + 1);
	for(int i = 1; i <= n; i++) a[i] = (int)(lower_bound(xx + 1, xx + n + 1, a[i]) - xx);
    for(int i = 1; i <= q; i++){ scanf("%d%d", &qa[i].s, &qa[i].e); qa[i].idx = i; }
    sort(qa + 1, qa + q + 1);
    qa[0].s = 1; qa[0].e = 0;
    S.init();
    for(int i = 1; i <= q; i++){
		int ls = qa[i - 1].s, le = qa[i - 1].e;
		int cs = qa[i].s, ce = qa[i].e;
		for(; ls < cs; ) S.upd(a[ls++], -1);
		for(; ls > cs; ) S.upd(a[--ls], 1);
		for(; le < ce; ) S.upd(a[++le], 1);
		for(; le > ce; ) S.upd(a[le--], -1);
		ans[qa[i].idx] = S.get();
    }
    for(int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}

Compilation message (stderr)

historic.cpp: In function 'int main()':
historic.cpp:29:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &q);
                       ^
historic.cpp:30:49: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= n; i++){ scanf("%d", a + i); xx[i] = a[i]; }
                                                 ^
historic.cpp:33:67: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= q; i++){ scanf("%d%d", &qa[i].s, &qa[i].e); qa[i].idx = i; }
                                                                   ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...