답안 #199543

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
199543 2020-02-02T02:15:50 Z arnold518 역사적 조사 (JOI14_historical) C++14
40 / 100
4000 ms 6000 KB
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int SQ = 300;

int N, Q, A[MAXN+10], B[MAXN+10], S;
vector<int> comp;

struct Query
{
	int l, r, p;
	bool operator < (const Query &p)
	{
		if(l/SQ==p.l/SQ) return r<p.r;
		return l/SQ<p.l/SQ;
	}
};

Query query[MAXN+10];

ll tree[MAXN*4+10], ans[MAXN+10];
void update(int node, int tl, int tr, int pos, ll val)
{
	while(tl!=tr)
	{
		int mid=tl+tr>>1;
		if(pos<=mid) node=node*2, tr=mid;
		else node=node*2+1, tl=mid+1;
	}
	tree[node]+=val;
	while(node!=1)
	{
		node/=2;
		tree[node]=max(tree[node*2], tree[node*2+1]);
	}
}
ll qquery() { return tree[1]; }

void push(int x) { update(1, 1, S, B[x], A[x]); }
void pop(int x) { update(1, 1, S, B[x], -A[x]); }

int main()
{
	int i, j;

	scanf("%d%d", &N, &Q);
	for(i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end()); S=comp.size();
	for(i=1; i<=N; i++) B[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1;
	for(i=1; i<=Q; i++) scanf("%d%d", &query[i].l, &query[i].r), query[i].p=i;
	sort(query+1, query+Q+1);

	int l=1, r=1; push(1);
	for(i=1; i<=Q; i++)
	{
		while(r<query[i].r) r++, push(r);
		while(l>query[i].l) l--, push(l);
		while(r>query[i].r) pop(r), r--;
		while(l<query[i].l) pop(l), l++;
		ans[query[i].p]=qquery();
	}
	for(i=1; i<=Q; i++) printf("%lld\n", ans[i]);
}

Compilation message

historic.cpp: In function 'void update(int, int, int, int, ll)':
historic.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid=tl+tr>>1;
           ~~^~~
historic.cpp: In function 'int main()':
historic.cpp:53:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
historic.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &Q);
  ~~~~~^~~~~~~~~~~~~~~~
historic.cpp:56:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
                      ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
historic.cpp:60:61: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=Q; i++) scanf("%d%d", &query[i].l, &query[i].r), query[i].p=i;
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 380 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 8 ms 376 KB Output is correct
4 Correct 12 ms 504 KB Output is correct
5 Correct 23 ms 632 KB Output is correct
6 Correct 32 ms 504 KB Output is correct
7 Correct 36 ms 504 KB Output is correct
8 Correct 24 ms 504 KB Output is correct
9 Correct 25 ms 504 KB Output is correct
10 Correct 47 ms 632 KB Output is correct
11 Correct 48 ms 632 KB Output is correct
12 Correct 47 ms 632 KB Output is correct
13 Correct 46 ms 632 KB Output is correct
14 Correct 46 ms 632 KB Output is correct
15 Correct 45 ms 632 KB Output is correct
16 Correct 27 ms 632 KB Output is correct
17 Correct 13 ms 632 KB Output is correct
18 Correct 46 ms 632 KB Output is correct
19 Correct 49 ms 632 KB Output is correct
20 Correct 53 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 8 ms 376 KB Output is correct
7 Correct 11 ms 504 KB Output is correct
8 Correct 13 ms 632 KB Output is correct
9 Correct 21 ms 932 KB Output is correct
10 Correct 15 ms 1140 KB Output is correct
11 Correct 74 ms 3948 KB Output is correct
12 Correct 48 ms 1648 KB Output is correct
13 Correct 76 ms 2416 KB Output is correct
14 Correct 103 ms 4204 KB Output is correct
15 Correct 131 ms 5616 KB Output is correct
16 Correct 95 ms 5232 KB Output is correct
17 Correct 46 ms 2416 KB Output is correct
18 Correct 71 ms 3824 KB Output is correct
19 Correct 81 ms 6000 KB Output is correct
20 Correct 52 ms 4336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 760 KB Output is correct
2 Correct 247 ms 1400 KB Output is correct
3 Correct 577 ms 1784 KB Output is correct
4 Correct 1062 ms 2304 KB Output is correct
5 Correct 1521 ms 2804 KB Output is correct
6 Correct 1845 ms 3304 KB Output is correct
7 Correct 1678 ms 3568 KB Output is correct
8 Correct 1188 ms 4080 KB Output is correct
9 Correct 792 ms 4592 KB Output is correct
10 Correct 262 ms 4720 KB Output is correct
11 Correct 1111 ms 4664 KB Output is correct
12 Correct 2827 ms 4676 KB Output is correct
13 Execution timed out 4086 ms 3696 KB Time limit exceeded
14 Halted 0 ms 0 KB -