Codeforces传送门
洛谷传送门
题意翻译
给定数列,区间查询和,区间取模,单点修改。
n,m≤105 n , m ≤ 10 5
题目描述
At the children’s day, the child came to Picks’s house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.
Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1],a[2],...,a[n] a [ 1 ] , a [ 2 ] , . . . , a [ n ] . Then he should perform a sequence of mm operations. An operation can be one of the following:
- Print operation l,r l , r . Picks should write down the value of .
- Modulo operation l,r,x l , r , x . Picks should perform assignment a[i]=a[i] mod x a [ i ] = a [ i ] m o d x for each i i .
- Set operation k,x k , x . Picks should set the value of a[k] a [ k ] to x x (in other words perform an assignment ).
Can you help Picks to perform the whole sequence of operations?
输入输出格式
输入格式:
The first line of input contains two integer: n,m n , m ( 1≤n,m≤105 1 ≤ n , m ≤ 10 5 ) . The second line contains n n integers, separated by space: — initial value of array elements.
Each of the next m m lines begins with a number .
- If type=1 t y p e = 1 , there will be two integers more in the line: l,r(1≤l≤r≤n) l , r ( 1 ≤ l ≤ r ≤ n ) , which correspond the operation 1 1 .
- If , there will be three integers more in the line: l,r,x(1≤l≤r≤n;1≤x≤109) l , r , x ( 1 ≤ l ≤ r ≤ n ; 1 ≤ x ≤ 10 9 ) , which correspond the operation 2.
- If type=3 t y p e = 3 , there will be two integers more in the line: k,x(1≤k≤n;1≤x≤109) k , x ( 1 ≤ k ≤ n ; 1 ≤ x ≤ 10 9 ) , which correspond the operation 3.
输出格式:
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
输入输出样例
输入样例#1:
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
输出样例#1:
8
5
输入样例#2:
10 10
6 9 6 7 6 1 10 10 9 5
1 3 9
2 7 10 9
2 5 10 8
1 4 7
3 3 7
2 7 9 9
1 2 4
1 6 6
1 5 9
3 1 10
输出样例#2:
49
15
23
1
9
样例解释
对于样例#1:
- At first, a = {1, 2, 3, 4, 5}.
- After operation 1, a = {1, 2, 3, 0, 1}.
- After operation 2, a = {1, 2, 5, 0, 1}.
- At operation 3, 2 + 5 + 0 + 1 = 8.
- After operation 4, a = {1, 2, 2, 0, 1}.
- At operation 5, 1 + 2 + 2 = 5.
解题分析
考虑每次取模后, 如果当前数值比模数大, 则肯定取模后的值至少比原来少一半, 因此不考虑单点修改的话每个数字最多修改 log(N) l o g ( N ) 次。 所以总复杂度为 O(Nlog2(N)) O ( N l o g 2 ( N ) ) 。
我们维护一个区间最大值, 如果当前区间最大值 < < <script type="math/tex" id="MathJax-Element-41"><</script>模数, 则直接 return r e t u r n 。
代码如下:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
#define R register
#define IN inline
#define gc getchar()
#define W while
#define MX 100050
#define ll long long
#define ls (now << 1)
#define rs (now << 1 | 1)
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
W (!isdigit(c)) c = gc;
W (isdigit(c))
x = (x << 1) + (x << 3) + c - 48, c = gc;
}
int dot, q;
ll dat[MX], MOD;
struct Node
{ll sum, mx;} tree[MX << 5];
namespace SGT
{
IN void pushup(const int &now)
{
tree[now].mx = std::max(tree[ls].mx, tree[rs].mx);
tree[now].sum = tree[ls].sum + tree[rs].sum;
}
void build(const int &now, const int &lef, const int &rig)
{
if(lef == rig) return tree[now].mx = tree[now].sum = dat[lef], void();
int mid = lef + rig >> 1;
build(ls, lef, mid), build(rs, mid + 1, rig);
pushup(now);
}
void modify(const int &now, const int &lef, const int &rig, const int &tar, const ll &del)
{
if(lef == rig) return tree[now].mx = tree[now].sum = del, void();
int mid = lef + rig >> 1;
if(tar <= mid) modify(ls, lef, mid, tar, del);
else modify(rs, mid + 1, rig, tar, del);
pushup(now);
}
void mod(const int &now, const int &lef, const int &rig, const int &lb, const int &rb)
{
if(tree[now].mx < MOD) return;
if(lef == rig) return tree[now].sum = tree[now].mx = tree[now].sum % MOD, void();
int mid = lef + rig >> 1;
if(lb <= mid) mod(ls, lef, mid, lb, rb);
if(rb > mid) mod(rs, mid + 1, rig, lb, rb);
pushup(now);
}
IN ll query(const int &now, const int &lef, const int &rig, const int &lb, const int &rb)
{
if(lef >= lb && rig <= rb) return tree[now].sum;
int mid = lef + rig >> 1; ll ret = 0;
if(lb <= mid) ret += query(ls, lef, mid, lb, rb);
if(rb > mid) ret += query(rs, mid + 1, rig, lb, rb);
return ret;
}
}
int main(void)
{
int typ, a, b; ll c;
in(dot), in(q);
for (R int i = 1; i <= dot; ++i) in(dat[i]);
SGT::build(1, 1, dot);
W (q--)
{
in(typ);
if(typ == 1)
{
in(a), in(b);
printf("%I64d\n", SGT::query(1, 1, dot, a, b));
}
else if(typ == 2)
{
in(a), in(b), in(MOD);
SGT::mod(1, 1, dot, a, b);
}
else
{
in(a), in(c);
SGT::modify(1, 1, dot, a, c);
}
}
}