码蹄集
T1 cup
使用cdq分治和树状数组动态维护逆序对
T2 查询
树套树求区间中小于r的值和小于l的值作差(改的y总板子)
#include<bits/stdc++.h>
using namespace std;
const int N = 20000010 , INF = 1e9;
int n, m;
struct Node{
int s[2], p, v;
int size;
void init(int _v, int _p){
v = _v, p = _p;
size = 1;
}
}tr[N];
int L[N], R[N], T[N], idx;
int w[N];
void pushup(int x){
tr[x].size = tr[tr[x].s[0]].size + tr[tr[x].s[1]].size + 1;
}
void rotate(int x){
int y = tr[x].p, z = tr[y].p;
int k = tr[y].s[1] == x;
tr[z].s[tr[z].s[1] == y] = x, tr[x].p = z;
tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
tr[x].s[k ^ 1] = y, tr[y].p = x;
pushup(y), pushup(x);
}
void splay(int& root, int x, int k){
while (tr[x].p != k){
int y = tr[x].p, z = tr[y].p;
if (z != k)
if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x);
else rotate(y);
rotate(x);
}
if (!k) root = x;
}
void insert(int& root, int v){
int u = root, p = 0;
while (u) p = u, u = tr[u].s[v > tr[u].v];
u = ++ idx;
if (p) tr[p].s[v > tr[p].v] = u;
tr[u].init(v, p);
splay(root, u, 0);
}
int get_k(int root, int v){
int u = root, res = 0;
while (u){
if (tr[u].v < v) res += tr[tr[u].s[0]].size + 1, u = tr[u].s[1];
else u = tr[u].s[0];
}
return res;
}
void update(int& root, int x, int y){
int u = root;
while (u){
if (tr[u].v == x) break;
if (tr[u].v < x) u = tr[u].s[1];
else u = tr[u].s[0];
}
splay(root, u, 0);
int l = tr[u].s[0], r = tr[u].s[1];
while (tr[l].s[1]) l = tr[l].s[1];
while (tr[r].s[0]) r = tr[r].s[0];
splay(root, l, 0), splay(root, r, l);
tr[r].s[0] = 0;
pushup(r), pushup(l);
insert(root, y);
}
void build(int u, int l, int r){
L[u] = l, R[u] = r;
insert(T[u], -INF), insert(T[u], INF);
for (int i = l; i <= r; i ++ ) insert(T[u], w[i]);
if (l == r) return;
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}
int query(int u, int a, int b, int x){
if (L[u] >= a && R[u] <= b) return get_k(T[u], x) - 1;
int mid = L[u] + R[u] >> 1, res = 0;
if (a <= mid) res += query(u << 1, a, b, x);
if (b > mid) res += query(u << 1 | 1, a, b, x);
return res;
}
void change(int u, int p, int x){
update(T[u], w[p], x);
if (L[u] == R[u]) return;
int mid = L[u] + R[u] >> 1;
if (p <= mid) change(u << 1, p, x);
else change(u << 1 | 1, p, x);
}
int get_pre(int root, int v){
int u = root, res = -INF;
while (u){
if (tr[u].v < v) res = max(res, tr[u].v), u = tr[u].s[1];
else u = tr[u].s[0];
}
return res;
}
int get_suc(int root, int v){
int u = root, res = INF;
while (u){
if (tr[u].v > v) res = min(res, tr[u].v), u = tr[u].s[0];
else u = tr[u].s[1];
}
return res;
}
int query_pre(int u, int a, int b, int x){
if (L[u] >= a && R[u] <= b) return get_pre(T[u], x);
int mid = L[u] + R[u] >> 1, res = -INF;
if (a <= mid) res = max(res, query_pre(u << 1, a, b, x));
if (b > mid) res = max(res, query_pre(u << 1 | 1, a, b, x));
return res;
}
int query_suc(int u, int a, int b, int x){
if (L[u] >= a && R[u] <= b) return get_suc(T[u], x);
int mid = L[u] + R[u] >> 1, res = INF;
if (a <= mid) res = min(res, query_suc(u << 1, a, b, x));
if (b > mid) res = min(res, query_suc(u << 1 | 1, a, b, x));
return res;
}
int main(){
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
build(1, 1, n);
scanf("%d", &m);
while (m -- ){
int op, a, b, x, y;
scanf("%d", &op);
if (op == 0){
scanf("%d%d%d%d", &a, &b, &x, &y);
int ans1 = query(1, a, b, x);
int ans2 = query(1, a, b, y+1);
printf("%d\n",ans2 - ans1);
}
else if (op == 1){
scanf("%d%d", &a, &x);
change(1, a, x);
w[a] = x;
}
}
return 0;
}
T3 捕鱼
首先可以明确的是捕鱼器一定在海岸线上
将每条鱼视为圆心做半径为d的圆同海岸线的交点有三种状态,相离 相切,相交
对于相离的状态直接判断无解
对于相交和相切的状态可以得到两个交点组成的区间(相切就是左右端点相同),捕鱼器在这个区间内都能覆盖这条鱼,这样就变成了区间找点问题。简单贪心即可
按区间右端点的大小顺序由小到大进行排序。最开始以首个区间的右端点作为基准点,用下一个区间的左端点与其进行比较,如果是错开的,那么说明要增加一个点数,然后以这个区间的右端点作为一个新的基准,进行下次比较。
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define tii tuple<int,int,int>
#define debug() cout << "wtl" << endl;
#define int long long
#define endl '\n'
long long read(){
long long ret=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
const int N = 1000;
int n; double d;
pair<double,double >p[N + 11];
double dis(double y){
return sqrt(d * d - y * y);
}
signed main(){
n = read(); d = read();
bool f = false;
for(int i = 1 ; i <= n; i++){
int x , y; x = read(); y = read();
if(y > d){
f = true;
continue;
}
double delta = dis(y);
p[i] = {x + delta, x - delta};//r,l
}
if(f == true){
cout << -1 << endl;
return 0;
}
sort(p + 1, p + 1 + n);
int ans = 1;
for(int i = 1,j = 1; i <= n; i++){
if(p[i].second > p[j].first){
j = i;
ans++;
}
}
cout << ans << endl;
return 0;
}
T4 射线的交
数据很小,直接暴力枚举即可
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define tii tuple<int,int,int>
#define debug() cout << "wtl" << endl;
#define int long long
#define endl '\n'
long long read(){
long long ret=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
int n;
struct node{
int x,y;
char tag;
}r[511],c[511];
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
cin >> n;
int cnt1 = 0; int cnt2 = 0;
for(int i = 1; i <= n; i++){
int x,y;char op;
cin >> x >> y >> op;
if(op == 'L' || op == 'R'){
r[++cnt1].x = x; r[cnt1].y = y; r[cnt1].tag = op;
}else{
c[++cnt2].x = x; c[cnt2].y = y; c[cnt2].tag = op;
}
}
int ans = 0;
for(int i = 1; i <= cnt2; i++){
int op = c[i].tag;
int x = c[i].x;
int y = c[i].y;
for(int j = 1; j <= cnt1; j++){
if(op == 'U'){
if(r[j].tag == 'L'){
if(r[j].x >= x && r[j].y >= y){
ans++;
}
}else{
if(r[j].x <= x && r[j].y >= y){
ans++;
}
}
}else{
if(r[j].tag == 'L'){
if(r[j].x >= x && r[j].y <= y){
ans++;
}
}else{
if(r[j].x <= x && r[j].y <= y){
ans++;
}
}
}
}
}
cout << ans << endl;
}
T5 平衡
每一次操作对后面点的影响是可以累加的,通过cnt记录每次操作的累加和
,依次将每位数加上cnt%3的值判断对后面的影响即可
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define tii tuple<int,int,int>
#define debug() cout << "wtl" << endl;
#define int long long
#define endl '\n'
long long read(){
long long ret=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
string s;
int cnt = 0;
signed main(){
cin >> s;
int ans = 0;
for(int i = 0; i < s.size(); i++){
int num = s[i] - '0';
if((num + cnt) % 3 == 1){
cnt = (cnt + 3 - 1) % 3;
ans ++;
}else if((num + cnt) % 3 == 2){
cnt = (cnt + 3 + 1) % 3;
ans ++;
}
}
cout << ans << endl;
}