当前位置:网站首页>Scan line, weight segment tree
Scan line, weight segment tree
2022-07-24 10:06:00 【xingxg.】
Two dimensional count

It's not easy to understand , It's rough, isn't it . But when I understand it, I will really sigh
focus:
Mutual exclusion theory
vx It is stored at various points x coordinate , Later, it is used for discretization
vector<array<int, 4>> event Indicates an event , Insert a point , Add , reduce It's an event of different types
Use the dynamic idea of scanline , Give priority to y Sort , The second is the type of event and x, Finally, the number of the query . Sorting time , Type of event and x In this topic, the order can be interchanged ( Measured ), The data stored in the array should be stored according to your requirements for sorting . array Sorting and pair similar , All of them compare the first , And then the second one ....
// problem : Mutually exclusive 、 discretization 、 Tree array
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int, int> PII;
#define pb push_back
const int N = 2e5 + 5;
int n, q, m, c[N], ans[N];
std::vector<int> vx;
vector<array<int, 4>> event;
void modify(int x, int d) {
for(; x <= m; x += x & -x)
c[x] += d;
}
int query(int x) {
int res = 0;
for(; x; x -= x & -x)
res += c[x];
return res;
}
int main(){
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; ++i) {
int x, y; scanf("%d %d", &x, &y);
vx.push_back(x);
event.push_back({y, 0, x});
}
for(int i = 1; i <= q; ++i) {
int x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &x2, &y1, &y2);
event.push_back({y2, 1, x2, i}); // Event type 1: Add 2: Subtraction ( interchangeable )
event.push_back({y1 - 1, 1, x1 - 1, i}); // But the insertion must be 0 ( High priority )
event.push_back({y2, 2, x1 - 1, i});
event.push_back({y1 - 1, 2, x2, i});
}
sort(vx.begin(), vx.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());
sort(event.begin(), event.end());
m = vx.size(); // vx After discretization , Only m A little bit , So the coordinates are [1,m] , The size of the tree array is m
for(auto evt : event) {
if(evt[1] == 0) {
int pos = lower_bound(vx.begin(), vx.end(), evt[2]) - vx.begin() + 1;
modify(pos, 1);
} else {
int pos = upper_bound(vx.begin(), vx.end(), evt[2]) - vx.begin();
int tmp = query(pos);
if(evt[1] == 1)ans[evt[3]] += tmp;
else ans[evt[3]] -= tmp;
}
}
for(int i = 1; i <= q; ++i)
printf("%d\n", ans[i]);
return 0;
}边栏推荐
- Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?
- [STM32 learning] (8) stm32f1 general timer configuration
- Gaode map
- Deployment and analysis of coredns
- Embedded development: Tools - optimizing firmware using DRT
- String sort
- Analysis of Kube proxy IPVS mode
- 2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
- Taurus. How to upgrade and run MVC on net6 and net7
- Anti shake and throttling
猜你喜欢

Analysis of Kube proxy IPVS mode

2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices

Dr. water 3

ASI-20220222-Implicit PendingIntent

The heads of the five major international institutions called for urgent action to deal with the global food security crisis
![[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus](/img/8f/19b0eb959d2b3f896c8e99f8e673d1.png)
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus

Android Version Description security privacy 13

note: expected ‘void * (***)(void ***)’ but argument is of type ‘void (*)(void *)’

Home raiding III (leetcode-337)

Server load and CPU performance tuning
随机推荐
Balance between management / business and technology
PHP Basics - PHP magic constants
[example] v-contextmenu right click menu component
In the envy of LETV, there is a "meaning crisis" of contemporary workers
cannot unpack non-iterable NoneType object
[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)
Spark Learning: Spark implementation of distcp
[C language] implementation of three versions of address book small project (including source code)
[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)
Spark Learning: implement compact table command
Analysis of Kube proxy IPVS mode
The best time to buy and sell stocks (leetcode-121)
The most complete solution for distributed transactions
ThreeJs
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
Uniapp uses PWA
Do you really understand the concept of buffer? Take you to uncover the buffer zone~
Rust tokio:: task:: localset running mode
Synchronized scope "concurrent programming"
MySQL status view qps/tps/ cache hit rate view
