# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
290590 |
2020-09-04T05:49:41 Z |
Berted |
Teams (IOI15_teams) |
C++14 |
|
1733 ms |
230004 KB |
#include "teams.h"
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#define ll long long
#define pii pair<int, ll>
#define fst first
#define snd second
#define ppp pair<pii, pii>
const int SZ = (1 << 20) + 5, SZ2 = 500005;
using namespace std;
struct p_node
{
int v = 0, l = -1, r = -1;
p_node() {}
};
int n, suf[SZ2], seg[SZ], lazy[SZ];
vector<p_node> p_seg;
vector<int> dt[SZ2];
int p_idx[SZ2];
vector<ppp> ivl;
void p_build(int idx, int L, int R)
{
if (L < R)
{
int M = L + R >> 1;
p_seg.push_back(p_node()); p_seg.push_back(p_node());
p_seg[idx].l = p_seg.size() - 2, p_seg[idx].r = p_seg.size() - 1;
p_build(p_seg[idx].l, L, M);
p_build(p_seg[idx].r, M + 1, R);
}
}
void p_upd(int idx, int odx, int L, int R, int x)
{
if (L <= R)
{
if (L == x && x == R) {p_seg[idx].v = p_seg[odx].v + 1;}
else
{
int M = L + R >> 1;
p_seg.push_back(p_node());
if (x <= M)
{
p_seg[idx].l = p_seg.size() - 1;
p_seg[idx].r = p_seg[odx].r;
p_upd(p_seg[idx].l, p_seg[odx].l, L, M, x);
}
else
{
p_seg[idx].r = p_seg.size() - 1;
p_seg[idx].l = p_seg[odx].l;
p_upd(p_seg[idx].r, p_seg[odx].r, M + 1, R, x);
}
p_seg[idx].v = p_seg[p_seg[idx].l].v + p_seg[p_seg[idx].r].v;
}
}
}
int p_qry(int idx, int L, int R, int l, int r)
{
l = max(L, l); r = min(R, r);
if (l <= r)
{
if (L == l && R == r) {return p_seg[idx].v;}
else
{
int M = L + R >> 1;
return p_qry(p_seg[idx].l, L, M, l, r) + p_qry(p_seg[idx].r, M + 1, R, l, r);
}
}
return 0;
}
inline void prop(int idx, int L, int R)
{
if (L <= R && lazy[idx] != -1)
{
seg[idx] = p_seg[lazy[idx]].v;
if (L < R)
{
lazy[2 * idx] = p_seg[lazy[idx]].l;
lazy[2 * idx + 1] = p_seg[lazy[idx]].r;
}
lazy[idx] = -1;
}
}
void bld(int idx, int L, int R)
{
seg[idx] = 0; lazy[idx] = -1;
if (L < R)
{
int M = L + R >> 1;
bld(2 * idx, L, M);
bld(2 * idx + 1, M + 1, R);
}
}
void upds(int idx, int edx, int L, int R, int l, int r)
{
l = max(L, l); r = min(R, r);
prop(idx, L, R);
if (l <= r)
{
if (L == l && R == r)
{
lazy[idx] = edx;
prop(idx, L, R);
}
else
{
int M = L + R >> 1;
upds(2 * idx, p_seg[edx].l, L, M, l, r);
upds(2 * idx + 1, p_seg[edx].r, M + 1, R, l, r);
seg[idx] = seg[2 * idx] + seg[2 * idx + 1];
}
}
}
void updi(int idx, int L, int R, int x, int v)
{
prop(idx, L, R);
if (L == x && x == R) {seg[idx] += v;}
else
{
int M = L + R >> 1;
if (x <= M) {updi(2 * idx, L, M, x, v);}
else {updi(2 * idx + 1, M + 1, R, x, v);}
seg[idx] = seg[2 * idx] + seg[2 * idx + 1];
}
}
inline void getInterval(int idx, int edx, int L, int R, int l, int r)
{
l = max(L, l); r = min(r, R);
prop(edx, L, R);
if (l <= r)
{
if (L == l && R == r)
{
//cout << "Splitted : " << L << " " << R << "\n";
ivl.push_back({{idx, edx}, {L, R}});
}
else
{
int M = L + R >> 1;
getInterval(p_seg[idx].l, 2 * edx, L, M, l, r);
getInterval(p_seg[idx].r, 2 * edx + 1, M + 1, R, l, r);
}
}
}
inline int findPos(int idx, int edx, int L, int R, int x)
{
prop(edx, L, R);
//cout << "FindPos " << L << " " << R << " " << x << " " << p_seg[idx].v - seg[edx] << "\n";
if (L < R)
{
int M = L + R >> 1;
prop(2 * edx, L, M); prop(2 * edx + 1, M + 1, R);
if (p_seg[p_seg[idx].l].v - seg[2 * edx] >= x) {return findPos(p_seg[idx].l, 2 * edx, L, M, x);}
else {return findPos(p_seg[idx].r, 2 * edx + 1, M + 1, R, x - p_seg[p_seg[idx].l].v + seg[2 * edx]);}
}
else {return L;}
}
int qry(int idx, int edx, int L, int R, int l, int r)
{
l = max(L, l); r = min(r, R);
prop(edx, L, R);
if (l <= r)
{
if (L == l && R == r)
{
return p_seg[idx].v - seg[edx];
}
else
{
int M = L + R >> 1;
return qry(p_seg[idx].l, 2 * edx, L, M, l, r) + qry(p_seg[idx].r, 2 * edx + 1, M + 1, R, l, r);
}
}
return 0;
}
void init(int N, int A[], int B[])
{
n = N;
for (int i = 0; i < n; i++)
{
suf[A[i]]++;
dt[A[i]].push_back(B[i]);
}
p_seg.push_back(p_node());
p_idx[0] = p_seg.size() - 1;
p_build(p_idx[0], 1, n);
for (int i = 1; i <= n; i++)
{
p_idx[i] = p_idx[i - 1];
for (auto &x : dt[i])
{
int nidx = p_seg.size();
p_seg.push_back(p_node());
p_upd(nidx, p_idx[i], 1, n, x);
p_idx[i] = nidx;
}
}
for (int i = n; i >= 0; i--) {suf[i] += suf[i + 1];}
bld(1, 1, n);
}
int can(int M, int K[])
{
//cout << "New " << M << "\n";
upds(1, p_idx[0], 1, n, 1, n);
sort(K, K + M);
for (int i = 0; i < M; i++)
{
if (qry(p_idx[K[i]], 1, 1, n, K[i], n) < K[i]) {return 0;}
else
{
ivl.clear();
getInterval(p_idx[K[i]], 1, 1, n, K[i], n);
int L = K[i], v = K[i];
for (int i = 0; i < ivl.size(); i++)
{
if (p_seg[ivl[i].fst.fst].v - seg[ivl[i].fst.snd] < v)
{
v -= p_seg[ivl[i].fst.fst].v - seg[ivl[i].fst.snd];
L = ivl[i].snd.snd + 1;
}
else {L = findPos(ivl[i].fst.fst, ivl[i].fst.snd, ivl[i].snd.fst, ivl[i].snd.snd, v); break;}
}
//cout << "LOL: " << i << " " << L << " " << qry(p_idx[K[i]], 1, 1, n, K[i], L - 1) << "\n";
updi(1, 1, n, L, K[i] - qry(p_idx[K[i]], 1, 1, n, K[i], L - 1));
upds(1, p_idx[K[i]], 1, n, K[i], L - 1);
}
}
return 1;
}
Compilation message
teams.cpp: In function 'void p_build(int, int, int)':
teams.cpp:32:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int M = L + R >> 1;
| ~~^~~
teams.cpp:34:31: warning: conversion from 'std::vector<p_node>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
34 | p_seg[idx].l = p_seg.size() - 2, p_seg[idx].r = p_seg.size() - 1;
| ~~~~~~~~~~~~~^~~
teams.cpp:34:64: warning: conversion from 'std::vector<p_node>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
34 | p_seg[idx].l = p_seg.size() - 2, p_seg[idx].r = p_seg.size() - 1;
| ~~~~~~~~~~~~~^~~
teams.cpp: In function 'void p_upd(int, int, int, int, int)':
teams.cpp:47:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int M = L + R >> 1;
| ~~^~~
teams.cpp:51:33: warning: conversion from 'std::vector<p_node>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
51 | p_seg[idx].l = p_seg.size() - 1;
| ~~~~~~~~~~~~~^~~
teams.cpp:57:33: warning: conversion from 'std::vector<p_node>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
57 | p_seg[idx].r = p_seg.size() - 1;
| ~~~~~~~~~~~~~^~~
teams.cpp: In function 'int p_qry(int, int, int, int, int)':
teams.cpp:74:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'void bld(int, int, int)':
teams.cpp:100:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
100 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'void upds(int, int, int, int, int, int)':
teams.cpp:119:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
119 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'void updi(int, int, int, int, int)':
teams.cpp:133:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
133 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'void getInterval(int, int, int, int, int, int)':
teams.cpp:153:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
153 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'int findPos(int, int, int, int, int)':
teams.cpp:166:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
166 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'int qry(int, int, int, int, int, int)':
teams.cpp:186:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
186 | int M = L + R >> 1;
| ~~^~~
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:202:26: warning: conversion from 'std::vector<p_node>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
202 | p_idx[0] = p_seg.size() - 1;
| ~~~~~~~~~~~~~^~~
teams.cpp:209:25: warning: conversion from 'std::vector<p_node>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
209 | int nidx = p_seg.size();
| ~~~~~~~~~~^~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:232:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
232 | for (int i = 0; i < ivl.size(); i++)
| ^
teams.cpp:224:11: note: shadowed declaration is here
224 | for (int i = 0; i < M; i++)
| ^
teams.cpp:232:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, long long int>, std::pair<int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
232 | for (int i = 0; i < ivl.size(); i++)
| ~~^~~~~~~~~~~~
teams.cpp:237:25: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
237 | L = ivl[i].snd.snd + 1;
teams.cpp:9:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
9 | #define snd second
teams.cpp:239:50: note: in expansion of macro 'snd'
239 | else {L = findPos(ivl[i].fst.fst, ivl[i].fst.snd, ivl[i].snd.fst, ivl[i].snd.snd, v); break;}
| ^~~
teams.cpp:9:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
9 | #define snd second
teams.cpp:239:82: note: in expansion of macro 'snd'
239 | else {L = findPos(ivl[i].fst.fst, ivl[i].fst.snd, ivl[i].snd.fst, ivl[i].snd.snd, v); break;}
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
10 ms |
12032 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
10 ms |
12160 KB |
Output is correct |
9 |
Correct |
9 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
10 ms |
12160 KB |
Output is correct |
13 |
Correct |
10 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12160 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
9 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
9 ms |
12032 KB |
Output is correct |
19 |
Correct |
8 ms |
12032 KB |
Output is correct |
20 |
Correct |
9 ms |
12032 KB |
Output is correct |
21 |
Correct |
10 ms |
12032 KB |
Output is correct |
22 |
Correct |
9 ms |
12032 KB |
Output is correct |
23 |
Correct |
8 ms |
12032 KB |
Output is correct |
24 |
Correct |
9 ms |
12032 KB |
Output is correct |
25 |
Correct |
8 ms |
12032 KB |
Output is correct |
26 |
Correct |
9 ms |
12032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
40756 KB |
Output is correct |
2 |
Correct |
133 ms |
40752 KB |
Output is correct |
3 |
Correct |
123 ms |
40756 KB |
Output is correct |
4 |
Correct |
125 ms |
41012 KB |
Output is correct |
5 |
Correct |
75 ms |
39624 KB |
Output is correct |
6 |
Correct |
85 ms |
39628 KB |
Output is correct |
7 |
Correct |
74 ms |
39628 KB |
Output is correct |
8 |
Correct |
71 ms |
39736 KB |
Output is correct |
9 |
Correct |
124 ms |
38828 KB |
Output is correct |
10 |
Correct |
89 ms |
38852 KB |
Output is correct |
11 |
Correct |
74 ms |
40008 KB |
Output is correct |
12 |
Correct |
72 ms |
38828 KB |
Output is correct |
13 |
Correct |
82 ms |
40008 KB |
Output is correct |
14 |
Correct |
93 ms |
39880 KB |
Output is correct |
15 |
Correct |
112 ms |
40588 KB |
Output is correct |
16 |
Correct |
112 ms |
40624 KB |
Output is correct |
17 |
Correct |
79 ms |
39880 KB |
Output is correct |
18 |
Correct |
86 ms |
40080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
41008 KB |
Output is correct |
2 |
Correct |
134 ms |
41012 KB |
Output is correct |
3 |
Correct |
412 ms |
43952 KB |
Output is correct |
4 |
Correct |
123 ms |
41020 KB |
Output is correct |
5 |
Correct |
143 ms |
40040 KB |
Output is correct |
6 |
Correct |
138 ms |
39864 KB |
Output is correct |
7 |
Correct |
79 ms |
39880 KB |
Output is correct |
8 |
Correct |
128 ms |
39860 KB |
Output is correct |
9 |
Correct |
124 ms |
38868 KB |
Output is correct |
10 |
Correct |
186 ms |
38852 KB |
Output is correct |
11 |
Correct |
195 ms |
39980 KB |
Output is correct |
12 |
Correct |
207 ms |
38856 KB |
Output is correct |
13 |
Correct |
320 ms |
40364 KB |
Output is correct |
14 |
Correct |
455 ms |
44080 KB |
Output is correct |
15 |
Correct |
230 ms |
42556 KB |
Output is correct |
16 |
Correct |
227 ms |
42488 KB |
Output is correct |
17 |
Correct |
162 ms |
41584 KB |
Output is correct |
18 |
Correct |
191 ms |
41564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
907 ms |
225152 KB |
Output is correct |
2 |
Correct |
885 ms |
225044 KB |
Output is correct |
3 |
Correct |
1641 ms |
225044 KB |
Output is correct |
4 |
Correct |
900 ms |
225048 KB |
Output is correct |
5 |
Correct |
569 ms |
215968 KB |
Output is correct |
6 |
Correct |
550 ms |
216092 KB |
Output is correct |
7 |
Correct |
417 ms |
215964 KB |
Output is correct |
8 |
Correct |
518 ms |
215968 KB |
Output is correct |
9 |
Correct |
704 ms |
216768 KB |
Output is correct |
10 |
Correct |
653 ms |
216600 KB |
Output is correct |
11 |
Correct |
686 ms |
219676 KB |
Output is correct |
12 |
Correct |
751 ms |
220448 KB |
Output is correct |
13 |
Correct |
1165 ms |
222876 KB |
Output is correct |
14 |
Correct |
1733 ms |
230004 KB |
Output is correct |
15 |
Correct |
979 ms |
229652 KB |
Output is correct |
16 |
Correct |
993 ms |
229912 KB |
Output is correct |
17 |
Correct |
661 ms |
223760 KB |
Output is correct |
18 |
Correct |
634 ms |
223688 KB |
Output is correct |