题意
给出一个n个数的序列。下面有m个指令
1指令:原序列从l到r的和
2指令:排序过的序列从l到r的和
思路
线段树可做….直接扫一遍记录sum数组也可以
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
const int maxn = 100010;
int n;
int s[maxn];
int ss[maxn];
ll segTree[maxn<<2];
ll ssegTree[maxn<<2];
void build(int l,int r,int node)
{
if(l == r) {
segTree[node] = s[l];
return;
}
build(l,(l+r)/2,node<<1);
build((l+r)/2+1,r,(node<<1)+1);
segTree[node] = segTree[node<<1]+segTree[(node<<1)+1];
}
void buildd(int l,int r,int node)
{
if(l == r) {
ssegTree[node] = ss[l];
return;
}
buildd(l,(l+r)/2,node<<1);
buildd((l+r)/2+1,r,(node<<1)+1);
ssegTree[node] = ssegTree[node<<1]+ssegTree[(node<<1)+1];
}
ll query(int a,int b,int l,int r,int node)
{
if(r < a || l > b) return 0;
if(a <= l && r <= b) return segTree[node];
return query(a,b,l,(l+r)/2,node<<1)+query(a,b,(l+r)/2+1,r,(node<<1)+1);
}
ll qquery(int a,int b,int l,int r,int node)
{
if(r < a || l > b) return 0;
if(a <= l && r <= b) return ssegTree[node];
return qquery(a,b,l,(l+r)/2,node<<1)+qquery(a,b,(l+r)/2+1,r,(node<<1)+1);
}
int main()
{
scanf("%d",&n);
for(int i = 1 ; i <= n ; i ++) {
scanf("%d",&s[i]);
ss[i] = s[i];
}
sort(ss+1,ss+n+1);
build(1,n,1);
buildd(1,n,1);
int m;
scanf("%d",&m);
while(m--) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a == 1) printf("%I64d\n",query(b,c,1,n,1));
else printf("%I64d\n",qquery(b,c,1,n,1));
}
return 0;
}
Codeforces Round #248 (Div. 2) B - Kuriyama Mirai's Stones
时间: 2024-10-01 11:26:31