复习了下线段树
线段树的区间修改
这道题有调试了两个多小时,怎么没进步啊,
wa的原因是两个int形相乘,结果如果是long形,不仅结果的变量要用long,这两个数也得是long形,不然仍会溢出,原来就犯过同样的错误,怎么又犯了,还有找了那么长时间,k靠!!!!!!!!!!!!还有还有,如果要组成if else 语句,要有括号就都有,不能只有一个有
#include <iostream>
#include <cstdio>
#include <algorithm>
#define LL __int64
using namespace std;
int n,tot;
const int maxn=70000+10;
struct node { int u,v,h; };
int ah[maxn*2*3];
int x[maxn*2];
node arc[maxn];
int max(int a,int b) { return a>b?a:b; }
int binary(int q)
{
int low=0,high=tot,mid;
while(high-low>0)
{
mid=low+(high-low)/2;
if(x[mid]==q) return mid;
if(x[mid]>q) high=mid;
else low=mid+1;
}
}
int lc,rc,ql,qr,maxv,th;
void update(int t,int l,int r)
{
if(l>=lc&&r<=rc)
{
if(th>ah[t]) ah[t]=th;
}
else
{
int m=l+(r-l)/2;
if(rc>m) update(t*2+2,m+1,r);
if(lc<=m) update(t*2+1,l,m);
}
}
void query(int t,int l,int r)
{
if(l>=ql&&r<=qr)
{
maxv=max(ah[t],maxv);
}
else
{
int m=l+(r-l)/2;
if(qr>m) query(t*2+2,m+1,r);
if(ql<=m) query(t*2+1,l,m);
maxv=max(maxv,ah[t]);
}
}
LL solve()
{
int i;
for(i=0;i<n;i++)
{
lc=binary(arc[i].u)+1,rc=binary(arc[i].v),th=arc[i].h;
update(0,1,tot-1);
}
LL area=0;
for(i=0;i<tot-1;i++)
{
maxv=0;
ql=qr=i+1;
query(0,1,tot-1);
area+=((LL)(x[i+1]-x[i]))*((LL)maxv);
}
return area;
}
int main()
{
while(~scanf("%d",&n))
{
int i;
int u,v,h;
memset(ah,0,sizeof(ah));
int q=0;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&u,&v,&h);
arc[i].u=u,arc[i].v=v,arc[i].h=h;
x[q++]=u,x[q++]=v;
}
sort(x,x+q);
int tem=x[0];
tot=1;
for(i=1;i<q;i++)
{
if(x[i]==tem) continue;
else
{
x[tot++]=x[i];
tem=x[i];
}
}
LL ans=solve();
printf("%I64d\n",ans);
}
return 0;
}