#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;
struct node{
bool collision = false;
vector<int> factors;
};
node segtree[4000005];
vector<int> primefactors[1000001];
node merge(node a, node b){
if(a.collision || b.collision)
return node{true, vector<int>()};
if(a.factors.size() == 0)
return b;
if(b.factors.size() == 0)
return a;
vector<int> merged;
int i = 0, j = 0;
while(i < a.factors.size() || j < b.factors.size()){
if(i == a.factors.size()){
merged.push_back(b.factors[j]);
j++;
continue;
}
if(i == b.factors.size()){
merged.push_back(a.factors[i]);
i++;
continue;
}
if(a.factors[i] == b.factors[j]){
return node{true, vector<int>()};
}
if(a.factors[i] < b.factors[j]){
merged.push_back(a.factors[i]);
i++;
} else {
merged.push_back(b.factors[j]);
j++;
}
}
return node{false, merged};
}
void insert(int x, int idx, int a, int b){
if(a + 1 == b){
if(a != x)
return;
if(segtree[idx].factors.size() == 0)
segtree[idx].factors = primefactors[x + 1];
else
segtree[idx].factors = vector<int>();
return;
}
if(x < a || x >= b)
return;
int center = (a + b) / 2;
int l = idx * 2;
int r = idx * 2 + 1;
insert(x, l, a, center);
insert(x, r, center, b);
segtree[idx] = merge(segtree[l], segtree[r]);
}
node query(int l, int r, int idx, int a, int b){
if(l >= b || r <= a)
return node{false, vector<int>()};
vector<int> merged;
if(l <= a && r >= b){
return segtree[idx];
}
int center = (a + b) / 2;
node left = query(l, r, idx * 2, a, center);
node right = query(l, r, idx * 2 + 1, center, b);
return merge(left, right);
}
int main(int, char**){
int n, q;
cin >> n >> q;
for (int i = 2; i < n + 1; i++)
{
if(primefactors[i].size() == 0){
for(int j = i; j < n + 1; j += i){
primefactors[j].push_back(i);
}
}
}
for (int i = 0; i < q; i++)
{
char qy;
cin >> qy;
if(qy == 'S'){
int x;
cin >> x;
insert(x - 1, 1, 0, n);
} else if(qy = 'C'){
int l, r;
cin >> l >> r;
cout << (query(l - 1, r, 1, 0, n).collision ? "DA\n" : "NE\n");
}
}
}
Compilation message
Main.cpp: In function 'node merge(node, node)':
Main.cpp:25:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while(i < a.factors.size() || j < b.factors.size()){
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:25:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while(i < a.factors.size() || j < b.factors.size()){
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:26:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | if(i == a.factors.size()){
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:31:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if(i == b.factors.size()){
| ~~^~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main(int, char**)':
Main.cpp:104:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
104 | } else if(qy = 'C'){
| ~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
149080 KB |
Output is correct |
2 |
Correct |
24 ms |
149088 KB |
Output is correct |
3 |
Correct |
24 ms |
149148 KB |
Output is correct |
4 |
Correct |
25 ms |
149080 KB |
Output is correct |
5 |
Correct |
24 ms |
149080 KB |
Output is correct |
6 |
Correct |
25 ms |
149080 KB |
Output is correct |
7 |
Correct |
22 ms |
149084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
129 ms |
309460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
149080 KB |
Output is correct |
2 |
Correct |
24 ms |
149088 KB |
Output is correct |
3 |
Correct |
24 ms |
149148 KB |
Output is correct |
4 |
Correct |
25 ms |
149080 KB |
Output is correct |
5 |
Correct |
24 ms |
149080 KB |
Output is correct |
6 |
Correct |
25 ms |
149080 KB |
Output is correct |
7 |
Correct |
22 ms |
149084 KB |
Output is correct |
8 |
Runtime error |
129 ms |
309460 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |