Transformation
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 10723 Accepted Submission(s): 2817
Problem Description
Yuanfang is puzzled with the question below:
There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.
Operation 1: Add c to each number between ax and ay inclusive. In other words, do transformation ak<---ak+c, k = x,x+1,…,y.
Operation 2: Multiply c to each number between ax and ay inclusive. In other words, do transformation ak<---ak×c, k = x,x+1,…,y.
Operation 3: Change the numbers between ax and ay to c, inclusive. In other words, do transformation ak<---c, k = x,x+1,…,y.
Operation 4: Get the sum of p power among the numbers between ax and ay inclusive. In other words, get the result of axp+ax+1p+…+ay p.
Yuanfang has no idea of how to do it. So he wants to ask you to help him.
Input
There are no more than 10 test cases.
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000.
Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3)
The input ends with 0 0.
Output
For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.
Sample Input
5 5 3 3 5 7 1 2 4 4 4 1 5 2 2 2 5 8 4 3 5 3 0 0
Sample Output
Source
2013ACM-ICPC杭州赛区全国邀请赛
Recommend
We have carefully selected several similar problems for you: 6656 6655 6654 6653 6652
题意:现在给出一个初始值全部为0的一段序列,现在总共有4种操作。
操作一:将[l,r]区间内所有数加上x;
操作二:将[l,r]区间所有数乘以x;
操作三:将[l,r]区间所有数变成x;
操作四:计算[l,r]区间每个数的p次方的和。
分析:
要是按照直接记录的放法,这个题的懒标记肯定非常复杂,但是这个题的时间给了8秒,数据也很特别,起始数据都为0。现在我们每次更改过后,那段区间的值也应该是相同的,所以,我们直接可以计算一段区间相同的数的操作的和,
sum=(r-l+1)*a[rt]^p;注意:每次更新和求和时都应该要注意懒标记。
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define pi acos(-1.0)
#define ll long long int
using namespace std;
const int M = (int)1e5;
const int mod = 10007;
ll a[M];
struct node
{
int l;
int r;
ll add;
ll mul;//区间乘法标记
ll rep;//区间修改标记
ll p[4];//存1次方,2次方和,3次方和,如果超时注意删除无用的
} tree[M * 4 + 5];
ll ans;
void pushdown(int k)
{
if(tree[k].rep)
{
tree[k<<1].rep = tree[k<<1|1].rep = tree[k].rep;
tree[k<<1].mul = tree[k<<1|1].mul = 0;
tree[k<<1].add = tree[k<<1|1].add = tree[k].rep;
tree[k<<1].p[3] = tree[k].rep * tree[k].rep * tree[k].rep % mod * (tree[k<<1].r - tree[k<<1].l + 1) % mod;
tree[k<<1].p[2] = tree[k].rep * tree[k].rep % mod * (tree[k<<1].r - tree[k<<1].l + 1) % mod;
tree[k<<1].p[1] = tree[k].rep % mod * (tree[k<<1].r - tree[k<<1].l + 1) % mod;
tree[k<<1|1].p[3] = tree[k].rep * tree[k].rep * tree[k].rep % mod * (tree[k<<1|1].r - tree[k<<1|1].l + 1) % mod;
tree[k<<1|1].p[2] = tree[k].rep * tree[k].rep % mod * (tree[k<<1|1].r - tree[k<<1|1].l + 1) % mod;
tree[k<<1|1].p[1] = tree[k].rep % mod * (tree[k<<1|1].r - tree[k<<1|1].l + 1) % mod;
tree[k].rep = 0;
}
if(tree[k].mul != 1)
{
tree[k<<1].add = tree[k<<1].add * tree[k].mul % mod;
tree[k<<1|1].add = tree[k<<1|1].add * tree[k].mul % mod;
tree[k<<1].mul = tree[k<<1].mul * tree[k].mul % mod;
tree[k<<1|1].mul = tree[k<<1|1].mul * tree[k].mul % mod;
tree[k<<1].p[3] = tree[k<<1].p[3] * tree[k].mul % mod * tree[k].mul % mod * tree[k].mul % mod;
tree[k<<1].p[2] = tree[k<<1].p[2] * tree[k].mul % mod * tree[k].mul % mod;
tree[k<<1].p[1] = tree[k<<1].p[1] * tree[k].mul % mod;
tree[k<<1|1].p[3] = tree[k<<1|1].p[3] * tree[k].mul % mod * tree[k].mul % mod * tree[k].mul % mod;
tree[k<<1|1].p[2] = tree[k<<1|1].p[2] * tree[k].mul % mod * tree[k].mul % mod;
tree[k<<1|1].p[1] = tree[k<<1|1].p[1] * tree[k].mul % mod;
tree[k].mul = 1;
}
if(tree[k].add)
{
tree[k<<1].add = (tree[k<<1].add + tree[k].add) % mod;
tree[k<<1|1].add = (tree[k<<1|1].add + tree[k].add) % mod;
tree[k<<1].p[3] = (tree[k<<1].p[3] + 3 * tree[k].add * tree[k<<1].p[2] % mod + 3 * tree[k].add * tree[k].add % mod * tree[k<<1].p[1] % mod + tree[k].add * tree[k].add * tree[k].add % mod * (tree[k<<1].r - tree[k<<1].l + 1) % mod) % mod;
tree[k<<1].p[2] = (tree[k<<1].p[2] + 2 * tree[k].add * tree[k<<1].p[1] + tree[k].add * tree[k].add % mod * (tree[k<<1].r - tree[k<<1].l + 1) % mod) % mod;
tree[k<<1].p[1] = (tree[k<<1].p[1] + tree[k].add * (tree[k<<1].r - tree[k<<1].l + 1) % mod) % mod;
tree[k<<1|1].p[3] = (tree[k<<1|1].p[3] + 3 * tree[k].add * tree[k<<1|1].p[2] % mod + 3 * tree[k].add * tree[k].add % mod * tree[k<<1|1].p[1] % mod + tree[k].add * tree[k].add * tree[k].add % mod * (tree[k<<1|1].r - tree[k<<1|1].l + 1) % mod) % mod;
tree[k<<1|1].p[2] = (tree[k<<1|1].p[2] + 2 * tree[k].add * tree[k<<1|1].p[1] + tree[k].add * tree[k].add % mod * (tree[k<<1|1].r - tree[k<<1|1].l + 1) % mod) % mod;
tree[k<<1|1].p[1] = (tree[k<<1|1].p[1] + tree[k].add * (tree[k<<1|1].r - tree[k<<1|1].l + 1) % mod) % mod;
tree[k].add = 0;
}
}
void pushup(int k)
{
tree[k].p[3] = (tree[k<<1].p[3] + tree[k<<1|1].p[3]) % mod;
tree[k].p[2] = (tree[k<<1].p[2] + tree[k<<1|1].p[2]) % mod;
tree[k].p[1] = (tree[k<<1].p[1] + tree[k<<1|1].p[1]) % mod;
}
void build(int k, int l, int r)
{
tree[k].l = l;
tree[k].r = r;
tree[k].add = 0;
tree[k].mul = 1;
tree[k].rep = 0;
if(l == r)
{
tree[k].p[3] = tree[k].p[2] = tree[k].p[1] = a[l];
return;
}
pushdown(k);
int mid = (l + r) >>1;
build(k<<1, l, mid);
build(k<<1|1, mid + 1, r);
pushup(k);
}
//dir=1,区间+;dir=2,区间乘;dir=3,区间修改
void interver(int k, int l, int r, ll c, int dir)
{
if(tree[k].l >= l && tree[k].r <= r)
{
if(dir == 1)
{
tree[k].add = (tree[k].add + c) % mod;
tree[k].p[3] = (tree[k].p[3] + 3 * c * tree[k].p[2] % mod + 3 * c * c % mod * tree[k].p[1] % mod + c * c * c % mod * (tree[k].r - tree[k].l + 1) % mod) % mod;
tree[k].p[2] = (tree[k].p[2] + 2 * c % mod * tree[k].p[1] % mod + c * c % mod * (tree[k].r - tree[k].l + 1) % mod) % mod;
tree[k].p[1] = (tree[k].p[1] + c * (tree[k].r - tree[k].l + 1) % mod) % mod;
return;
}
else if(dir == 2)
{
tree[k].add = tree[k].add * c % mod;
tree[k].mul = tree[k].mul * c % mod;
tree[k].p[3] = tree[k].p[3] * c % mod * c % mod * c % mod;
tree[k].p[2] = tree[k].p[2] * c % mod * c % mod;
tree[k].p[1] = tree[k].p[1] * c % mod;
return;
}
else if(dir == 3)
{
tree[k].add = 0;
tree[k].mul = 1;
tree[k].rep = c;
tree[k].p[3] = c * c * c % mod * (tree[k].r - tree[k].l + 1) % mod;
tree[k].p[2] = c * c % mod * (tree[k].r - tree[k].l + 1) % mod;
tree[k].p[1] = c * (tree[k].r - tree[k].l + 1) % mod;
return;
}
}
pushdown(k);
int mid = (tree[k].l + tree[k].r) >>1;
if(l <= mid)
interver(k<<1, l,r, c, dir);
if(mid < r)
interver(k<<1|1,l,r, c, dir);
pushup(k);
}
//ans求和,求表示ans=a[l]^c+a[l+1]^c......+a[r]^c
void query(int k, int l, int r, int c)
{
if(tree[k].l >= l && tree[k].r <= r)
{
ans = (ans + tree[k].p[c]) % mod;
return;
}
pushdown(k);
int mid = (tree[k].l + tree[k].r) >>1;
if(l <= mid)
query(k<<1, l,r, c);
if(mid < r)
query(k<<1|1, l,r, c);
pushup(k);
}
int main()
{
int n, m;
while(~scanf("%d %d", &n, &m))
{
if(n + m == 0)
break;
build(1, 1, n);
while((m--) > 0)
{
int dir, l, r;
ll c;
scanf("%d %d %d %lld", &dir, &l, &r, &c);
if(dir <= 3)
interver(1,l, r, c, dir);
else
{
ans = 0;
query(1,l, r, c);
printf("%lld\n", ans);
}
}
}
return 0;
}