1、POJ 3067 Japan
http://poj.org/problem?id=3067
算是线段相交的感觉。给定一些线段,判断其中一个线段和多少线段相交,当然线段不是随意排列的,线段的a,b端点分别在两条平行线上。这样我们就可以对线段以左端点进行排序。右端点放在树状数组中。如果当前询问到i线段,那么只要求比i线段右端点大的线段数即可。因为对于当前线段i和比i线段右端点大的线段j来说,i线段的左端点肯定比j线段左端点大(已经排序)。那么只要i线段右端点小于j线段右端点那么i,j必然相交。画图自然理解。
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, k, c[1000+10];
const int MAX = 1000000 + 10;
struct road
{
int x;
int y;
}r[MAX];
bool operator<(const road& a,const road&b)
{
return a.x < b.x || a.x == b.x && a.y < b.y;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x, int v)
{
while (x<=m)
{
c[x] += v;
x += lowbit(x);
}
}
int getsum(int x)
{
int sum = 0;
while (x)
{
sum += c[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
int T, i, j;
__int64 ans;
while (scanf("%d", &T) == 1)
{
for (j=1; j<=T; j++)
{
ans = 0;
memset(c, 0, sizeof(c));
scanf("%d%d%d", &n, &m, &k);
for (i=1; i<=k; i++)
{
scanf("%d%d",&r[i].x, &r[i].y);
}
sort(r+1, r+k+1);
for (i=1; i<=k; i++)
{
ans += (i-1-getsum(r[i].y));
update(r[i].y, 1);
}
printf("Test case %d: %I64d\n", j, ans);
}
}
}
2、soj 2497Number sequence
http://cstest.scu.edu.cn/soj/problem.action?id=2497
用两个树状数组维护,一个维护左边比他小和右边比他大的数即可。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 32768 + 10;
int a[50000+10], b[50000+10], c[MAX], num[50000+10];
int lowbit(int x)
{
return x&(-x);
}
void update(int x, int v)
{
while (x<MAX)
{
c[x] += v;
x+=lowbit(x);
}
}
int getsum(int x)
{
int sum = 0;
while (x)
{
sum += c[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
int n, i;
long long ans;
while (scanf("%d", &n) == 1)
{
ans = 0;
memset(c, 0, sizeof(c));
for (i=0; i<n; i++)
{
scanf("%d", num+i);
num[i]++;
a[i] = getsum(num[i]-1);
update(num[i], 1);
}
reverse(num, num+n);
memset(c, 0, sizeof(c));
for (i=0; i<n; i++)
{
b[n-i-1] = getsum(num[i]-1);
update(num[i], 1);
}
for (i=0; i<n; i++)
{
ans+=a[i]*b[i];
}
printf("%lld\n", ans);
}
return 0;
}
3、HOJ 1867 经理的烦恼
http://acm.hit.edu.cn/hoj/problem/view?id=1867
每更新一个值,判断这个值是否为素数,更新即可,不可忘记记录原先这个点的值是否为素数。
/*This Code is Submitted by 1043041006 for Problem 1867 at 2013-03-31 16:32:22*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 1000000 + 10;
int C, N, M, c[MAX], a[MAX];
int lowbit(int x)
{
return x&(-x);
}
void update(int x, int v)
{
while (x<MAX)
{
c[x] += v;
x+=lowbit(x);
}
}
int getsum(int x)
{
int sum = 0;
while (x)
{
sum += c[x];
x-=lowbit(x);
}
return sum;
}
bool isprime(int x)
{
int i;
if (x<=1)
{
return false;
}
if (x == 2)
{
return true;
}
for (i=2; i*i<=x; i++)
{
if (x%i==0)
{
return false;
}
}
return true;
}
int main()
{
int i;
bool flag;
int Cas = 0;
while (scanf("%d%d%d", &C, &N, &M) == 3, C+N+M)
{
memset(c, 0, sizeof(c));
printf("CASE #%d:\n", ++Cas);
int ins, x, y;
int tmp = isprime(M);
for (i=1; i<=C;i++)
{
a[i] = M;
update(i, tmp);
}
for (i=1; i<=N; i++)
{
scanf("%d%d%d", &ins, &x, &y);
if (ins == 0)
{
flag = isprime(a[x]);
a[x] += y;
if (!flag&&isprime(a[x]))
{
update(x, 1);
}
else if (flag&&!isprime(a[x]))
{
update(x, -1);
}
}
if (ins ==1)
{
printf("%d\n", getsum(y) - getsum(x-1));
}
}
puts("");
}
return 0;
}