4822: [Cqoi2017]老C的任务
练手速...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
inline int read() {
char c=getchar(); int x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int n, Q, mp[N], tot, x1, y1, x2, y2; ll ans[N];
struct meow {
int x, y, type, qid; ll v;
bool operator <(const meow &a) const {return x == a.x ? type < a.type : x < a.x;}
} q[N];
namespace bit {
int n; ll c[N];
inline void add(int p, ll v) {for(; p<=n; p+=p&-p) c[p] += v;}
inline ll sum(int p) {ll ans = 0; for(; p; p-=p&-p) ans += c[p]; return ans;}
}
int main() {
freopen("in", "r", stdin);
n = read(); Q = read();
for(int i=1; i<=n; i++)
mp[++mp[0]] = q[i].x = read(), mp[++mp[0]] = q[i].y = read(), q[i].v = read(), q[i].type = 0;
tot = n;
for(int i=1; i<=Q; i++) {
mp[++mp[0]] = x1 = read()-1; mp[++mp[0]] = y1 = read()-1;
mp[++mp[0]] = x2 = read(); mp[++mp[0]] = y2 = read();
tot++; q[tot] = (meow){x1, y1, 1, i, 1LL};
tot++; q[tot] = (meow){x2, y2, 1, i, 1LL};
tot++; q[tot] = (meow){x2, y1, 1, i, -1LL};
tot++; q[tot] = (meow){x1, y2, 1, i, -1LL};
}
sort(mp+1, mp+1+mp[0]); mp[0] = unique(mp+1, mp+1+mp[0]) - mp - 1;
for(int i=1; i<=tot; i++) {
q[i].x = lower_bound(mp+1, mp+1+mp[0], q[i].x) - mp;
q[i].y = lower_bound(mp+1, mp+1+mp[0], q[i].y) - mp;
}
sort(q+1, q+1+tot);
bit::n = mp[0];
for(int i=1; i<=tot; i++) {
if(q[i].type) ans[q[i].qid] += q[i].v * bit::sum(q[i].y);
else bit::add(q[i].y, q[i].v);
}
for(int i=1; i<=Q; i++) printf("%lld
", ans[i]);
}