-
Notifications
You must be signed in to change notification settings - Fork 1
31-g0rnn #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฐ๋จธ์ ํผํฌ ์ด๋ก (Ballmer's Peak Theory)์ ํ๋ก๊ทธ๋๋จธ์ ํ์ค ์์ฝ ๋๋๊ฐ 0.129% ์ 0.138% ์ฌ์ด์ผ ๋ ์ด์ธ์ ์ธ ์ฝ๋ฉ ์ค๋ ฅ์ ๋ฐํํ๋ค๋ ์ด๋ก ์ด๋ค.
์ ๋ ์ ํ์ฅํ๊ณ ์ฝ๋ฉํ๋์ฃผ์
ใด๋ใ
.
์ ๋๋๊ฑฐ ใ
๊ฐ๋ค์ใ
.
์ฅ๋์ด๊ตฌ์. ์ค๋๋ ์ฌ๋ฐ๋ ์ธ๊ทธ๋จผํธํธ๋ฆฌ ๋ฌธ์ ๋ฅผ ๋ค๊ณ ์์ฃผ์
จ๊ตฐ์.
์ ๋ฒ์ ํ๋ ์ธ๊ทธ๋จผํธ ๊ตฌ์กฐ๊ฐ ์์ง ์ฒดํ๋์ง์์์.. ์ด์ ๋ฌธ์ ๋ฅผ ์กฐ๊ธ ์ปจ๋ ํ์ต๋๋ค.
๋ง์
์ ํญ๋ฑ์์ 0์ด๋ผ๋ ์ ๊ณผ ๊ณฑ์
์ ํญ๋ฑ์์ 1์ด๋ผ๋ ์ ๋ง ์ฃผ์ํ๋ฉด์ ์ฝ๋ฉ์ ํ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ X์ ๋ค์ด๊ฐ ์ซ์๋ค์ 1,0,-1๋ก ๋ณ๊ฒฝํด๋๋ ์์ด๋์ด๋ ์ค์ํ ๊ฒ ๊ฐ์ต๋๋ค.
์ฌ์ ํ ์ธ๊ทธ๋จผํธ๋ ์ด๋ ต๋ค์.
๋ค์ ๋ฌธ์ ๋ก ๋์ค๋ฉด ์ง์ง์ง์ง ๋ง์ถฐ๋ณด๊ฒ ์ต๋๋ค.
#include <iostream>
#include <vector>
using namespace std;
vector<int> X;
vector<int> tree;
int makeTree(int start, int end, int node) {
if (start == end) return tree[node] = X[start];
int mid = (start + end) / 2;
return tree[node] = makeTree(start, mid, node * 2) *
makeTree(mid + 1, end, node * 2 + 1);
}
void updateTree(int start, int end, int node, int index, int V) {
if (index < start || index > end) return;
if (start == end) {
tree[node] = V;
return;
}
int mid = (start + end) / 2;
updateTree(start, mid, node * 2, index, V);
updateTree(mid + 1, end, node * 2 + 1, index, V);
tree[node] = tree[node * 2] * tree[node * 2 + 1];
}
int solution(int start, int end, int left, int right, int node) {
if (end < left || right < start) return 1;
if (left <= start && end <= right) return tree[node];
int mid = (start + end) / 2;
return solution(start, mid, left, right, node * 2) *
solution(mid + 1, end, left, right, node * 2 + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int N, K;
while (cin >> N >> K) {
X.assign(N + 1, 0);
for (int i = 1; i <= N; i++) {
int temp;
cin >> temp;
if (temp == 0) X[i] = 0;
else if (temp > 0) X[i] = 1;
else X[i] = -1;
}
tree.assign(4 * N + 4, 1);
makeTree(1, N, 1);
vector<char> answer;
while (K--) {
char command;
cin >> command;
if (command == 'C') {
int i, V;
cin >> i >> V;
if (V > 0) V = 1;
else if (V < 0) V = -1;
updateTree(1, N, 1, i, V);
} else if (command == 'P') {
int i, j;
cin >> i >> j;
int temp = solution(1, N, i, j, 1);
if (temp == 0) answer.push_back('0');
else if (temp > 0) answer.push_back('+');
else answer.push_back('-');
}
}
for (char c : answer) cout << c;
cout << '\n';
}
return 0;
}| def minimize(x): | ||
| if x > 0: | ||
| return 1 | ||
| elif x < 0: | ||
| return -1 | ||
| else: | ||
| return 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ด๊ฑฐ ํจ์ํ ํ๋ ค๋ค๊ฐ ๋ง์๋๋ฐ... ๊ตฟ์ ๋๋ค.
kokeunho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋๋ฌด ์ด๋ ต์๋๋ค...
์ ๋ฒ์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ ๋ฐฐ์ ๊ธด ํ์ง๋ง
์์ง ํผ์ ๊ตฌํ์ ์ด๋ ต๋ค์
๊ทธ๋์ ์๋ ์ฝ๋๊ฐ ๊ฑฐ์ ์ ์ฝ๋๋ ์๋ ์์ค์
๋๋ค..
๊ทธ๋ฆฌ๊ณ ๋ค์ด์ค๋ ๋ช
๋ น์ด C๋ P๋์ ๋ฐ๋ผ์
๊ทธ ๋ค์ ๊ฐ์ด ๋ณ๊ฒฝํ ๊ฐ์ธ์ง j(์ธ๋ฑ์ค)์ธ์ง๋ก ๋๋์ด์
๊ตฌํ์์ ๋ํ
์ผํ ๋ถ๋ถ์ ์ ๊ฒฝ์จ์ผํด์ ๋ ์ด๋ ค์ ์ต๋๋ค.
์ธ๊ทธ๋จผํธ ํธ๋ฆฌ ๋ ์ฐ์ตํด์ ์์ ํ ์ฒด๋ํด์ผ๊ฒ ์ต๋๋ค
์๊ณ ํ์
จ์ต๋๋ค.
Details
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N, K;
static int[] tree;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
StringBuilder sb = new StringBuilder();
while (true) {
line = br.readLine();
if (line == null || line.isEmpty()) break;
StringTokenizer st = new StringTokenizer(line);
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken());
tree = new int[4 * N];
init(1, 0, N-1, arr);
StringBuilder lineSb = new StringBuilder();
for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
String cmd = st.nextToken();
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken());
if (cmd.equals("C")) {
update(1, 0, N-1, a, b);
} else {
int response = query(1, 0, N-1, a, b-1);
if (response > 0) lineSb.append('+');
else if (response < 0) lineSb.append('-');
else lineSb.append('0');
}
}
sb.append(lineSb).append("\n");
}
System.out.print(sb);
}
static void init(int node, int start, int end, int[] arr) {
if (start == end) {
tree[node] = Integer.compare(arr[start], 0);
} else {
int mid = (start + end) / 2;
init(node*2, start, mid, arr);
init(node*2+1, mid+1, end, arr);
tree[node] = tree[node*2] * tree[node*2+1];
}
}
static void update(int node, int start, int end, int idx, int val) {
if (idx < start || idx > end) return;
if (start == end) {
tree[node] = Integer.compare(val, 0);
} else {
int mid = (start + end) / 2;
update(node*2, start, mid, idx, val);
update(node*2+1, mid+1, end, idx, val);
tree[node] = tree[node*2] * tree[node*2+1];
}
}
static int query(int node, int start, int end, int left, int right) {
if (right < start || end < left) return 1;
if (left <= start && end <= right) return tree[node];
int mid = (start + end) / 2;
return query(node*2, start, mid, left, right) * query(node*2+1, mid+1, end, left, right);
}
}
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ป๊ฒ๋ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ฅผ ์์ฐ๊ณ ์... ์ด๋ฒ์๋ Sqrt Decomposition ๊ธฐ๋ฒ์ ๋ค๊ณ ์์ต๋๋ค. ๊ทธ๋ฐ๋ฐ ๊ณฑ ์ฒ๋ฆฌ๊ฐ ๊ณจ๋๋ ค์ ์ด๋ป๊ฒ ํ์ง... ํ๋ค๊ฐ GPTํํ ํํธ ์ป๊ณ ํด๊ฒฐํ๋ค์.
์ค์ ์ฐ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ์ ํ์ ์์ด [0์ด๋ -1์ด๋ 1์ด๋] ํ๋จ๋ง ํ ์ ์์ผ๋ฉด ๋๋, ์ ์ด๋ถํฐ ๋ฐ์ดํฐ๋ฅผ -1/0/1๋ก ์์ถํด์ ๋ค๊ณ ์๋๋ก ํฉ๋๋ค. ๋ํ ์ฐ์ฐ์ ์ํฅ์ ์ฃผ๋ -1๊ณผ 0 ๊ฐ์๋ฅผ ๋ค๊ณ ์๋๋ก ํฉ๋๋ค.
inline int Comp(ll x) {
if(x > 0) {
return 1;
} else if(x < 0) {
return -1;
} else {
return 0;
}
}
void Init(int& B, int& NB) {
X.resize(N);
B = max(1, (int)sqrt(N)); // ๋ธ๋ก ํฌ๊ธฐ
NB = (N + B - 1) / B; // ๋ธ๋ก ๊ฐ์
zeroCnts.assign(NB, 0);
negCnts.assign(NB, 0);
for(int i = 0; i < N; ++i) {
ll x; cin >> x;
X[i] = Comp(x);
int b = i / B;
if(X[i] == 0) {
zeroCnts[b]++; // ํด๋น ๊ตฌ๊ฐ 0 ๊ฐ์ ๋์
} else if(X[i] < 0) {
negCnts[b]++; // ํด๋น ๊ตฌ๊ฐ ์์ ๊ฐ์ ๋์
}
}
}๊ฐ์ ๋ณ๊ฒฝํ๋ ์ฟผ๋ฆฌ๋ ๋จ์ํฉ๋๋ค. ์๋ณธ ๋ฐฐ์ด๊ฐ๊ณผ 0 ๊ฐ์/์์ ๊ฐ์ ๋ฐฐ์ด์ ์ ์ ํ ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋ฉ๋๋ค.
void Change(const int B, int i, ll V) {
int val = Comp(V);
if(X[i] == val) { // ๊ฐ์ผ๋ฉด ๋ณ๊ฒฝํ์ง ์์ต๋๋ค
return;
}
// ๊ธฐ์กด ์ ๋ณด ๋ ๋ฆฌ๊ธฐ
int b = i / B;
if(X[i] == 0) {
zeroCnts[b]--;
} else if(X[i] < 0) {
negCnts[b]--;
}
// ์๋ก์ด ์ ๋ณด ๊ธฐ๋ก
X[i] = val;
if(X[i] == 0) {
zeroCnts[b]++;
} else if(X[i] < 0) {
negCnts[b]++;
}
}๊ณฑ ์ฐ์ฐ์ ๊ตฌ๊ฐ์ ๋ฐ๋ผ ์ ์ ํ ์ฒ๋ฆฌํ๋ฉด ๋๋๋ฐ์, i ~ j ๋ถ๋ถ ๊ตฌ๊ฐ์ด ์ฃผ์ด์ง๋ฉด [์ค์ ๋ฐฐ์ด๋ก ์ฒ๋ฆฌํ ์ผ์ชฝ ์ํฌ๋ฆฌ][ ๋ธ๋ก ๋จ์๋ก ์ฒ๋ฆฌํ ๊ฐ์ด๋ฐ ][์ค์ ๋ฐฐ์ด๋ก ์ฒ๋ฆฌํ ์ค๋ฅธ์ชฝ ์ํฌ๋ฆฌ] 3 ๊ตฌ๊ฐ์ผ๋ก ๋๋ ์ ์ ์ ํ ์ฒ๋ฆฌํ๋ฉด ๋ฉ๋๋ค. ๋ง์ฝ i, j๊ฐ ๊ฐ์ ๋ธ๋ก์ ์ํ๋ค๋ฉด ๊ทธ๊ฒ์ ๋ํ ์์ธ์ฒ๋ฆฌ๋ง ํด์ฃผ๋ฉด ๋์ฃ .
void Product(const int B, const int i, const int j, string& out) {
bool zero = false; int neg = 0;
int bi = i / B, bj = j / B;
if(bi == bj) {
for(int idx = i; idx <= j; ++idx) {
if(X[idx] == 0) { // 0์ด๋ฉด ๊ฒฐ๊ณผ๊ฐ 0์ด๋ ์ฆ์ break
zero = true;
break;
}
if(X[idx] < 0) { // ์์ ๊ฐ์ ์นด์ดํ
neg++;
}
}
if(zero) {
out.push_back('0');
} else { // ์์ ๊ฐ์๊ฐ ํ์๋ฉด -, ์ง์๋ผ๋ฉด +
out.push_back((neg & 1) ? '-' : '+');
}
return;
}
// ์ผ์ชฝ ์ํฌ๋ฆฌ ์์ญ
for(int idx = i; idx < (bi + 1) * B; ++idx) {
if(X[idx] == 0) {
zero = true;
break;
}
if(X[idx] < 0) {
neg++;
}
}
// ๊ฐ์ด๋ฐ ๋ธ๋ก ์์ญ
for(int b = bi + 1; b < bj && !zero; ++b) {
if(zeroCnts[b] > 0) {
zero = true;
break;
}
neg += negCnts[b];
}
// ์ค๋ฅธ์ชฝ ์ํฌ๋ฆฌ ์์ญ
for(int idx = bj * B; idx <= j && !zero; ++idx) {
if(X[idx] == 0) {
zero = true;
break;
}
if(X[idx] < 0) {
neg++;
}
}
if(zero) {
out.push_back('0');
} else {
out.push_back((neg & 1) ? '-' : '+');
}
return;
}์ ์ฒด ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int Comp(ll x) {
if(x > 0) {
return 1;
} else if(x < 0) {
return -1;
} else {
return 0;
}
}
int N, K;
vector<int> X;
vector<int> zeroCnts;
vector<int> negCnts;
void Init(int& B, int& NB) {
X.resize(N);
B = max(1, (int)sqrt(N));
NB = (N + B - 1) / B;
zeroCnts.assign(NB, 0);
negCnts.assign(NB, 0);
for(int i = 0; i < N; ++i) {
ll x; cin >> x;
X[i] = Comp(x);
int b = i / B;
if(X[i] == 0) {
zeroCnts[b]++;
} else if(X[i] < 0) {
negCnts[b]++;
}
}
}
void Change(const int B, int i, ll V) {
int val = Comp(V);
if(X[i] == val) {
return;
}
int b = i / B;
if(X[i] == 0) {
zeroCnts[b]--;
} else if(X[i] < 0) {
negCnts[b]--;
}
X[i] = val;
if(X[i] == 0) {
zeroCnts[b]++;
} else if(X[i] < 0) {
negCnts[b]++;
}
}
void Product(const int B, const int i, const int j, string& out) {
bool zero = false; int neg = 0;
int bi = i / B, bj = j / B;
if(bi == bj) {
for(int idx = i; idx <= j; ++idx) {
if(X[idx] == 0) {
zero = true;
break;
}
if(X[idx] < 0) {
neg++;
}
}
if(zero) {
out.push_back('0');
} else {
out.push_back((neg & 1) ? '-' : '+');
}
return;
}
for(int idx = i; idx < (bi + 1) * B; ++idx) {
if(X[idx] == 0) {
zero = true;
break;
}
if(X[idx] < 0) {
neg++;
}
}
for(int b = bi + 1; b < bj && !zero; ++b) {
if(zeroCnts[b] > 0) {
zero = true;
break;
}
neg += negCnts[b];
}
for(int idx = bj * B; idx <= j && !zero; ++idx) {
if(X[idx] == 0) {
zero = true;
break;
}
if(X[idx] < 0) {
neg++;
}
}
if(zero) {
out.push_back('0');
} else {
out.push_back((neg & 1) ? '-' : '+');
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
while((cin >> N >> K)) {
int B, NB;
Init(B, NB);
string out; out.reserve(K);
while(K--) {
char cmd; cin >> cmd;
if(cmd == 'C') {
int i; ll V; cin >> i >> V;
Change(B, i - 1, V);
} else {
int i, j; cin >> i >> j;
Product(B, i - 1, j - 1, out);
}
}
cout << out << "\n";
}
return 0;
}
wnsmir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i์์ j๊น์ง์ ๊ณฑ์ ๊ตฌํ๋ผ๋๊ฒ์ ๋ณด๊ณ
๊ตฌ๊ฐ์ ์ฌ๋ฌ๋ฒ ๊ณ์ฐํด์ผํ๋ ์ ์์ ๊ทธ๋ฆฌ๊ณ ๋ณ๊ฒฝ์ด ์์ฃผ ์ผ์ด๋๋ค๋ ์ ์์
์ง๋์ฃผ ํ์๋ ์ธ๊ทธ๋จผํธํธ๋ฆฌ๊ฐ ๋ ์ค๋ฅด๋ฉฐ ๋ณต์ต๊ฒธ ์ด๋ฌธ์ ๋ฅผ ๊ณจ๋๊ตฌ๋ ๋ผ๋ ์๊ฐ์ด ์ค์ณ์ง๋๊ฐ์ต๋๋ค.
๋ค์ ํธ๋ฆฌ ๊ตฌํํ๋ ค๋๊น .. ์๋นํ ์ด๋ ต๋ค์
ํธ๋ฆฌ๋ฅผ ์ด์ฉํ๋์ ์ด์ธ์๋ ๊ฐ๋จํ์ต๋๋ค.
๋ค๋ง ์ด์ ์๋ ๋ฆฌํ๋
ธ๋๋ค์ด ํฌํจ๋ ๋ฆฌ์คํธ๋ฅผ ๋ถ๋ชจ๋
ธ๋๋ก ๊ฐ์ฃผํ๋ค๋ฉด ์ด ๋ฌธ์ ์์๋ ๋
ธ๋๋ค์ +1, -1, 0 ์ด๋ ๊ฒ๋ง ์ ์ฅํด๋์์ต๋๋ค.
๋ ธ๋์ ๋ถํธ๊ฐ ์ค์ํ์ง ์์ ์ซ์๋ ์ค์ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ฆ P 1 4 ๋ช ๋ น์ tree[1] ๊ฐ๋ง ์ฝ์ผ๋ฉด ๋ฐ๋ก ๊ทธ ๊ตฌ๊ฐ์ ๊ณฑ์ ์ ์ ์๊ฒ ๋ฉ๋๋ค.
- ์๊ฐ๋ณต์ก๋ == O(log N)
def sign(x):
if x > 0: return 1
elif x < 0: return -1
else: return 0
# ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ ๊ตฌ์ถ
def build(arr, tree, node, start, end):
if start == end:
tree[node] = sign(arr[start])
else:
mid = (start + end) // 2
build(arr, tree, node*2, start, mid)
build(arr, tree, node*2+1, mid+1, end)
tree[node] = tree[node*2] * tree[node*2+1]
# ๋จ์ผ ๊ฐ ๊ฐฑ์
def update(tree, node, start, end, idx, val):
if start == end:
tree[node] = sign(val)
else:
mid = (start + end) // 2
if idx <= mid:
update(tree, node*2, start, mid, idx, val)
else:
update(tree, node*2+1, mid+1, end, idx, val)
tree[node] = tree[node*2] * tree[node*2+1]
# ๊ตฌ๊ฐ ๊ณฑ ์ฟผ๋ฆฌ
def query(tree, node, start, end, l, r):
if r < start or end < l: # ๊ตฌ๊ฐ ๋ฒ์ด๋จ
return 1
if l <= start and end <= r: # ์์ ํ ํฌํจ
return tree[node]
mid = (start + end) // 2
left = query(tree, node*2, start, mid, l, r)
right = query(tree, node*2+1, mid+1, end, l, r)
return left * right
# ๋ฉ์ธ
N, K = map(int, input().split())
number = list(map(int, input().split()))
tree = [1] * (4*N)
build(number, tree, 1, 0, N-1)
result = []
for _ in range(K):
order, I, V = input().split()
I, V = int(I), int(V)
if order == "C":
update(tree, 1, 0, N-1, I-1, V)
else:
res = query(tree, 1, 0, N-1, I-1, V-1)
if res > 0: result.append("+")
elif res < 0: result.append("-")
else: result.append("0")
print("".join(result))
๐ ๋ฌธ์ ๋งํฌ
์์ฃผ ์ฝ๋ฉ
โ๏ธ ์์๋ ์๊ฐ
40m
โจ ์๋ ์ฝ๋
์ด๋์ ๊ฐ ๋ณธ์ ์๋ ์ ํ์ด์ฃ ? ๋ฐฐ์ด์ ํน์ ๊ตฌ๊ฐ์ ๊ณฑํ๊ฑฐ๋, ํน์ idx์ ๊ฐ์ ๋ฐ๊ฟ์ผํ๋ ๋ฌธ์ ์ ๋๋ค.
์ ๋ฒ PR ์ ์ฌ๋ ธ๋ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ์ ๋๋ค. ์ ๋ฒ ๋ฌธ์ ๊ฐ ๋๋ฌด ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ง์ ์ํ ๋ฌธ์ ๊ฐ์๊ธฐ์ ์ข ์์ฌ์์ ๋ค๋ฅธ ๋ฌธ์ ๋ฅผ ๊ฐ์ ธ์๋ณด์์ต๋๋ค. ๊ทผ๋ฐ ์ด๊ฒ๋ ์ข ๊ทธ๋ฐ๋๋์ด๋ค์..ใ
์ด๋ฒ์๋ ์ ์ฒด์ ์ธ ๊ตฌ์กฐ๋ ๊ฐ์ต๋๋ค. ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ๋ฅผ ๋ง๋ค๊ณ ,
updateํ๊ฑฐ๋queryํ๋ ๊ฑฐ์ฃ .๋ฌธ์ ๋ฅผ ํธ๋๋ฐ ์ฝ๊ฐ์ ํ์ ๋๋ฆฌ์๋ฉด ๊ณฑ์ ์ ์ผ์ผ์ด ๋ค ํด์ฃผ๊ธฐ ํ๋๋ 1, 0, -1๋ก ์นํํ ํ ๊ณฑ์ ์ ์ฒ๋ฆฌํ๋๊ฒ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค :)
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