This is much like Ke Duoli tree ah ~
The idea is very clever.
code:
#include <cstdio> #include <set> #include <map> #include <vector> #include <algorithm> #include <cstring> #include <string> #define N 500009 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; namespace BIT { ll c[N]; int lowbit(int t) { return t&(-t); } ll query(int x) { ll re=0; for(;x>0;x-=lowbit(x)) re+=c[x]; return re; } void update(int x,ll v) { for(;x<N;x+=lowbit(x)) c[x]+=v; } }; int n,m,Q; ll answer[N]; struct node { you are, r, v, d; node(int l=0,int r=0,int v=0,int d=0):l(l),r(r),v(v),d(d){} bool operator<(node b) const { return l<b.l; } } A [n], c [n]; set<node>se; set<node>::iterator t; struct ask{ int l,r,id; }as[N]; bool cmp(ask a,ask b) { return a.r<b.r; } void split(node a,int pos) { if(a.l==a.r||pos<=a.l||pos>a.r) return; node p=a; se.erase (a); p.l=pos,a.r=pos-1; se.insert(a); se.insert(p); } void Insert(node a) { int = l to r = r, v = w, d = ad, even = 0; set<node>::iterator itl,itr,it; itl=se.lower_bound(node(l+1,0,0,0)); if (ITL! = se.begin ()) --itl, Split (ITL * l); itr=se.lower_bound(node(r+1,0,0,0)); if(itr!=se.begin()) --itr,split(*itr,r+1); itl=se.lower_bound(node(l,0,0,0)); itr=se.lower_bound(node(r+1,0,0,0)); for(it=itl;it!=itr;it++) { BIT::update((*it).d,-(ll)((*it).r-(*it).l+1)*(*it).v); Cn [so ++] = 'it; } for(int i=1;i<=tot;++i) se.erase(cn[i]); BIT::update(a.d,(ll)(a.r-a.l+1)*a.v); se.insert(a); } int main () { // setIO("input"); int i,j; scanf("%d%d%d",&n,&m,&Q); for(i=1;i<=n;++i) scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].v),a[i].d=i; for(i=1;i<=Q;++i) scanf("%d%d",&as[i].l,&as[i].r),as[i].id=i; sort(as+1,as+1+Q,cmp); for(j=i=1;i<=Q;++i) { for(;j<=n&&a[j].d<=as[i].r;++j) Insert(a[j]); answer[as[i].id]=BIT::query(as[i].r)-BIT::query(as[i].l-1); } for(i=1;i<=Q;++i) printf("%lld\n",answer[i]); return 0; }