Problem 2171 defensive position IIAccept: 143 submit: 565
Time Limit: 3000 msec memory limit: 32768 kb Problem Description
There are a total of N soldiers in the army, each of which has its own capacity index XI. During a drill, the headquarters determined m locations to be defended, the headquarters will select M soldiers to go to the designated location for defensive tasks in turn. The reference index obtained is the sum of the capabilities of M soldiers. Over time, the headquarters will issue Q commands to replace m of soldiers defending. The performance index of each soldier who completes the defensive task will drop by 1 due to fatigue and other reasons. Now the soldiers are in a row. Please calculate the reference index of each defender.
Input
The input contains multiple groups of data.
The first line contains two integers, n, m, and Q (1 <=n <= 100000,1 <= m <= 100000 ,1 <= q <= ), the N integers in the second row indicate the capability index XI (1 <= xi <= 1000) of each soldier ).
In the next Q row, each row has an integer x, indicating that the previous soldiers are replaced with M soldiers starting with X in the original queue. (1 <= x <= N-M + 1)
For 30% of data 1 <= m, n, q <= 1000.
Output
Output Q rows, each row has an integer, which is the reference index of soldiers who defend each command execution.
Sample input5 3 3 2 1 3 1 4 1 2 3 sample output6 3 5
Modify the line segment tree interval. Pay attention to the delay mark. You cannot modify the parameters of the function and query function!
Latency Tag:
A new flag is added for each node to record whether the node has been modified (this modification will affect its subnodes, we first divide the node into the nodes in the line segment tree according to the interval query method, then modify the information of these nodes, and mark these nodes with the tags representing the modification operation. When modifying and querying, if we reach a node P and decide to consider its subnodes, we need to check whether node P is marked. If yes, modify the information of its subnodes according to the tag, mark the subnodes with the same tag, and delete the P tag.
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=100000+10; 7 int b[MAXN*4]; 8 struct node 9 { 10 int l,r; 11 int num; 12 int col; 13 int mid() 14 { 15 return (l+r)/2; 16 } 17 }a[MAXN*5]; 18 19 void pushup(int step) 20 { 21 a[step].num=a[step*2].num+a[step*2+1].num; 22 } 23 24 void pushdown(int x,int step) 25 { 26 if(a[step].col!=0) 27 { 28 a[step*2+1].col+=a[step].col; 29 a[step*2].col+=a[step].col; 30 a[step*2].num+=a[step].col*(x-(x/2)); 31 a[step*2+1].num+=a[step].col*((x/2)); 32 a[step].col=0; 33 } 34 } 35 36 void btree(int l,int r,int step) 37 { 38 a[step].l=l; 39 a[step].r=r; 40 a[step].col=0; 41 if(l==r) 42 { 43 a[step].num=b[l]; 44 return ; 45 } 46 int mid=a[step].mid(); 47 btree(l,mid,step*2); 48 btree(mid+1,r,step*2+1); 49 pushup(step); 50 } 51 52 void ptree(int l,int r,int val,int step) 53 { 54 if(l<=a[step].l&&a[step].r<=r) 55 { 56 a[step].col+=val; 57 a[step].num+=val*(a[step].r-a[step].l+1); 58 return ; 59 } 60 pushdown(a[step].r-a[step].l+1,step); 61 int mid=a[step].mid(); 62 if(l>mid) 63 ptree(l,r,val,step*2+1); 64 else if(r<=mid) 65 ptree(l,r,val,step*2); 66 else 67 { 68 ptree(l,r,val,step*2); 69 ptree(l,r,val,step*2+1); 70 } 71 pushup(step); 72 } 73 74 int fintree(int l,int r,int step) 75 { 76 if(l<=a[step].l&&a[step].r<=r) 77 return a[step].num; 78 pushdown(a[step].r-a[step].l+1,step); 79 int mid=a[step].mid(); 80 if(l>mid) 81 return fintree(l,r,step*2+1); 82 else if(r<=mid) 83 return fintree(l,r,step*2); 84 else 85 return fintree(l,r,step*2)+fintree(l,r,step*2+1); 86 } 87 88 int main() 89 { 90 int n,m,kase,num; 91 while(scanf("%d %d %d",&n,&m,&kase)!=EOF) 92 { 93 for(int i=1;i<=n;i++) 94 scanf("%d",&b[i]); 95 btree(1,n,1); 96 while(kase--) 97 { 98 scanf("%d",&num); 99 int ans=fintree(num,num+m-1,1);100 printf("%d\n",ans);101 ptree(num,num+m-1,-1,1);102 }103 }104 return 0;105 }
View code