이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
using namespace std;
struct segtree
{
int N;
vector<int> tree;
int upd(int index, int b, int e, int l, int r)
{
if (l <= b && e <= r)
return ++tree[index];
if (r <= b || e <= l)
return tree[index];
return upd(index * 2, b, (e + b) / 2, l, r) +
upd(index * 2 + 1, (e + b) / 2, e, l, r);
}
void upd(int l, int r)
{
upd(1, 0, N, l, r);
}
int query(int index, int b, int e, int pos, int cary)
{
tree[index] += cary;
if (pos = b || e <= pos)
return 0;
if (e - b == 1)
return tree[index];
return query(index * 2, b, (e + b) / 2, pos, tree[index]) +
query(index * 2 + 1, (e + b) / 2, e, pos, tree[index]);
}
int query(int pos)
{
return query(1, 0, N, pos, 0);
}
} tree;
long long count_swaps(std::vector<int> s)
{
tree.N = s.size();
tree.tree.resize(tree.N * 4 + 5);
vector<int> posadd(s.size());
vector<bool> sorted(s.size(), false);
vector<queue<int>> pleft(s.size() + 1), pright(s.size() + 1);
iota(posadd.begin(), posadd.end(), 0);
long long numswaps = 0;
for (int i = 0; i < s.size(); i++)
(s[i] > 0 ? pright : pleft)[abs(s[i])].push(i);
for (int e, i = 0; i < s.size(); i++)
{
if (sorted[i])
continue;
(s[i] > 0 ? pright : pleft)[abs(s[i])].pop();
e = (s[i] < 0 ? pright : pleft)[abs(s[i])].front();
(s[i] < 0 ? pright : pleft)[abs(s[i])].pop();
sorted[i] = sorted[e] = true;
numswaps += (e + tree.query(e)) - (i + tree.query(i)) - 1;
tree.upd(i + 1, e);
if (s[i] > 0)
numswaps++;
}
return numswaps;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In member function 'int segtree::query(int, int, int, int, int)':
shoes.cpp:29:11: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
29 | if (pos = b || e <= pos)
| ~~~~^~~~~~~~~~~~~~~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:56:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
shoes.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int e, i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |