标签:
Description
Input
Output
Sample Input
2
ACMlove2015
1 11
8 10
1
testMessage
1 1
Sample Output
6891 9240 88
//这题用数学公式做,貌似很简单,但是我不会,用线段树做的,也比较快嘛。187ms AC
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 struct Tree 7 { 8 int hx; 9 int l,r; 10 }tree[300000]; 11 12 char str[100005]; 13 14 int build_tree(int left,int right,int k) 15 { 16 tree[k].l=left; 17 tree[k].r=right; 18 19 if (left==right) //到子节点 20 { 21 tree[k].hx=str[left-1]-28; 22 return tree[k].hx; 23 } 24 25 int mid=(left+right)/2; 26 27 tree[k].hx=( build_tree(left,mid,2*k) * build_tree(mid+1,right,2*k+1))%9973; 28 return tree[k].hx; 29 } 30 31 int find(int left,int right,int k) 32 { 33 if( left==tree[k].l && right==tree[k].r ) 34 return tree[k].hx; 35 36 int mid=(tree[k].l+tree[k].r)/2; 37 38 if (left>mid) return find(left,right,2*k+1); 39 if (right<=mid) return find(left,right,2*k); 40 41 return ( find(left,mid,2*k) * find(mid+1,right,2*k+1) )%9973; 42 } 43 44 int main() 45 { 46 int N; 47 int len,a,b; 48 while (scanf("%d",&N)!=EOF) 49 { 50 scanf("%s",str); 51 len=strlen(str); 52 build_tree(1,len,1); 53 54 while (N--) 55 { 56 scanf("%d%d",&a,&b); 57 printf("%d\n",find(a,b,1)); 58 } 59 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/haoabcd2010/p/5742472.html