//线段树
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 10000;
int counts[MAXN];
int num[MAXN];
int ri, le;
void build(int x,int y, int state)
{
if (x == y)
{
counts[state] = num[x];
return;
}
int mid = (x+y)/2;
build(x, mid, state*2);
build(mid+1, y, state*2+1);
counts[state] = counts[state*2]+counts[state*2+1];
}
int gettotal(int x, int y, int state)
{
if (le>y || x>ri) return 0;
if (le<=x && y<=ri)
{
return counts[state];
}
int mid=(x+y)/2;
return gettotal(x,mid,state*2)+gettotal(mid+1,y,state*2+1);
}
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
num[i] = i;
}
build(1, n, 1);
cin >> le >> ri;
cout << gettotal(1, n, 1) << endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 10000;
int counts[MAXN];
int num[MAXN];
int ri, le;
void build(int x,int y, int state)
{
if (x == y)
{
counts[state] = num[x];
return;
}
int mid = (x+y)/2;
build(x, mid, state*2);
build(mid+1, y, state*2+1);
counts[state] = counts[state*2]+counts[state*2+1];
}
int gettotal(int x, int y, int state)
{
if (le>y || x>ri) return 0;
if (le<=x && y<=ri)
{
return counts[state];
}
int mid=(x+y)/2;
return gettotal(x,mid,state*2)+gettotal(mid+1,y,state*2+1);
}
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
num[i] = i;
}
build(1, n, 1);
cin >> le >> ri;
cout << gettotal(1, n, 1) << endl;
system("pause");
return 0;
}

11万+

被折叠的 条评论
为什么被折叠?



