2025.07.28课堂总结

· · 个人记录

2025.07.28课堂总结

T638034 真实社交

这题就是一个图的存储,因为我map<int, vector<int> > mp;写的太史了,甚至还要用循环遍历,看了正解后我纳闷:我当时怎么没想出用map<int, vector<int> > mp;标记而非直接记录人的id 示例如图:
本图记录的则是1和2壶关了。查询3 1 2时则输出Yes。
正解:

#include<bits/stdc++.h>
using namespace std;
map<int, map<int, int > >q;//记录的是a是否关注b,即q[a][b] = 1;
int n, p, op, a, b;
int main(){
    cin>> n >> p;
    while(p--){
        cin>> op >> a >> b;
        if(op == 1) q[a][b] = 1;//灌注了
        else if(op == 2) q[a][b] = 0;//取灌了
        else if(op == 3){//查询
            if(q[a][b] == q[b][a] && q[a][b] == 1) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
    return 0;
}

T638156 落落的去维护数组

本题我从一开始是用暴力写的,写出了70分。(暴力代码就不挂在这了)lldq说使用一种叫做时间戳的牛而逼之的算法,可以将时间复杂度从o(n)到o(1)。正解如下:

#include<bits/stdc++.h>
using namespace std;
long long n, a[2000005], q, last_time, last_k, t[2000005], x;
int main(){ 
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    cin >> q;
    for(int i = 1;i <= q; i++){
        int op, b, k;
        cin >> op;
        if(op == 1){
            cin >> k;
            last_time = i;//操作序号
            last_k = k;//新值
        }
        else if(op == 2){
            cin >> x >> k;
            if(last_time == 0) a[x] += k;//无操作时直接修改
            else if(t[x] < last_time) a[x] = k;//若该位置自上次操作1后未被修改,重置增量
            else if(t[x] > last_time) a[x] += k;//若该位置自上次操作1后已被修改,累加增量
                t[x] = i;
        }else{
            cin >> x;
            if(last_time == 0) cout<< a[x] <<endl;//无操作1时:直接输出
            else if(t[x] < last_time) cout<< last_k <<endl;//若该位置自上次操作1后未被修改,输出全局值
            else if(t[x] > last_time) cout<<a[x]+last_k <<endl; //输出全局值 + 增量
        }
    }
    return 0;
}

T638430 交叉区间

本题就是总结其规律并且写,思路难,代码实现难,这类题只能靠总结和练才可提升啊!

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    vector<pair<int, int>> k;
    vector<int> l, r;

    for (int i = 0; i < n; i++) {
        int ll, rr;
        cin >> ll >> rr;
        k.push_back({ll, rr});
        l.push_back(ll);
        r.push_back(rr);
    }

    sort(l.begin(), l.end());
    sort(r.begin(), r.end());

    int tot = n * (n - 1) / 2;
    int sum = 0;

    for (const auto& i : k) {
        int l_i = i.first;
        int r_i = i.second;

        // 统计左端点大于r_i的区间数
        auto pos1 = upper_bound(l.begin(), l.end(), r_i);
        int cnt1 = l.end() - pos1;  

        // 统计右端点小于l_i的区间数
        auto pos2 = lower_bound(r.begin(), r.end(), l_i);
        int cnt2 = pos2 - r.begin();  

        sum += cnt1 + cnt2;
    }

    int t = sum / 2;
    int ans = tot - t;
    cout << ans << endl;

    return 0;
}

好了,这次的总结就写到这里了,感谢您的观看😊😊😊