题目链接
思路:
单纯的线段树区间修改和区间最值查询,只不过需要建立四个线段树来分别存储。(ind数组存储素数对应线段树的标号,刚开始开了数组长度为5,然后存了素数7对应的线段树下标,即ind[7],找了n年bug…)
code:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define debug(x) cout << #x << ":" << x << endl;
#define bug cout << "********" << endl;
#define ios ios::sync_with_stdio(false), cin.tie(0);
#define fi first
#define se second
#define lson node << 1
#define rson node << 1 | 1
using namespace std;
typedef long long ll;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int tree[maxn << 2][5];
int lazy[maxn << 2][5];
int ind[10], n, t;
void pushup(int node,int d)
{
tree[node][d] = max(tree[lson][d], tree[rson][d]);
}
void pushdown(int node,int d)
{
if(lazy[node][d]){
lazy[lson][d] += lazy[node][d];
lazy[rson][d] += lazy[node][d];
tree[lson][d] += lazy[node][d];
tree[rson][d] += lazy[node][d];
lazy[node][d] = 0;
}
}
void add(int node,int l,int r,int L,int R,int d,int b)
{
if(L<=l&&r<=R){
lazy[node][d] += b;
tree[node][d] += b;
return;
}
pushdown(node, d);
int mid = l + r >> 1;
if(mid>=L)
add(lson, l, mid, L, R, d, b);
if(mid<R)
add(rson, mid + 1, r, L, R, d, b);
pushup(node, d);
}
int query(int node,int l,int r,int L,int R,int d)
{
if(L<=l&&r<=R)
return tree[node][d];
pushdown(node, d);
int mid = l + r >> 1;
int ans = 0;
if(L<=mid)
ans = max(ans, query(lson, l, mid, L, R, d));
if(mid<R)
ans = max(ans, query(rson, mid + 1, r, L, R, d));
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
#endif
ind[2] = 0, ind[3] = 1, ind[5] = 2, ind[7] = 3;
scanf("%d%d", &n, &t);
char s[10];
while(t--){
scanf("%s",s+1);
int a, b, c;
if(s[2]=='U'){
scanf("%d%d%d", &a, &b, &c);
if(c==4)
add(1, 1, n, a, b, ind[2], 2);
else if(c==6)
add(1, 1, n, a, b, ind[2], 1), add(1, 1, n, a, b, ind[3], 1);
else if(c==8)
add(1, 1, n, a, b, ind[2], 3);
else if(c==9)
add(1, 1, n, a, b, ind[3], 2);
else if(c==10)
add(1, 1, n, a, b, ind[2], 1), add(1, 1, n, a, b, ind[5], 1);
else
add(1, 1, n, a, b, ind[c], 1);
}
else{
scanf("%d%d", &a, &b);
int ans = 0;
for (int i = 0; i <= 3;i++){
ans = max(ans, query(1, 1, n, a, b, i));
}
printf("ANSWER %d\n", ans);
}
}
return 0;
}