HDU 4027 Can you answer these queries?
来源:互联网 发布:淘宝怎么开店好赚钱呢 编辑:程序博客网 时间:2024/05/29 14:27
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9288 Accepted Submission(s): 2127
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
Case #1:1976
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100010
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
struct node
{
int l,r;
ll sum;
}nod[maxn*3];
int n,m;
ll num[maxn];
void build(int i,int l,int r)
{
nod[i].l=l;
nod[i].r=r;
if(l==r)
{
nod[i].sum=num[l];
return ;
}
int mid=(r+l)/2;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
nod[i].sum=nod[i<<1].sum+nod[i<<1|1].sum;
}
void update(int i,int a,int b)
{
if(nod[i].l==nod[i].r)
{
nod[i].sum=(ll)sqrt(nod[i].sum*1.0);
return ;
}
if(nod[i].sum==nod[i].r-nod[i].l+1)return ;
int mid=(nod[i].l+nod[i].r)/2;
if(b<=mid)update(i<<1,a,b);
else if(a>mid)update(i<<1|1,a,b);
else
{
update(i<<1,a,mid);
update(i<<1|1,mid+1,b);
}
nod[i].sum=nod[i<<1].sum+nod[i<<1|1].sum;
}
ll query(int i,int a,int b)
{
if(nod[i].l==a&&nod[i].r==b)
{
return nod[i].sum;
}
ll ans=0;
int mid=(nod[i].l+nod[i].r)/2;
if(b<=mid)ans+=query(i<<1,a,b);
else if(a>mid)ans+=query(i<<1|1,a,b);
else ans+=(query(i<<1,a,mid)+query(i<<1|1,mid+1,b));
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,a,b,cnt=0;
while(scanf("%d",&n)!=EOF)
{
cnt++;
for(int i=1;i<=n;i++)
scanf("%I64d",&num[i]);
printf("Case #%d:\n",cnt);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&t,&a,&b);
if(t)
{
printf("%I64d\n",query(1,min(a,b),max(a,b)));
}
else
{
update(1,min(a,b),max(a,b));
}
}
printf("\n");
}
return 0;
}
- HDU--4027(Can you answer these queries?)
- HDU-4027-Can you answer these queries
- hdu 4027 Can you answer these queries?
- HDU-4027-Can you answer these queries?
- hdu 4027 Can you answer these queries?
- hdu 4027 Can you answer these queries?
- hdu 4027 Can you answer these queries?
- HDU 4027 Can you answer these queries?
- HDU--4027(Can you answer these queries?)
- hdu - 4027 Can you answer these queries?
- hdu-4027 Can you answer these queries?
- hdu 4027 Can you answer these queries?
- hdu 4027 Can you answer these queries?
- HDU 4027 Can you answer these queries?
- hdu-4027 Can you answer these queries?
- hdu 4027 Can you answer these queries?
- HDU 4027 Can you answer these queries?
- hdu 4027 Can you answer these queries?
- Tarjan算法
- POJ 1236 Network of Schools
- POJ 2553 The Bottom of a Graph
- POJ 2186 Popular Cows
- HDU 5050 Divided Land
- HDU 4027 Can you answer these queries?
- Kosaraju算法
- HDU 2063 过山车
- HDU 3861 The King’s Problem
- CF 293 A. Vitaly and Strings
- centos7 下双网卡如何配置静态IP
- Codeforces Round #295 (Div. 2)
- Codeforces Round #294 (Div. 2)
- Codeforces Round #293 (Div. 2)