Submission #813526

# Submission time Handle Problem Language Result Execution time Memory
813526 2023-08-07T19:35:41 Z JakobZorz Nuclearia (CEOI15_nuclearia) C++14
100 / 100
645 ms 88368 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <stack>
#include <limits.h>
#include <math.h>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <cstring>
#include <sstream>
 
#pragma GCC target("popcnt")
 
typedef long long ll;
typedef long double ld;
using namespace std;
const int MOD=1e9+7;
typedef pair<ll,ll>point;
//#define x first
//#define y second

int w,h;

class Matrix{
    vector<ll>map_arr;
public:
    void init_map(){
        map_arr.resize(w*h);
    }

    ll get2(int x,int y){
        if(x<0||y<0)
            return 0;
        //if(x>=w||y>=h)
            //assert(false);
        return map_arr[y*w+x];
    }

    ll&get(int x,int y){
        //if(x<0||y<0||x>=w||y>=h)
            //assert(false);
        
        return map_arr[y*w+x];
    }

    void add_bounded(int x,int y,ll val){
        if(x<0)
            x=0;
        if(y<0)
            y=0;
        if(x>=w)
            return;
        if(y>=h)
            return;
        get(x,y)+=val;
    }
};
 
Matrix mat,mat_diag1,mat_diag2;
vector<ll>hor_vec,ver_vec;

void add_hor_line(int x1,int x2,ll val){
    if(x1>=x2)
        return;
    hor_vec[x1]+=val;
    if(x2<w)
        hor_vec[x2]-=val;
}

void add_ver_line(int y1,int y2,ll val){
    if(y1>=y2)
        return;
    ver_vec[y1]+=val;
    if(y2<h)
        ver_vec[y2]-=val;
}

void add_diagonal1(int x,int y,int l,ll val){
    //cout<<"call: "<<x<<" "<<y<<" "<<l<<" "<<val<<"\n";
    mat_diag1.add_bounded(x,y,val);
    mat_diag1.add_bounded(x+l,y+l,-val);
    //for(int i=0;i<l;i++)
        //mat_diag1.get(x+i,y+i)+=val;
}

void add_diagonal2(int x,int y,int l,ll val){
    mat_diag2.add_bounded(x,y,val);
    mat_diag2.add_bounded(x-l,y+l,-val);
    
    //for(int i=0;i<l;i++)
        //mat_diag2.get(x-i,y+i)+=val;
}

