题意:

初始n个空串,m个操作:

1.给[l,r]的所有字符串头尾加一个‘d’,将原字符串x变为dxd

2.求[l,r]所有字符串代表的数字之和mod 1e9+7

思路:

据说是硬核线段树。。

对于线段树我们要先找出来对于一个区间改变的时候对要询问的区间(sum)造成的变化

对于一个数x,如果对他操作了一次(头尾加一个c),那么它将变成$10x+c+c*10^{n_i+1}$,其中$n_i$表示x的实际长度(位数)

那么对当前区间操作时,当前区间的sum会变成$10sum+(r-l+1)*c+c*sumlen$,其中$sumlen=\sum_{i=l}^{r}10^{n_i+1}$

这个东西是应该写在要打懒惰标记的地方的

上式显然是有先后操作顺序的,我们只需要将操作系列$d_3d_2d_1xd_1d_2d_3$分成$d_3d_2d_1$和$d_1d_2d_3$(用两个数保存),用两个懒惰标记addl和addr分别保存

不过在pushdown的时候要注意,懒惰标记存在于已经操作过的最后一个节点里,所以要注意次序与操作的对象

在pushdown中,如果只是下传到一个节点(比如是lc),

那么$sum[lc]=addr[root]+sum[lc]*addlen[root]+addl[root]*addlen[root]*10^{n_i+1}/10$

所以对整个lc区间,$sum[lc]=\sum addr[root]+sum[lc]*addlen[root]+addl[root]+addl[root]*addlen[root]*sumlen[root]/10$

其他的都是常规操作了,注意乘的时候别溢出了

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); ll fp(ll a, ll n){
ll ans = ;
while(n){
if(n & ) ans *= a;
a *= a;
a %= mod;
n >>= ;
ans %= mod;
}
return ans;
}
ll div10;
ll sum[maxn];
ll sumlen[maxn];
ll addl[maxn],addr[maxn],addlen[maxn];
int t;
int n, m;
void build(int l, int r, int root){
int mid = (l+r)>>;
addl[root]=addr[root]=;
addlen[root]=;
if(l==r){
sum[root] = ;
sumlen[root] = ;
return;
}
build(lson);
build(rson);
sum[root]=;
sumlen[root]=sumlen[lc]+sumlen[rc];
sumlen[root]%=mod;
return;
}
void pushdown(int l, int r, int root){
int mid = (l+r)>>;
if(addlen[root]>){
addl[lc]=(addl[root]*addlen[lc]%mod+addl[lc])%mod;
addl[rc]=(addl[root]*addlen[rc]%mod+addl[rc])%mod;
addr[lc]=(addr[lc]*addlen[root]%mod+addr[root])%mod;
addr[rc]=(addr[rc]*addlen[root]%mod+addr[root])%mod;
addlen[lc]=addlen[lc]*addlen[root]%mod;
addlen[rc]=addlen[rc]*addlen[root]%mod; sum[lc]=(addr[root]*(mid-l+)%mod+sum[lc]*addlen[root]%mod+addl[root]*addlen[root]%mod*sumlen[lc]%mod*div10%mod)%mod;
sum[rc]=(addr[root]*(r-mid)%mod+sum[rc]*addlen[root]%mod+addl[root]*addlen[root]%mod*sumlen[rc]%mod*div10%mod)%mod; sumlen[lc]=(sumlen[lc]*addlen[root]%mod*addlen[root]%mod)%mod;
sumlen[rc]=(sumlen[rc]*addlen[root]%mod*addlen[root]%mod)%mod; addl[root]=addr[root]=;
addlen[root]=;
}
}
void update(int ql, int qr, ll val, int l, int r, int root){
int mid = (l+r)>>;
if(ql<=l&&r<=qr){
sum[root]=(*sum[root]%mod+(r-l+)*val%mod+val*sumlen[root]%mod)%mod;
sumlen[root]=sumlen[root]*%mod;
addl[root]=(addl[root]+val*addlen[root])%mod;
addr[root]=(addr[root]*+val)%mod;
addlen[root]=(addlen[root]*)%mod;
return;
}
pushdown(l,r,root);
if(ql<=mid)update(ql,qr,val,lson);
if(qr>mid)update(ql,qr,val,rson);
sum[root]=(sum[lc]+sum[rc])%mod;
sumlen[root]=(sumlen[lc]+sumlen[rc])%mod;
return;
}
ll query(int ql, int qr, int l, int r, int root){
int mid = (l+r)>>;
if(ql<=l&&r<=qr)return sum[root]%mod;
ll ans = ;
pushdown(l,r,root);
if(ql<=mid)ans+=query(ql,qr,lson);
if(qr>mid)ans+=query(ql,qr,rson);
return ans%mod;
}
char op[];
int main(){
scanf("%d", &t);
div10=fp(,mod-)%mod;
for(int ncase = ; ncase <= t; ncase++){
scanf("%d %d", &n, &m);
build(,n,);
printf("Case %d:\n",ncase);
while(m--){
ll w;
int x, y;
scanf("%s",op+);
scanf("%d %d", &x, &y);
if(op[]=='w'){
scanf("%lld", &w);
update(x, y, w, , n, );
}
else{
printf("%lld\n",query(x,y,,n,)%mod);
}
} } return ;
}
/*
2
3 3
wrap 1 3 1
wrap 2 4 3
query 1 2
4 4
wrap 1 3 0
wrap 2 4 3
query 1 4
query 2 3 1
4 4
wrap 1 3 0
wrap 2 4 3 3
4 4
wrap 1 4 2
wrap 1 4 3
wrap 1 4 1
wrap 2 4 6
3 2
wrap 1 3 0
wrap 2 2 1
5 3
wrap 1 5 1
wrap 2 6 0
wrap 3 5 2
*/

