zoukankan      html  css  js  c++  java
  • BZOJ 1798 题解

    1798: [Ahoi2009]Seq 维护序列seq

    Time Limit: 30 Sec  Memory Limit: 64 MB
    Submit: 5531  Solved: 1946
    [Submit][Status][Discuss]

    Description

    老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。

    Input

    第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。第三行有一个整数M,表示操作总数。从第四行开始每行描述一个操作,输入的操作有以下三种形式: 操作1:“1 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含双引号)。表示把所有满足t≤i≤g的ai改为ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含双引号)。询问所有满足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

    Output

    对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。

    Sample Input

    7 43
    1 2 3 4 5 6 7
    5
    1 2 5 5
    3 2 4
    2 3 7 9
    3 1 3
    3 4 7

    Sample Output

    2
    35
    8

    HINT

    【样例说明】

    初始时数列为(1,2,3,4,5,6,7)。
    经过第1次操作后,数列为(1,10,15,20,25,6,7)。
    对第2次操作,和为10+15+20=45,模43的结果是2。
    经过第3次操作后,数列为(1,10,24,29,34,15,16}
    对第4次操作,和为1+10+24=35,模43的结果是35。
    对第5次操作,和为29+34+15+16=94,模43的结果是8。



    测试数据规模如下表所示

    数据编号 1 2 3 4 5 6 7 8 9 10
    N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
    M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

    Solution 

    双标记线段树,与普通线段树类似,标记下传时考虑乘法即可。

      1 /**************************************************************
      2     Problem: 1798
      3     User: shadowland
      4     Language: C++
      5     Result: Accepted
      6     Time:4500 ms
      7     Memory:40392 kb
      8 ****************************************************************/
      9  
     10 #include "bits/stdc++.h"
     11  
     12 using namespace std ;
     13 typedef long long QAQ ;
     14 const int maxN = 1001000 ;
     15 struct SegTree { int l , r ; QAQ sum , mul , add ; } tr[ maxN ] ;
     16 QAQ MOD , val ; 
     17  
     18 QAQ arr[ maxN ] ;
     19  
     20 void Push_up ( const int i ) {
     21         tr[ i ].sum = ( tr[ i << 1 | 1 ].sum + tr[ i << 1 ].sum ) % MOD ;
     22 }
     23  
     24 void Push_down ( int i , int m ) {
     25         tr[ i << 1 ].add = ( tr[ i << 1 ].add * tr[ i ].mul + tr[ i ].add ) % MOD ;
     26         tr[ i << 1 | 1 ].add = ( tr[ i << 1 | 1 ].add * tr[ i ].mul + tr[ i ].add ) % MOD ;
     27         tr[ i << 1 ].mul = tr[ i << 1 ].mul * tr[ i ].mul % MOD ;
     28         tr[ i << 1 | 1 ].mul = tr[ i << 1 | 1 ].mul * tr[ i ].mul % MOD ;
     29         tr[ i << 1 ].sum = ( tr[ i << 1].sum * tr[ i ].mul + tr[ i ].add * ( m - ( m >> 1 ) ) ) % MOD ;
     30         tr[ i << 1 | 1 ].sum = ( tr[ i << 1 | 1 ].sum * tr[ i ].mul + tr[ i ].add * ( m >> 1 ) ) % MOD ;
     31         tr[ i ].add = 0 ;
     32         tr[ i ].mul = 1 ;
     33 }
     34  
     35 void Build_Tree ( const int x , const int y , const int i ) {
     36         tr[ i ].l = x ; tr[ i ].r = y ;
     37         tr[ i ].mul = 1 ;
     38         if ( x == y ) {
     39                 tr[ i ].sum = arr[ x ] ;
     40                 return ;
     41         }
     42         else {
     43                 int mid = ( x + y ) >> 1 ;
     44                 Build_Tree ( x , mid , i << 1 ) ;
     45                 Build_Tree ( mid + 1 , y , i << 1 | 1 ) ;
     46                 Push_up ( i ) ;
     47         }
     48 }
     49  
     50 QAQ Query_Tree ( const int q , const int w , const int i ) {
     51         if ( q <= tr[ i ].l && tr[ i ].r <= w ) {
     52                 return tr[ i ].sum % MOD ; 
     53         }
     54         else {
     55                 Push_down ( i , tr[ i ].r - tr[ i ].l + 1 ) ;
     56                 int mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ;
     57                 if ( q > mid ) return Query_Tree ( q , w , i << 1 | 1 ) % MOD;
     58                 else if ( w <= mid ) return Query_Tree ( q , w , i << 1 )  % MOD ;
     59                 else return ( Query_Tree ( q , w , i << 1 | 1 ) + Query_Tree ( q , w , i << 1 ) ) % MOD ;
     60                 Push_up ( i ) ;
     61         }
     62 }
     63  
     64 void Update_Tree ( int i , int q , int w , int _ ) {
     65         if ( q <= tr[ i ].l && tr[ i ].r <= w ) {
     66                 if ( _ == 1 ) {
     67                         tr[ i ].add = tr[ i ].add * val % MOD ;
     68                         tr[ i ].mul = tr[ i ].mul * val % MOD ;
     69                         tr[ i ].sum = tr[ i ].sum * val % MOD ;
     70                 }
     71                 else if ( _ == 2 ) {
     72                         tr[ i ].add = ( tr[ i ].add + val ) % MOD ;
     73                         tr[ i ].sum = ( tr[ i ].sum + ( QAQ ) val * ( tr[ i ].r - tr[ i ].l + 1 ) ) % MOD ;
     74                 }
     75         }
     76         else {
     77                 Push_down ( i , tr[ i ].r - tr[ i ].l + 1 ) ;
     78                 int mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ;
     79                 if ( q > mid ) Update_Tree ( i << 1 | 1 , q , w , _ ) ;
     80                 else if ( w <= mid ) Update_Tree ( i << 1 , q , w , _ ) ;
     81                 else {
     82                         Update_Tree ( i << 1 | 1 , q , w , _ ) ;
     83                         Update_Tree ( i << 1 , q , w , _ ) ;
     84                 }
     85                 Push_up ( i ) ;
     86         }
     87 }
     88  
     89 void DEBUG_( int n ) {
     90         for ( int i=1 ; i<=n ; ++i ) {
     91                 printf ( "
    %d:
    sum:%d
    add:%d
    mul:%d
    " , i , tr[ i ].sum , tr[ i ].add , tr[ i ].mul ) ;
     92         }
     93 }
     94 int main ( ) {
     95         QAQ N , Q ; 
     96         //freopen ( "sbsbs.out" , "w" , stdout ) ;
     97         scanf ( "%lld %lld" , &N , &MOD ) ;
     98         for ( int i=1 ; i<=N ; ++i ) scanf ( "%lld" , arr + i ) ;
     99         Build_Tree( 1 , N , 1 ) ;
    100         scanf ( "%lld" , &Q ) ;
    101         for ( int i=1 ; i<=Q ; ++i ){
    102                 QAQ op , l , r ;
    103                 scanf ( "%lld" , &op ) ;
    104                 //DEBUG_( 13 ) ;
    105                 if ( op == 3 ) {
    106                         scanf ( "%lld %lld" , &l , &r ) ;
    107                         printf ( "%lld
    " , Query_Tree ( l , r , 1 ) ) ;
    108                 }
    109                 else {
    110                         scanf ( "%lld %lld %lld" , &l , &r , &val ) ;
    111                         Update_Tree ( 1 , l , r , op ) ;
    112                 }
    113         }
    114         return 0 ;
    115 }
    View Code

    2016-10-13 22:15:31

    ()

  • 相关阅读:
    分享我设计的iOS项目目录结构
    swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)
    swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象
    获取UIColor中的RGB值(本人亲测多个获取RGB值的方法,这个最有效)
    swift语言开发的一个游戏------熊猫跑酷(KongfuPanda)
    ios上传应用后,审核流程完成前(reveiw)修改了程序内容,如何上传替换
    上架app 到app store 的出现: “The IPA is invalid. It does not inlude a Payload directory.”错误处理
    ios 8+ (xcode 6.0 +)应用程序Ad Hoc 发布前多设备测试流程详解
    Swift 实现iOS Animation动画教程
    新浪微博项目---首页技术点三.上拉刷新,下拉加载的实现(使用ios自带的小菊花实现)
  • 原文地址:https://www.cnblogs.com/shadowland/p/5958383.html
Copyright © 2011-2022 走看看