int main(){
    ios::sync_with_stdio(false);
    cout.tie(NULL);
    cin.tie(NULL);
    
    cin>>w>>h;
    mat.init_map();
    mat_diag1.init_map();
    mat_diag2.init_map();
    hor_vec.resize(w);
    ver_vec.resize(h);
    int n;
    cin>>n;
    while(n--){
        int x1,y1;
        ll center,step;
        cin>>x1>>y1>>center>>step;
        x1--;y1--;
        
        ll last_step=(center-1)%step+1;
        int num_steps=(int)(center+step-1)/step;
        
        if(x1>y1){
            // hits ceiling
            add_diagonal1(x1-min(num_steps-1,y1)+1,y1-min(num_steps-1,y1)+1,min(num_steps-1,y1),step);
            
            add_hor_line(x1-min(num_steps-1,x1)+1,x1-y1+1,step);
            
            if(x1<num_steps-1)
                mat.get(0,0)+=step*(num_steps-1-x1);
        }else{
            // hits left wall
            add_diagonal1(x1-min(num_steps-1,x1)+1,y1-min(num_steps-1,x1)+1,min(num_steps-1,x1),step);
            
            add_ver_line(y1-min(num_steps-1,y1)+1,y1-x1+1,step);
            
            if(y1<num_steps-1)
                mat.get(0,0)+=step*(num_steps-1-y1);
        }
        
        if(x1+y1<w){
            // hits ceiling
            for(int i=0;i<min(num_steps-1,y1);i++)
                mat.get(x1+min(num_steps-1,y1)-i,y1-min(num_steps-1,y1)+1+i)-=step;
            
            add_hor_line(y1+x1+1,min(num_steps+x1,w),-step);
            
        }else
            add_diagonal2(x1+min(num_steps-1,w-x1-1),y1-min(num_steps-1,w-x1-1)+1,min(num_steps-1,w-x1-1),-step);
        
        if(x1+y1<h){
            // hits left wall
            add_diagonal2(x1,y1+1,min(num_steps-1,x1),-step);
            
            add_ver_line(x1+y1+1,min(num_steps+y1,h),-step);
            
        }else
            add_diagonal2(x1,y1+1,min(num_steps-1,h-y1-1),-step);
        
        if(w-x1<h-y1)
            add_diagonal1(x1+1,y1+1,min(num_steps-1,w-x1-1),step);
        else
            add_diagonal1(x1+1,y1+1,min(num_steps-1,h-y1-1),step);

        mat.add_bounded(x1-num_steps+1, y1-num_steps+1, last_step);
        mat.add_bounded(x1-num_steps+1, y1+num_steps,   -last_step);
        mat.add_bounded(x1+num_steps,   y1-num_steps+1, -last_step);
        mat.add_bounded(x1+num_steps,   y1+num_steps,   last_step);
    }
    
    for(int i=0;i<w;i++){
        if(i)
            hor_vec[i]+=hor_vec[i-1];
        mat.get(i,0)+=hor_vec[i];
    }
    
    for(int i=0;i<h;i++){
        if(i)
            ver_vec[i]+=ver_vec[i-1];
        mat.get(0,i)+=ver_vec[i];
    }
    
    /*for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            cout<<mat_diag1.get(x,y)<<" ";
        }
        cout<<"\n";
    }*/
    
    for(int y=0;y<h;y++)
        for(int i=1;y+i<h&&i<w;i++)
            mat_diag1.get(i,y+i)+=mat_diag1.get(i-1,y+i-1);
    
    for(int x=1;x<w;x++)
        for(int i=1;x+i<w&&i<h;i++)
            mat_diag1.get(x+i,i)+=mat_diag1.get(x+i-1,i-1);
    
    
    for(int x=0;x<w;x++)
        for(int i=1;x-i>=0&&i<h;i++)
            mat_diag2.get(x-i,i)+=mat_diag2.get(x-i+1,i-1);
    
    for(int y=1;y<h;y++)
        for(int i=0;w-i>=0&&y+i<h;i++)
            mat_diag2.get(w-i-1,y+i)+=mat_diag2.get(w-i,y+i-1);
    
    /*for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            cout<<mat_diag1.get(x,y)<<" ";
        }
        cout<<"\n";
    }*/
    
    for(int x=0;x<w;x++)
        for(int y=0;y<h;y++)
            mat.get(x,y)+=mat.get2(x-1,y)+mat.get2(x,y-1)-mat.get2(x-1,y-1)+mat_diag1.get(x,y)+mat_diag2.get(x,y);
    
    /*for(int y=0;y<h;y++){
        for(int x=0;x<w;x++){
            cout<<mat.get(x,y)<<" ";
        }
        cout<<"\n";
    }*/
    
    for(int x=0;x<w;x++)
        for(int y=0;y<h;y++)
            mat.get(x,y)+=mat.get2(x-1,y)+mat.get2(x,y-1)-mat.get2(x-1,y-1);
    
    int q;
    cin>>q;
    while(q--){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        x1--;y1--;
        
        ll sum=mat.get2(x2-1,y2-1)+mat.get2(x1-1,y1-1)-mat.get2(x1-1,y2-1)-mat.get2(x2-1,y1-1);
        
        ll area=(x2-x1)*(y2-y1);
        ll res=sum/area;
        if((sum%area)*2>=area)
            res++;
        cout<<res<<"\n";
    }

    return 0;
}

/*

4 3
2
1 1 7 3
3 2 4 2
4
1 2 2 3
1 1 4 3
4 2 4 2
1 3 4 3

*/
# Verdict Execution time Memory Grader output
1 Correct 57 ms 78604 KB Output is correct
2 Correct 47 ms 3924 KB Output is correct
3 Correct 44 ms 3296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 78584 KB Output is correct
2 Correct 57 ms 4000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 59064 KB Output is correct
2 Correct 47 ms 3652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 59988 KB Output is correct
2 Correct 60 ms 3896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 134 ms 82664 KB Output is correct
2 Correct 52 ms 4556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 35728 KB Output is correct
2 Correct 48 ms 3952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 63168 KB Output is correct
2 Correct 50 ms 4192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 31768 KB Output is correct
2 Correct 48 ms 3728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 82732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 82724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 388 ms 64204 KB Output is correct
2 Correct 388 ms 63944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 446 ms 64340 KB Output is correct
2 Correct 338 ms 63420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 248 ms 63440 KB Output is correct
2 Correct 464 ms 62560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 645 ms 63960 KB Output is correct
2 Correct 252 ms 88368 KB Output is correct