HDU 6562 lovers 2018CCPC吉林H(线段树)的更多相关文章

  1. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  4. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

  5. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

  7. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  8. hdu 1754 I Hate It (模板线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others)    M ...

  9. HDU 1166 敌兵布阵(线段树 单点更新)

     点我看题目  题意 :HDU的中文题也不常见....这道题我就不详述了..... 思路 :这个题用线段树用树状数组都可以,用线段树的时候要注意输入那个地方,输入一个字符串的时候不要紧接着输入两个数字 ...

随机推荐

  1. AcWing 251. 小Z的袜子| 分块+莫队

    传送门 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿. 终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命. 具体来说,小Z把这N只袜子从 ...

  2. POJ 3304 Segments(判断直线与线段是否相交)

    题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...

  3. ACM北大暑期课培训第五天

    今天讲的扫描线,树状数组,并查集还有前缀树. 扫描线   扫描线的思路:使用一条垂直于X轴的直线,从左到右来扫描这个图形,明显,只有在碰到矩形的左边界或者右边界的时候,这个线段所扫描到的情况才会改变, ...

  4. 一个动态扩展表格控件列和行的 jQuery 插件

    一个动态扩展表格控件列和行的 jQuery 插件 不过这并不影响使用鸭! 看这里:https://github.com/zhuwansu/table-ext.js 一个简单的示范 html <t ...

  5. next_permutation 函数

    next_permutation 是一个定义在 <algorithm> 中的一个全排列函数, 用于按顺序生成一个数列的全排列 基本用法 : int a[] = {1, 2, 3}; do{ ...

  6. 点分治 (等级排) codeforces 321C

    Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected ...

  7. git 工作中实用 多人协同开发

    多人协同开发 .克隆分支 git clone -b dev1. url .创建并关联远程分支 git checkout -b dev_wt origin/dev_wt 情况一获取其它分支的代码,并合并 ...

  8. gitbub 基本使用

    一.环境 git:https://git-scm.com/ 申请github账号:https://github.com/ 二.安装git 一直next即可 三.创储存建库 1.选择New reposi ...

  9. Java入门 - 面向对象 - 01.继承

    原文地址:http://www.work100.net/training/java-inheritance.html 更多教程:光束云 - 免费课程 继承 序号 文内章节 视频 1 概述 2 继承的特 ...

  10. Codeforces Round #615 (Div. 3) 题解

    A - Collecting Coins 题意: 给你四个数a,b,c,d,n.问你是否能将n拆成三个数A,B,C,使得A+a=B+b=C+c. 思路: 先计算三个数的差值的绝对值abs,如果abs大 ...