hdu_3074_Multiply game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3074
题意:求区间的的乘积。
思路:模板题,把求区间和改为求区间乘积就行了,单点更新。
/* * hdu_3074_Multiplygame.cpp * Created on: 2013年8月26日 * Author: pirate */ #include <set> #include <map> #include <list> #include <deque> #include <queue> #include <cmath> #include <bitset> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <fstream> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define N 50005 #define I64 __int64 #define LL long long #define PI acos(-1.0) #define L(a) ((a)*2) #define R(a) ((a)*2+1) #define mod 1000000007 #define sqr(a) ((a)*(a)) #define MID(a,b) ((a)+(b))/2 #define ABS(a) (a)>0?(a):(-a) #define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b)) #define INIT(a,b) memset(a,b,sizeof(a)) struct edge{ int l,r; LL sum; }T[N*3]; void Build(int s,int ll,int rr){ T[s].l = ll; T[s].r = rr; T[s].sum = 0; if(ll==rr) return; int mid = MID(ll,rr); Build(L(s),ll,mid); Build(R(s),mid+1,rr); } void update(int s,int pos,int val){ if(T[s].l == T[s].r){ T[s].sum = val; return; } int mid = MID(T[s].l,T[s].r); if(pos <= mid){ update(L(s),pos,val); } else{ update(R(s),pos,val); } T[s].sum = T[L(s)].sum * T[R(s)].sum % mod; } LL getsum(int s,int ll,int rr){ LL ans = 1; if(T[s].l == ll && T[s].r == rr){ return T[s].sum; } int mid = MID(T[s].l,T[s].r); if(rr <= mid){ ans *= getsum(L(s),ll,rr); } else if(ll > mid){ ans *= getsum(R(s),ll,rr); } else{ ans = ans * getsum(L(s),ll,mid) % mod; ans = ans * getsum(R(s),mid+1,rr) % mod; } return ans; } int main(){ int t; cin >> t; while(t--){ int n; cin >> n; Build(1,1,n); for(int i = 1;i <= n;i ++){ int x; cin >> x; update(1,i,x); } int m; cin >> m; while(m--){ int x,y,z; cin >> x >> y >> z; if(x == 0){ printf("%lld\n",getsum(1,y,z)); } else{ update(1,y,z); } } } return 0; }

浙公网安备 33010602011771号