标签:sub form tin build query void nes spoj 线段树
Time Limit: 500MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than 1018. On this sequence you have to apply M (M <= 100,000) operations:
(A) For given x,y, for each elements between the x-th and the y-th ones (inclusively, counting from 1), modify it to its positive square root (rounded down to the nearest integer).
(B) For given x,y, query the sum of all the elements between the x-th and the y-th ones (inclusively, counting from 1) in the sequence.
Multiple test cases, please proceed them one by one. Input terminates by EOF.
For each test case:
The first line contains an integer N. The following line contains N integers, representing the sequence A1..AN.
The third line contains an integer M. The next M lines contain the operations in the form "i x y".i=0 denotes the modify operation, i=1 denotes the query operation.
For each test case:
Output the case number (counting from 1) in the first line of output. Then for each query, print an integer as the problem required.
Print an blank line after each test case.
See the sample output for more details.
Input:
5
1 2 3 4 5
5
1 2 4
0 2 4
1 2 4
0 4 5
1 1 5
4
10 10 10 10
3
1 1 4
0 2 3
1 1 4
Output:
Case #1:
9
4
6
Case #2:
40
26
Hint
Added by: | Fudan University Problem Setters |
Date: | 2008-05-21 |
Time limit: | 0.5s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: C99 strict ERL JS PERL 6 |
Resource: | Own problem, used in ACM/ICPC Regional Contest, Shanghai 2011 preliminary |
区间开方,询问区间和。
@Bzoj3038 上帝造题的七分钟2
这题常见的解法是并查集,然而现在放在了线段树的套题里,就要思考怎么用线段树做了……
然而根本不虚哈哈哈哈 我当初就是用线段树做的哈哈哈哈
如果一个数已经变成了1,继续开方也只会得到1 。只要在线段树里标记出已经都变成1的连续区间,修改的时候就可以跳过整段区间,大幅度提高效率。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define ls l,mid,rt<<1 8 #define rs mid+1,r,rt<<1|1 9 using namespace std; 10 const int mxn=100010; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 14 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 15 return x*f; 16 } 17 long long read1(){ 18 long long x=0,f=1;char ch=getchar(); 19 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 20 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 21 return x*f; 22 } 23 int n,m; 24 long long data[mxn]; 25 struct node{ 26 long long num; 27 bool one; 28 }t[mxn<<2]; 29 void Build(int l,int r,int rt){ 30 if(l==r){ 31 t[rt].num=data[l]; 32 if(data[l]==1)t[rt].one=1; 33 else t[rt].one=0; 34 return; 35 } 36 int mid=(l+r)>>1; 37 Build(ls);Build(rs); 38 t[rt].one=(t[rt<<1].one&t[rt<<1|1].one); 39 t[rt].num=t[rt<<1].num+t[rt<<1|1].num; 40 return; 41 } 42 void change(int L,int R,int l,int r,int rt){ 43 if(l==r && L<=l && r<=R){ 44 t[rt].num=sqrt(t[rt].num); 45 if(t[rt].num==1) t[rt].one=1; 46 return; 47 } 48 int mid=(l+r)>>1; 49 if(L<=mid && !t[rt<<1].one)change(L,R,ls); 50 if(R>mid && !t[rt<<1|1].one)change(L,R,rs); 51 if(t[rt<<1].one && t[rt<<1|1].one) t[rt].one=1; 52 t[rt].num=t[rt<<1].num+t[rt<<1|1].num; 53 return; 54 } 55 long long smm(int L,int R,int l,int r,int rt){ 56 if(L<=l && r<=R){ 57 return t[rt].num; 58 } 59 long long res=0; 60 int mid=(l+r)>>1; 61 if(L<=mid)res+=smm(L,R,ls); 62 if(R>mid)res+=smm(L,R,rs); 63 return res; 64 } 65 int op; 66 int main(){ 67 int T=0; 68 while(scanf("%d",&n)!=EOF){ 69 printf("Case #%d:\n",++T); 70 int i,j; 71 for(i=1;i<=n;i++) 72 data[i]=read1(); 73 Build(1,n,1); 74 m=read(); 75 int x,y; 76 while(m--){ 77 op=read();x=read();y=read(); 78 if(x>y)swap(x,y); 79 if(op==0){ 80 change(x,y,1,n,1); 81 } 82 else{ 83 long long ans=smm(x,y,1,n,1); 84 printf("%lld\n",ans); 85 } 86 } 87 printf("\n"); 88 } 89 return 0; 90 }
SPOJ GSS4 Can you answer these queries IV
标签:sub form tin build query void nes spoj 线段树
原文地址:http://www.cnblogs.com/SilverNebula/p/6116030.html