题意是说TF的 钩子换了某些节之后价值多少。
最开始为铜的。价值为1,银价值为2,金价值为3.
长度为 n ,接下来的操作可以将某一段的钩子换成 铜,银,金的某一种。
其实就是线段树的区间修改。
直接修改区间所有 为某一个值。 核心思想就是延时。
大白书上p204~p207有详细讲解。不过貌似大白书上的maintain函数不能直接使用前面区间增减的。
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<bitset>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR_(i,a,b) for(int i=a;i>=b;i--)
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)
#define MOD 1000000007
#define SIZE 100001
using namespace std;
int t[SIZE*4];
int up[SIZE*4];
int ul,ur,us;
void cal(int l,int r,int o)
{
t[o]=0;
if(up[o]>=0)
t[o]+=up[o]*(r-l+1);
else if(l<r)
t[o]=t[o*2]+t[o*2+1];
//pf("%d:%d\n",o,t[o]);
}
void update(int l,int r,int o)
{
if(ul<=l&&ur>=r)
up[o]=us;
else
{
if(up[o]>=0)
{
up[o*2]=up[o*2+1]=up[o];
up[o]=-1;
}
int m=(l+r)>>1;
if(ul<=m)update(l,m,o*2);
else cal(l,m,o*2);
if(ur>m)update(m+1,r,o*2+1);
else cal(m+1,r,o*2+1);
}
cal(l,r,o);
}
int ql,qr;
int query(int l,int r,int o)
{
int ans=0;
if(up[o]>=0)
ans+=up[o]*(min(r,qr)-max(l,ql)+1);
else if(ql<=l&&qr>=r)
ans+=t[o];
else
{
int m=(l+r)>>1;
if(ql<=m)ans+=query(l,m,o*2);
if(qr>m)ans+=query(m+1,r,o*2+1);
}
return ans;
}
int main()
{
int T;
sf("%d",&T);
FOR(o,1,T+1)
{
int n,m;
sf("%d%d",&n,&m);
CLR(t,0);
CLR(up,-1);
ul=1,ur=n,us=1;
update(1,n,1);
while(m--)
{
sf("%d%d%d",&ul,&ur,&us);
update(1,n,1);
}
ql=1,qr=n;
pf("Case %d: The total value of the hook is %d.\n",o,query(1,n,1));
}
}
在写测试数据时,想到一种新方法,就是后面的肯定把前面的覆盖了,
那么我直接从后面找,只要在这个范围就好了。
然后写完提交,果然思路是正确的。
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<bitset>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR_(i,a,b) for(int i=a;i>=b;i--)
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)
#define SIZE 100000 +1
#define MOD 1000000007
using namespace std;
struct node
{
int l,r;
int h;
}hook[SIZE];
int main()
{
int t;
sf("%d",&t);
FOR(o,1,t+1)
{
int n,m;
sf("%d%d",&n,&m);
FOR(i,0,m)
sf("%d%d%d",&hook[i].l,&hook[i].r,&hook[i].h);
int ans=0;
FOR(i,1,n+1)
{
int tmp=1;
FOR_(k,m-1,0)
{
if(i>=hook[k].l&&i<=hook[k].r)
{
tmp=hook[k].h;
break;
}
}
ans+=tmp;
}
pf("Case %d: The total value of the hook is %d.\n",o,ans);
}
}