Submission #199553

# Submission time Handle Problem Language Result Execution time Memory
199553 2020-02-02T02:36:08 Z arnold518 역사적 조사 (JOI14_historical) C++14
100 / 100
1935 ms 7720 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 = 500;

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<p.l;
	}
};

Query query[MAXN+10];

ll tree[MAXN*4+10], ans[MAXN+10];
void update(int pos, ll val)
{
	int node=pos+S-1;
	tree[node]+=val;
	while(node!=1)
	{
		node>>=1;
		tree[node]=max(tree[node*2], tree[node*2+1]);
	}
}
ll qquery() { return tree[1]; }

void push(int x) { update(B[x], A[x]); }
void pop(int x) { update(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();
	while(__builtin_popcount(S)!=1) S++;
	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 'int main()':
historic.cpp:48:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
historic.cpp:50: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:51: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:56: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;
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory 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 380 KB Output is correct
5 Correct 6 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 380 KB Output is correct
13 Correct 5 ms 380 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 504 KB Output is correct
3 Correct 8 ms 376 KB Output is correct
4 Correct 11 ms 376 KB Output is correct
5 Correct 18 ms 504 KB Output is correct
6 Correct 26 ms 632 KB Output is correct
7 Correct 25 ms 504 KB Output is correct
8 Correct 21 ms 632 KB Output is correct
9 Correct 22 ms 632 KB Output is correct
10 Correct 34 ms 632 KB Output is correct
11 Correct 33 ms 632 KB Output is correct
12 Correct 33 ms 632 KB Output is correct
13 Correct 34 ms 632 KB Output is correct
14 Correct 31 ms 632 KB Output is correct
15 Correct 31 ms 632 KB Output is correct
16 Correct 23 ms 632 KB Output is correct
17 Correct 14 ms 632 KB Output is correct
18 Correct 30 ms 632 KB Output is correct
19 Correct 33 ms 760 KB Output is correct
20 Correct 36 ms 632 KB Output is correct
# Verdict Execution time Memory 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 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 7 ms 504 KB Output is correct
8 Correct 10 ms 632 KB Output is correct
9 Correct 18 ms 1016 KB Output is correct
10 Correct 15 ms 1140 KB Output is correct
11 Correct 71 ms 3952 KB Output is correct
12 Correct 39 ms 1676 KB Output is correct
13 Correct 59 ms 2288 KB Output is correct
14 Correct 84 ms 3824 KB Output is correct
15 Correct 115 ms 5488 KB Output is correct
16 Correct 76 ms 4728 KB Output is correct
17 Correct 41 ms 2496 KB Output is correct
18 Correct 65 ms 3824 KB Output is correct
19 Correct 70 ms 5616 KB Output is correct
20 Correct 43 ms 3828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 760 KB Output is correct
2 Correct 116 ms 1272 KB Output is correct
3 Correct 270 ms 1784 KB Output is correct
4 Correct 432 ms 2240 KB Output is correct
5 Correct 576 ms 2836 KB Output is correct
6 Correct 659 ms 3160 KB Output is correct
7 Correct 604 ms 3568 KB Output is correct
8 Correct 505 ms 4208 KB Output is correct
9 Correct 390 ms 4588 KB Output is correct
10 Correct 245 ms 4720 KB Output is correct
11 Correct 533 ms 4720 KB Output is correct
12 Correct 898 ms 4672 KB Output is correct
13 Correct 1387 ms 4792 KB Output is correct
14 Correct 1777 ms 5288 KB Output is correct
15 Correct 1925 ms 7720 KB Output is correct
16 Correct 1851 ms 7600 KB Output is correct
17 Correct 1935 ms 7536 KB Output is correct
18 Correct 1875 ms 7376 KB Output is correct
19 Correct 1850 ms 7408 KB Output is correct
20 Correct 1755 ms 7408 KB Output is correct
21 Correct 1705 ms 7308 KB Output is correct
22 Correct 1716 ms 7280 KB Output is correct
23 Correct 1746 ms 7336 KB Output is correct
24 Correct 1795 ms 7192 KB Output is correct
25 Correct 228 ms 7288 KB Output is correct