# BZOJ 1798 题解

## 1798: [Ahoi2009]Seq 维护序列seq

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

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

2
35
8

## HINT

【样例说明】

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
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 ( "\n%d:\nsum:%d\nadd:%d\nmul:%d\n" , 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\n" , 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

()

posted @ 2016-10-13 22:16  SHHHS  阅读(153)  评论(0编辑  收藏  举报