Hdu 1166 enemy army deployment, hdu1166
Enemy army deploymentTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 42907 Accepted Submission (s): 18153
A direct template for a raw tree Array
#include<iostream>#include<cstdio>#include<cstring>#define M 100005using namespace std;int a[M],n;int lowbit(int i){ return i&(-i);}void add(int i,int x){ while(i<=n) { a[i]+=x; i+=lowbit(i); }}int sum(int i){ int ans=0; while(i>0) { ans+=a[i]; i-=lowbit(i); } return ans;}int main(){ int i,cas,t; scanf("%d",&cas); t=1; while(cas--) { memset(a,0,sizeof a); scanf("%d",&n); int b; for(i=1;i<=n;i++) { scanf("%d",&b); add(i,b); } printf("Case %d:\n",t); t++; while(1) { int s,e; char str[10]; scanf("%s",str); if(str[0]=='E') break; scanf("%d%d",&s,&e); if(str[0]=='Q') { printf("%d\n",sum(e)-sum(s-1)); } else if(str[0]=='A') { add(s,e); } else if(str[0]=='S') { add(s,-e); } } } return 0;}
Tree array, HDU1166, enemy soldiers, battle. I don't know whether it's an endless loop or something is hard to write.
50000
Are you using an array? Do you need to use a number array so small?
What's wrong with acm's enemy, bu tianhang, and Hangzhou Power's 1166 process?
# Include <iostream>
# Include <cstring>
Using namespace std;
# Define N 50005
Int a [N], n;
Int low_bit (int t)
{
Return t & (-t );
}
Void ins (int t, int der)
{
While (t <= n)
{
A [t] + = der;
T + = low_bit (t );
}
}
Int sum (int t)
{
Int ret = 0;
While (t)
{
Ret + = a [t];
T-= low_bit (t );
}
Return ret;
}
Int main ()
{
Int t, I, x, r1, r2, cs = 1; // Delete n
Char c [10];
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n );
Printf ("Case % d: \ n", cs ++ );
Memset (a, 0, sizeof ());
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & x );
Ins (I, x );
}
While (scanf ("% s", c) = 1 & strcmp (c, "End "))
{
Scanf ("% d", & r1, & r2 );
If (strcmp (c, "Query") = 0)
{
// Cout <sum (r2)-sum (r1-1) <endl;
Printf ("% d \ n", sum (r2)-sum (r1-1 ));
}
Else if (strcmp (c, "Add") = 0)
{
Ins (r1, r2 );
}
Else if (strcmp (c, "...... the remaining full text>