Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define ll long long
- #define ld long double
- #define f first
- #define s second
- #define mp make_pair
- const int maxn = 1e6 + 1, maxm = 24, maxk = 61;
- ll inf = 1000000000, mod = inf + 7, mod2 = 998244353;
- const ll pa = 997, pa2 = 113, block = 400;
- using namespace std;
- int n, q;
- int t[maxn * 4], t2[maxn * 4], lp[maxn], was[maxn], l[maxn], r[maxn];
- set<int> setik[maxn];
- void upd(int pos, int val, int v = 1, int tl = 1, int tr = n){
- if (tl == tr){
- t[v] = val;
- return;
- }
- int tm = (tl + tr) / 2;
- if (pos <= tm)
- upd(pos, val, v + v, tl, tm);
- else
- upd(pos, val, v + v + 1, tm + 1, tr);
- t[v] = min(t[v + v], t[v + v + 1]);
- }
- void upd2(int pos, int val, int v = 1, int tl = 1, int tr = n){
- if (tl == tr){
- t2[v] = val;
- return;
- }
- int tm = (tl + tr) / 2;
- if (pos <= tm)
- upd2(pos, val, v + v, tl, tm);
- else
- upd2(pos, val, v + v + 1, tm + 1, tr);
- t2[v] = max(t2[v + v], t2[v + v + 1]);
- }
- int get(int l, int r, int v = 1, int tl = 1, int tr = n){
- if (l <= tl && tr <= r)
- return t[v];
- if (l > tr || r < tl)
- return inf;
- int tm = (tl + tr) / 2;
- return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
- }
- int get2(int l, int r, int v = 1, int tl = 1, int tr = n){
- if (l <= tl && tr <= r)
- return t2[v];
- if (l > tr || r < tl)
- return 0;
- int tm = (tl + tr) / 2;
- return max(get2(l, r, v + v, tl, tm), get2(l, r, v + v + 1, tm + 1, tr));
- }
- int main(){
- // ios_base::sync_with_stdio(false);
- // cin.tie(0), cout.tie(0);
- for (int i = 2; i <= maxn - 1; ++i){
- for (int j = i; j <= maxn - 1; j += i){
- if (lp[j] == 0){
- lp[j] = i;
- }
- }
- }
- cin >> n >> q;
- for (int i = 1; i <= n; ++i){
- l[i] = -1;
- r[i] = inf;
- upd(i, r[i]);
- upd2(i, l[i]);
- }
- for (int i = 1; i <= q; ++i){
- char c;
- cin >> c;
- if (c == 'S'){
- int x;
- cin >> x;
- if (was[x] == 0){
- vector<int> v;
- int x2 = x;
- while (x2 != 1){
- int y = lp[x2];
- v.pb(y);
- while (x2 % y == 0){
- x2 /= y;
- }
- }
- l[x] = -1;
- r[x] = inf;
- for (auto j : v){
- auto it = setik[j].upper_bound(x);
- if (it != setik[j].end()){
- r[x] = min(r[x], *it);
- }
- if (it != setik[j].begin()){
- it--;
- l[x] = max(l[x], *it);
- }
- setik[j].insert(x);
- }
- upd(x, r[x]);
- upd2(x, l[x]);
- was[x] = 1;
- }
- else{
- vector<int> v;
- int x2 = x;
- while (x2 != 1){
- int y = lp[x2];
- v.pb(y);
- while (x2 % y == 0){
- x2 /= y;
- }
- }
- for (auto p : v){
- setik[p].erase(x);
- }
- if (l[x] != -1){
- v.clear();
- x2 = l[x];
- while (x2 != 1){
- int y = lp[x2];
- v.pb(y);
- while (x2 % y == 0){
- x2 /= y;
- }
- }
- r[x] = inf;
- for (auto j : v){
- auto it = setik[j].upper_bound(x);
- if (it != setik[j].end()){
- r[x] = min(r[x], *it);
- }
- }
- upd(x, r[x]);
- }
- if (r[x] != inf){
- v.clear();
- x2 = l[x];
- while (x2 != 1){
- int y = lp[x2];
- v.pb(y);
- while (x2 % y == 0){
- x2 /= y;
- }
- }
- l[x] = -1;
- for (auto j : v){
- auto it = setik[j].upper_bound(x);
- if (it != setik[j].begin()){
- it--;
- l[x] = max(l[x], *it);
- }
- }
- upd2(x, l[x]);
- }
- }
- }
- else{
- int l, r;
- cin >> l >> r;
- int z = get(l, r), w = get2(l, r);
- if (z <= r || w >= l){
- cout << "DA\n";
- }
- else{
- cout << "NE\n";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment