# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
127229 |
2019-07-09T07:06:30 Z |
choikiwon |
Sushi (JOI16_sushi) |
C++17 |
|
1176 ms |
4100 KB |
#include<bits/stdc++.h>
using namespace std;
const int MN = 100010;
const int SQ = 500;
const int BS = (MN + SQ - 1) / SQ;
int N, Q;
int A[MN];
struct Query {
int s, e, p;
};
Query query[MN];
int Xn;
vector<int> X;
unordered_map<int, int> dx;
struct Fenwick {
int n;
vector<int> tree;
void init(int n_) {
n = n_;
tree = vector<int>(n + 1, 0);
}
void upd(int x, int v) {
for(int i = x + 1; i <= n; i += (i & -i)) tree[i] += v;
}
int quer(int a) {
if(a >= n) a = n - 1;
int ret = 0;
for(int i = a + 1; i >= 1; i -= (i & -i)) ret += tree[i];
return ret;
}
int quer(int a, int b) {
return quer(b) - quer(a - 1);
}
};
Fenwick fw[BS], tmp;
vector<int> V[BS];
int S[MN], E[MN], P[MN];
void prop(int bid) {
int l = bid * SQ;
int r = min(N - 1, l + SQ - 1);
vector<int> L;
for(int i = l; i <= r; i++) {
int s = 0, e = A[i], p = -1;
while(s <= e) {
int m = (s + e)>>1;
int t = tmp.quer(m, Xn - 1);
if(t >= fw[bid].quer(m - 1)) {
p = m;
s = m + 1;
}
else e = m - 1;
}
tmp.upd(A[i], 1);
L.push_back(A[i]);
A[i] = p;
}
for(int i = 0; i < L.size(); i++) {
tmp.upd(L[i], -1);
}
for(int i = 0; i < V[bid].size(); i++) {
fw[bid].upd(V[bid][i], -1);
}
V[bid].clear();
}
int proc1(int s, int e, int p) {
int bid = s / SQ;
prop(bid);
for(int i = s; i <= e; i++) if(A[i] > p) swap(A[i], p);
return p;
}
int proc2(int bid, int v) {
int l = bid * SQ;
int r = min(N - 1, l + SQ - 1);
fw[bid].upd(v, 1);
V[bid].push_back(v);
int s = v + 1, e = Xn - 1, p = v;
while(s <= e) {
int m = (s + e)>>1;
int cnt = 0;
for(int i = l; i <= r; i++) if(A[i] >= m) cnt++;
if(fw[bid].quer(m - 1) <= cnt) {
p = m;
s = m + 1;
}
else e = m - 1;
}
return p;
}
int solve(int s, int e, int p) {
if(s / SQ == e / SQ) {
return proc1(s, e, p);
}
else {
p = proc1(s, (s / SQ + 1) * SQ - 1, p);
for(int i = s / SQ + 1; i < e / SQ; i++) {
p = proc2(i, p);
}
p = proc1(e / SQ * SQ, e, p);
return p;
}
}
int main() {
scanf("%d %d", &N, &Q);
for(int i = 0; i < N; i++) {
scanf("%d", &A[i]);
X.push_back(A[i]);
}
for(int i = 0; i < Q; i++) {
int s, e, p; scanf("%d %d %d", &s, &e, &p);
s--; e--;
query[i] = {s, e, p};
X.push_back(p);
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end()) - X.begin());
Xn = X.size();
for(int i = 0; i < Xn; i++) dx[X[i]] = i;
for(int i = 0; i < N; i++) {
A[i] = dx[A[i]];
}
for(int i = 0; i < Q; i++) {
query[i].p = dx[query[i].p];
}
for(int i = 0; i < BS; i++) {
fw[i].init(Xn);
}
tmp.init(Xn);
for(int i = 0; i < Q; i++) {
int s = query[i].s;
int e = query[i].e;
int p = query[i].p;
if(s <= e) {
printf("%d\n", X[ solve(s, e, p) ]);
}
else {
p = solve(s, N - 1, p);
printf("%d\n", X[ solve(0, e, p) ]);
}
}
}
Compilation message
sushi.cpp: In function 'void prop(int)':
sushi.cpp:67:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < L.size(); i++) {
~~^~~~~~~~~~
sushi.cpp:70:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < V[bid].size(); i++) {
~~^~~~~~~~~~~~~~~
sushi.cpp: In function 'int main()':
sushi.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~~
sushi.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
sushi.cpp:128:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int s, e, p; scanf("%d %d %d", &s, &e, &p);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
866 ms |
3932 KB |
Output is correct |
2 |
Correct |
834 ms |
3988 KB |
Output is correct |
3 |
Correct |
822 ms |
4060 KB |
Output is correct |
4 |
Correct |
838 ms |
3960 KB |
Output is correct |
5 |
Correct |
643 ms |
2040 KB |
Output is correct |
6 |
Correct |
635 ms |
1916 KB |
Output is correct |
7 |
Correct |
74 ms |
504 KB |
Output is correct |
8 |
Correct |
75 ms |
504 KB |
Output is correct |
9 |
Correct |
825 ms |
4088 KB |
Output is correct |
10 |
Correct |
802 ms |
4088 KB |
Output is correct |
11 |
Correct |
840 ms |
3960 KB |
Output is correct |
12 |
Correct |
851 ms |
3852 KB |
Output is correct |
13 |
Correct |
732 ms |
2808 KB |
Output is correct |
14 |
Correct |
1176 ms |
4072 KB |
Output is correct |
15 |
Correct |
1132 ms |
4100 KB |
Output is correct |
16 |
Correct |
321 ms |
3832 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
22 ms |
1912 KB |
Time limit exceeded (wall clock) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
866 ms |
3932 KB |
Output is correct |
2 |
Correct |
834 ms |
3988 KB |
Output is correct |
3 |
Correct |
822 ms |
4060 KB |
Output is correct |
4 |
Correct |
838 ms |
3960 KB |
Output is correct |
5 |
Correct |
643 ms |
2040 KB |
Output is correct |
6 |
Correct |
635 ms |
1916 KB |
Output is correct |
7 |
Correct |
74 ms |
504 KB |
Output is correct |
8 |
Correct |
75 ms |
504 KB |
Output is correct |
9 |
Correct |
825 ms |
4088 KB |
Output is correct |
10 |
Correct |
802 ms |
4088 KB |
Output is correct |
11 |
Correct |
840 ms |
3960 KB |
Output is correct |
12 |
Correct |
851 ms |
3852 KB |
Output is correct |
13 |
Correct |
732 ms |
2808 KB |
Output is correct |
14 |
Correct |
1176 ms |
4072 KB |
Output is correct |
15 |
Correct |
1132 ms |
4100 KB |
Output is correct |
16 |
Correct |
321 ms |
3832 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Execution timed out |
22 ms |
1912 KB |
Time limit exceeded (wall clock) |
24 |
Halted |
0 ms |
0 KB |
- |