QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65059#4622. Mario PartyqinjianbinAC ✓5286ms95252kbC++6.0kb2022-11-26 21:55:312022-11-26 21:55:31

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-26 21:55:31]
  • 评测
  • 测评结果:AC
  • 用时:5286ms
  • 内存:95252kb
  • [2022-11-26 21:55:31]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<ctime>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<functional>
using namespace std;
//STL库常用(vector, map, set, pair)
#define pii pair<int,int>
#define pdd pair<double,double>
#define F first
#define S second
//常量
#define PI (acos(-1.0))
#define INF 0x3f3f3f3f
//必加
#ifdef TanJI
#include<cassert>
#define dbg(...) do {cerr << #__VA_ARGS__ << " -> "; err(__VA_ARGS__); } while(0)
void err() {cerr << endl;}
template<template<typename...> class T, typename t, typename... A> void err(T<t> a, A... x) {for (auto v: a) cerr << v << ' '; cerr << ", "; err(x...);}
template<typename T, typename... A> void err(T a, A... x) {cerr << a << ' '; err(x...);}
template<typename T> void err(T *a, int len) {for (int i = 0; i < len; i++) cerr << a[i] << ' '; err();}
#else 
#define dbg(...)
#define assert(...)
#endif
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
// 快读快输
template<typename T> inline T read() {T x=0; bool f=0; char c=getchar();while(!isdigit(c)){if(c =='-')f= 1; c=getchar();}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}return f?~x+1:x;}
template<typename T> inline void print(T k){ int num = 0,ch[20]; if(k == 0){ putchar('0'); return ; } (k<0)&&(putchar('-'),k = -k); while(k>0) ch[++num] = k%10, k /= 10; while(num) putchar(ch[num--]+48); }
//图论
const int GRAPHN = 1, GRAPHM = 1;
int h[GRAPHN], e[GRAPHM], ne[GRAPHM], wei[GRAPHM], idx;
void AddEdge(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}
void AddEdge(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], wei[idx] = c, h[a] = idx++;}
void AddEdge(int h[], int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}
void AddEdge(int h[], int a, int b, int c) {e[idx] = b, ne[idx] = h[a], wei[idx] = c, h[a] = idx++;}
// 数学公式
template<typename T> inline T gcd(T a, T b){ return b==0 ? a : gcd(b,a%b); }
template<typename T> inline T lowbit(T x){ return x&(-x); }
template<typename T> inline bool mishu(T x){ return x>0?(x&(x-1))==0:false; }
template<typename T1,typename T2, typename T3> inline ll q_mul(T1 a,T2 b,T3 p){ ll w = 0; while(b){ if(b&1) w = (w+a)%p; b>>=1; a = (a+a)%p; } return w; }
template<typename T,typename T2> inline ll f_mul(T a,T b,T2 p){ return (a*b - (ll)((long double)a/p*b)*p+p)%p; }
template<typename T1,typename T2, typename T3> inline ll q_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = (w*a)%p; b>>=1; a = (a*a)%p;} return w; }
template<typename T1,typename T2, typename T3> inline ll s_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = q_mul(w,a,p); b>>=1; a = q_mul(a,a,p);} return w; }
template<typename T> inline ll ex_gcd(T a, T b, T& x, T& y){ if(b == 0){ x = 1, y = 0; return (ll)a; } ll r = ex_gcd(b,a%b,y,x); y -= a/b*x; return r;/*gcd*/ }
template<typename T1,typename T2> inline ll com(T1 m, T2 n) { int k = 1;ll ans = 1; while(k <= n){ ans=((m-k+1)*ans)/k;k++;} return ans; }
template<typename T> inline bool isprime(T n){ if(n <= 3) return n>1; if(n%6 != 1 && n%6 != 5) return 0; T n_s = floor(sqrt((db)(n))); for(int i = 5; i <= n_s; i += 6){ if(n%i == 0 || n%(i+2) == 0) return 0; } return 1; }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------- */

const int MAXN = 1e6 + 10;

int T, n, q;
int a[MAXN];
int fa[MAXN];
int Find(int u) { 
    int a = u;
    while (u != fa[u]) 
        u = fa[u];
    while (a != fa[a]) {
        int b = a;
        a = fa[a];
        fa[b] = u;
    }
    return u;
}
int pos[MAXN], invp[MAXN * 3]; // 每个询问指向的值,值反函数对应哪些询问并查集的头节点(用于合并询问)

void Merge(int val1, int val2) {
    if (invp[val1] == -1) return;
    if (invp[val2] == -1) {
        invp[val2] = invp[val1];
        invp[val1] = -1;
        pos[invp[val2]] = val2;
        return;
    }
    fa[invp[val1]] = invp[val2];
    invp[val1] = -1;
}

map<int, vector<int> > add, del;
int x[MAXN], ans[MAXN];
void init() {
    for (int i = 1; i <= q; i++)
        fa[i] = i;
    for (int i = 1; i <= q; i++)
        pos[i] = -1;
    for (int i = 0; i <= 3e6; i++)
        invp[i] = -1;
    add.clear();
    del.clear();
}


int main() {
#ifdef TanJI
    clock_t c1 = clock();
    freopen("D:\\Cpp\\1.in", "r", stdin);
    freopen("D:\\Cpp\\1.out", "w", stdout);
#endif
    //--------------------------------------------
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &q);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        init();
        int l, r;
        for (int i = 1; i <= q; i++) {
            scanf("%d%d%d", &l, &r, &x[i]);
            add[l].push_back(i);
            del[r].push_back(i);
        }
        int start = 1e6;
        for (int i = 1; i <= n; i++) {
            if (a[i] < 0)
                for (int j = start; j < start - a[i]; j++)
                    Merge(j, j - a[i]);
            start -= a[i];
            // dbg(start);
            if (add.count(i))
            for (auto u : add[i]) {
                int now = x[u] + start;
                if (invp[now] == -1) {
                    pos[u] = now;
                    invp[now] = u;
                    // dbg(now);
                } else {
                    // merege
                    fa[u] = invp[now];
                }
            }
            if (del.count(i))
            for (auto u : del[i]) {
                ans[u] = pos[Find(u)] - start;
                // dbg(u, Find(u), pos[Find(u)], start);
            }
        }
        for (int i = 1; i <= q; i++)
            printf("%d\n", ans[i]);
    }
//--------------------------------------------
#ifdef TanJI
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5286ms
memory: 95252kb

input:

4
500000 500000
2 -1 2 -1 1 -1 -4 -2 0 -3 4 1 -3 3 5 1 -3 -2 0 3 -2 0 -1 0 0 -3 -2 0 0 1 -2 -3 -4 2 5 0 -1 -2 2 2 -2 -1 2 0 0 -2 3 -2 -4 -3 -3 -1 0 2 1 0 2 -1 -3 0 -2 3 2 -2 1 4 -1 -1 -2 0 2 -2 1 -4 -5 -1 -2 -3 -2 1 4 2 -3 0 1 -1 2 2 0 5 -3 0 2 -1 5 -3 5 -4 -4 0 2 2 1 -1 -6 0 2 -3 2 3 0 0 -2 3 4 2 1...

output:

1363
427
212
818
736
132
561
623
81
800
12
971
218
28
278
737
581
363
25
231
216
642
684
283
1033
982
469
106
156
197
992
384
230
112
226
38
31
561
637
43
623
246
397
1640
1252
1354
4
1338
168
266
364
68
10
435
1391
229
106
0
1114
1416
201
416
206
113
474
190
612
206
109
670
34
0
804
604
376
164
251...

result:

ok 2000000 lines