Problem F: Homework
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld
题目描述
Problem Description
There are n children (numbered from 1 to n) learning the arithmetic operations, which include addition “+”, subtraction “−”, multiplication “×”, and division “÷” on rational numbers.
In the beginning, each child has a paper sheet with only a zero on it. Their teacher, Frank,will then give them q operations. The i-th operation consists of an operator ci and an integer xi . The children numbered ℓi , ℓi + 1, . . . , ri have to append the operator ci and the integer xi to their paper sheets. After that, every child has an expression on their sheet to be evaluated.
For example, suppose that n = 3, q = 2, c1 is “+”, x1 = 1, ℓ1 = 1, r1 = 2, c2 is “−”, x2 = 2, ℓ2 = 2, and r2 = 3. The expressions on the sheets are are 0 + 1, 0 + 1 − 2 and 0 − 2 for children 1, 2 and 3, respectively.
Since Frank is really lazy and wants to verify the answers quickly, he asks you to calculate the sums of the values of all children’s expressions. Suppose that the value of the expression assigned to child i is aibibiai, then the value will be a × b−1 mod 109 + 7 instead, where b−1 denotes the integer satisfying b × b−1 ≡ 1 mod 109 + 7. If the sum is not in [0, 109 + 7), then the sum modulo 109 + 7 should be returned to Frank.
Note: The arithmetic operations has PEMDAS rule, that is, multiplications and divisions should be evaluated before evaluating additions and subtraction.
输入描述:
Input Format
The first line consists of two space-separated integers n and q. The i-th of the following q lines consists of four space-separated tokens ℓi , ri , ci , xi . For the sake of convenience, * and / are used to represent the multiplication and the division operators, respectively.
输出描述:
Output the number that you should return to Frank.
示例1
输入
复制
3 2 1 2 + 1 2 3 - 2
输出
复制
1000000005
备注:
•1 ≤ n ≤ 105
•1 ≤ q ≤ 105
•ℓi , ri ∈ [1, n] for all 1 ≤ i ≤ q.
•ci ∈ {+, -, *, /} for all 1 ≤ i ≤ q.
•For all 1 ≤ i ≤ q, xi = 0 implies that ci is not /.
•xi ∈ [0, 109 + 7) for all 1 ≤ i ≤ q.
#include<bits/stdc++.h>
using namespace std;
#define ls (o << 1)
#define rs (o << 1 | 1)
typedef long long ll;
const int MAXN = 100010;
const ll MOD = 1000000007;
inline ll Pow(ll a, ll b)
{
ll ans = 1;
for(; b; b >>= 1)
{
if(b & 1) ans = ans * a % MOD;
a = a * a % MOD;
}
return ans;
}
struct Node
{
ll sum, tag, res;
}t[MAXN << 2];
void pushdown(int o, int l, int r)
{
int mid = (l + r) >> 1;
if(t[o].res != -1)
{
t[ls].sum = (mid - l + 1) * t[o].res % MOD;
t[ls].tag = 1, t[ls].res = t[o].res;
t[rs].sum = (r - mid) * t[o].res % MOD;
t[rs].tag = 1, t[rs].res = t[o].res;
t[o].res = -1;
}
if(t[o].tag != 1)
{
t[ls].sum = t[ls].sum * t[o].tag % MOD;
t[ls].tag = t[ls].tag * t[o].tag % MOD;
t[rs].sum = t[rs].sum * t[o].tag % MOD;
t[rs].tag = t[rs].tag * t[o].tag % MOD;
t[o].tag = 1;
}
}
void Reset(int o, int l, int r, int L, int R, ll val)
{
if(L <= l && r <= R)
{
t[o].sum = (r - l + 1) * val % MOD;
t[o].tag = 1, t[o].res = val;
return;
}
pushdown(o, l, r);
int mid = (l + r) >> 1;
if(L <= mid) Reset(ls, l, mid, L, R, val);
if(R > mid) Reset(rs, mid + 1, r, L, R, val);
t[o].sum = (t[ls].sum + t[rs].sum) % MOD;
}
ll query(int o, int l, int r, int L, int R)
{
if(L <= l && r <= R) return t[o].sum;
pushdown(o, l, r);
int mid = (l + r) >> 1; ll ans = 0;
if(L <= mid) ans = (ans + query(ls, l, mid, L, R)) % MOD;
if(R > mid) ans = (ans + query(rs, mid + 1, r, L, R)) % MOD;
return ans;
}
void mul(int o, int l, int r, int L, int R, ll val)
{
if(L <= l && r <= R)
{
t[o].sum = t[o].sum * val % MOD;
t[o].tag = t[o].tag * val % MOD;
return;
}
pushdown(o, l, r);
int mid = (l + r) >> 1;
if(L <= mid) mul(ls, l, mid, L, R, val);
if(R > mid) mul(rs, mid + 1, r, L, R, val);
t[o].sum = (t[ls].sum + t[rs].sum) % MOD;
}
int main()
{
for(int i = 0; i < (MAXN << 2); ++i) t[i].sum = 0, t[i].tag = 1, t[i].res = -1;
int n, q; scanf("%d %d", &n, &q); ll ans = 0;
while(q--)
{
int l, r; char c; ll val;
scanf("%d %d %c %lld", &l, &r, &c, &val);
if(c == '-') {val = MOD - val; c = '+';}
if(c == '/') {val = Pow(val, MOD - 2); c = '*';}
if(c == '+')
{
ans = (ans + query(1, 1, n, l, r)) % MOD;
Reset(1, 1, n, l, r, val);
}
if(c == '*')
{
mul(1, 1, n, l, r, val);
}
}
ans = (ans + query(1, 1, n, 1, n)) % MOD;
printf("%lld\n", ans);
return 0;
}

本文介绍了一种算法来帮助教师快速计算孩子们作业中涉及的加减乘除运算后的总和,特别考虑了取模运算规则。问题涉及数据范围和计算复杂性,适合C/C++等语言实现。

334

被折叠的 条评论
为什么被折叠?



